MySQL备份恢复第一篇(r5笔记第5天)
今天学习了下MySQL的备份恢复内容,也算是对之前的 数据导入导出的一个细化内容。备份恢复的内容其实还是蛮复杂的,一般网站上提到的备份恢复也基本都是逻辑备份恢复的内容。对于更为高效的备份mysqlbackup和恢复的内容提到的很少,mysqlbackup是需要Licence,需要单独收费的,这也算是向oracle产品线的一个靠拢了。
首先我们还是走走老路,来看看最基本的逻辑备份恢复吧。我们模拟了3万多条的数据。然后尝试恢复回来。
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select count(*)from new_test;
+----------+
| count(*) |
+----------+
| 32046 |
+----------+
1 row in set (0.02 sec)
mysql> select current_timestamp(); --在删除数据之前,我们先来看看时间戳。
+---------------------+
| current_timestamp() |
+---------------------+
| 2015-04-13 14:52:27 |
+---------------------+
1 row in set (0.00 sec)
然后使用经典工具mysqldump来导出 test的数据。
[mysql@oel1 ~]$ mysqldump -u test --databases test > exp_test.log
然后我们直接运行exp_test.log文件
[mysql@oel1 ~]$ mysql -u test < exp_test.log
[mysql@oel1 ~]$
mysql> use information_schema;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql> select table_schema,table_name,table_type,engine,create_time from tables where table_schema='test';
+--------------+-------------+------------+--------+---------------------+
| table_schema | table_name | table_type | engine | create_time |
+--------------+-------------+------------+--------+---------------------+
| test | innodb_test | BASE TABLE | InnoDB | 2015-04-13 14:54:09 |
| test | myisam_test | BASE TABLE | MyISAM | 2015-04-13 14:54:09 |
| test | new_test | BASE TABLE | InnoDB | 2015-04-13 14:54:09 |
+--------------+-------------+------------+--------+---------------------+
3 rows in set (0.01 sec)
记录时间戳的目的就是可以看到表new_test的创建时间是在恢复之后,也就是说在恢复的时候也是删除了表,然后重建导入数据。
然后我们再次尝试通过source运行脚本来恢复数据,效果也是一样的。
mysql> source exp_test.log
...
mysql> select count(*)from new_test; --可以看到数据都原原本本的回来了。
+----------+
| count(*) |
+----------+
| 32046 |
+----------+
1 row in set (0.03 sec)
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2015-04-13 14:56:32 |
+---------------------+
1 row in set (0.00 sec)
还可以通过如下的方式来导出表结构和表数据,然后分别处理。下面两种方式都是等价的。
mysqldump -u test -T /u02/mysql/dump test
mysqldump -u test --tab=/u02/mysql/dump test
运行后生成的文件结构如下:.txt是数据内容,.sql是表结构语句。
[mysql@oel1 dump]$ ll
total 948
-rw-r--r-- 1 mysql dba 1349 Apr 13 16:02 innodb_test.sql
-rw-rw-rw- 1 mysql dba 2 Apr 13 16:02 innodb_test.txt
-rw-r--r-- 1 mysql dba 1349 Apr 13 16:02 myisam_test.sql
-rw-rw-rw- 1 mysql dba 2 Apr 13 16:02 myisam_test.txt
-rw-r--r-- 1 mysql dba 1380 Apr 13 16:02 new_test.sql
-rw-rw-rw- 1 mysql dba 943641 Apr 13 16:02 new_test.txt
导入数据可以使用下面的两种方式来实现。
[mysql@oel1 dump]$ mysqlimport -u test test /u02/mysql/dump/new_test.txt
test.new_test: Records: 32046 Deleted: 0 Skipped: 0 Warnings: 0
mysql> load data infile '/u02/mysql/dump/new_test.txt' into table new_test;
Query OK, 32046 rows affected (0.23 sec)
Records: 32046 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select count(*)from new_test; --再次导入数据,数据量就翻倍了。
+----------+
| count(*) |
+----------+
| 64092 |
+----------+
1 row in set (0.05 sec)
一些细节的补充。
下面两种方式还是存在着较大的差距。我们来看看差别到底都在哪里。标黄部分就是差别所在。
mysqldump -u test test > b.sql
mysqldump -u test --databases test > a.sql
> sdiff a.sql b.sql
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
-- Current Database: `test` <
-- <
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFA <
USE `test`; <
-- <
-- Table structure for table `innodb_test` -- Table structure for table `innodb_test`
在实际工作中,基本上我们不会导个数据重建个数据,所以第二种方式还是推荐的,不建议使用--databases选项。
mysqlimport导入数据,不能导入ddl语句
[mysql@oel1 dump]$ mysqlimport -u test test '/u02/mysql/dump/innodb_test.sql'
mysqlimport: Error: 1146, Table 'test.innodb_test' doesn't exist, when using table: innodb_test
-----导出存储程序
对于存储程序来说,可以使用下面的选项来选择性的导出,也可以忽略。
--events: Dump Event Scheduler events
--routines: Dump stored procedures and functions
--triggers: Dump triggers for tables
--skip-events, --skip-routines, or --skip-triggers.
只导出表结构
mysqldump -u test --no-data test > dump-defs.sql
只导出表中数据
mysqldump -u test --no-create-info test > dump-data.sql
导出数据中包含存储程序
mysqldump -u test --no-data --routines --events test > dump-defs.sql
以上基本就是逻辑备份的内容了,说起高效的备份,mysqlbackup性能提升还是很大的。不过企业版本的加强版本,还是需要付费的。下载的时候其实也是很方便的。
通过这个链接 http://www.mysql.com/downloads/ 选择enterprise backup对应的安装包即可
至于说安装,其实就是个插件一样,完全不需要安装,解压的事情,解压完配置一下环境变量就好了。
[mysql@oel1 ~]$ ll *.zip
-rw-r--r-- 1 mysql dba 4141359 Apr 13 17:37 V59683-01.zip
[mysql@oel1 ~]$ unzip V59683-01.zip
Archive: V59683-01.zip
extracting: meb-3.11.1-linux-glibc2.5-x86-32bit.tar.gz
extracting: meb-3.11.1-linux-glibc2.5-x86-32bit.tar.gz.asc
extracting: meb-3.11.1-linux-glibc2.5-x86-32bit.tar.gz.md5
extracting: README.txt
[mysql@oel1 ~]$ gunzip meb-3.11.1-linux-glibc2.5-x86-32bit.tar.gz
[mysql@oel1 ~]$ tar -xvf *.tar
meb-3.11.1-linux-glibc2.5-x86-32bit/
meb-3.11.1-linux-glibc2.5-x86-32bit/mvl.css
meb-3.11.1-linux-glibc2.5-x86-32bit/bin/
meb-3.11.1-linux-glibc2.5-x86-32bit/bin/mysqlbackup
meb-3.11.1-linux-glibc2.5-x86-32bit/README.txt
meb-3.11.1-linux-glibc2.5-x86-32bit/LICENSE.mysql
meb-3.11.1-linux-glibc2.5-x86-32bit/manual.html
[mysql@oel1 ~]$
解压好以后,直接配置.bash_profile把mysqlbackup配置到里面就大功告成了。
[mysql@oel1 mysql]$ mysqlbackup --version
MySQL Enterprise Backup version 3.11.1 Linux-2.6.18-274.el5-i686 [2014/11/04]
Copyright (c) 2003, 2014, Oracle and/or its affiliates. All Rights Reserved.
Run mysqlbackup --help for help information.
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解
- 要实现60FPS动画, 你需要了解这些
- 数论-素数
- 自动补全、回滚!介绍一款可视化 sql 诊断利器
- 深度学习-IMBD二分类问题
- 开源、强大的Linux服务器集群管理工具,比宝塔好用!
- 组合数学-抽屉原理
- 利用 Harbor 搭建企业级私有镜像仓库
- Redis-Java项目应用(Jedis、SpringBoot整合、工具类模板)
- 微软被指剽窃他人开源作品!作者被迫终止该项目
- 秋招系列 | 推荐岗史上最强面经来袭(上)
- 一个Tomcat 如何部署多个项目?附多种解决方案及详细步骤!
- 初识SpringCloud
- .NET程序设计复习总结
- Ribbon讲解与应用
- break, continue 和 return 的用法及区别