MySQL Backup myloader
正文
之前的博文当中提到备份工具mydumper的使用,而软件包中还包含了与之对应的恢复工具myloader,本文就总结下myloader的用法。关于mydumper的安装与使用可以参考之前的博文:mysql backup mydumper。
查看myloader的版本信息:
# myloader -v myloader 0.9.5, built against mysql 5.7.21-21
主要选项
# myloader --help usage: myloader [option?] multi-threaded mysql loader help options: -?, --help show help options application options: -d, --directory directory of the dump to import -q, --queries-per-transaction number of queries per transaction, default 1000 -o, --overwrite-tables drop tables if they already exist -b, --database an alternative database to restore into -s, --source-db database to restore -e, --enable-binlog enable binary logging of the restore data -h, --host the host to connect to -u, --user username with the necessary privileges -p, --password user password -a, --ask-password prompt for user password -p, --port tcp/ip port to connect to -s, --socket unix domain socket file to use for connection -t, --threads number of threads to use, default 4 -c, --compress-protocol use compression on the mysql connection -v, --version show the program version and exit -v, --verbose verbosity of output, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2 --defaults-file use a specific defaults file
-d, --directory
要import的备份文件所在目录-q, --queries-per-transaction
每个事务的查询数, 默认为1000-o, --overwrite-tables
如果表存在则删除表-b, --database
指定需要还原成的数据库名-s, --source-db
指定还原的源数据库-e, --enable-binlog
还原数据时记录binlog日志-h, --host
指定连接host-u, --user
指定连接用户,需有相应的权限-p, --password
指定用户密码-a, --ask-password
指定用户密码提示输入-p, --port
指定连接port-s, --socket
指定本地socket文件连接-t, --threads
指定dump线程数, 默认是4-c, --compress-protocol
在mysql连接时使用压缩协议-v, --version
显示程序版本并退出-v, --verbose
显示更详细的输出, 0 = silent, 1 = errors, 2 = warnings, 3 = info, 默认是2--defaults-file
指定默认参数文件
还原流程
前期准备工作与mydumper一致,可参考mydumper备份流程mysql backup mydumper 备份流程。
- 备份全库
# mydumper -h 192.168.58.3 -u admin -a -p 3306 -t 8 -e -o /data/test/
- 查看test2库信息
(root@localhost) [(none)] > use test2; database changed (root@localhost) [test2] > show tables; +-----------------+ | tables_in_test2 | +-----------------+ | t1 | +-----------------+ 1 row in set (0.00 sec) (root@localhost) [test2] > select * from t1; +----+----------+ | id | title | +----+----------+ | 1 | mydumper | | 2 | myloader | +----+----------+ 2 rows in set (0.00 sec)
- 删除test2库
(root@localhost) [(none)] > show databases; +--------------------+ | database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test1 | | test2 | +--------------------+ 6 rows in set (0.01 sec) (root@localhost) [(none)] > drop database test2; query ok, 4 rows affected (0.16 sec) (root@localhost) [(none)] > show databases; +--------------------+ | database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test1 | +--------------------+ 5 rows in set (0.01 sec)
- 还原备份里test2库成test3库
# myloader -h 192.168.58.3 -u admin -a -p 3306 -s test2 -b test3 -d /data/test/
- 查看general log
-- 主线程连接数据库,设置临时session级别参数,默认导入数据时不记录binlog 264 connect admin@dbabd on using tcp/ip 264 query set session wait_timeout = 2147483 264 query set sql_log_bin=0 264 query /*!40014 set foreign_key_checks=0*/ -- 产生了4个子进程,设置会话级参数,默认不记录binlog,关闭自动提交 265 connect admin@dbabd on using tcp/ip 265 query set session wait_timeout = 2147483 265 query set sql_log_bin=0 265 query /*!40101 set names binary*/ 265 query /*!40101 set sql_mode='no_auto_value_on_zero' */ 265 query /*!40014 set unique_checks=0 */ 265 query set autocommit=0 266 connect admin@dbabd on using tcp/ip 266 query set session wait_timeout = 2147483 266 query set sql_log_bin=0 266 query /*!40101 set names binary*/ 266 query /*!40101 set sql_mode='no_auto_value_on_zero' */ 266 query /*!40014 set unique_checks=0 */ 266 query set autocommit=0 267 connect admin@dbabd on using tcp/ip 267 query set session wait_timeout = 2147483 267 query set sql_log_bin=0 267 query /*!40101 set names binary*/ 267 query /*!40101 set sql_mode='no_auto_value_on_zero' */ 267 query /*!40014 set unique_checks=0 */ 267 query set autocommit=0 268 connect admin@dbabd on using tcp/ip 268 query set session wait_timeout = 2147483 268 query set sql_log_bin=0 268 query /*!40101 set names binary*/ 268 query /*!40101 set sql_mode='no_auto_value_on_zero' */ 268 query /*!40014 set unique_checks=0 */ 268 query set autocommit=0 -- 主线程创建还原数据库和表 264 query show create database `test3` 264 query create database `test3` 264 query use `test3` 264 query /*!40101 set names binary*/ 264 query /*!40014 set foreign_key_checks=0*/ 264 query /*!40103 set time_zone='+00:00' */ 264 query create table `t1` ( `id` int(11) not null auto_increment, `title` varchar(10) default null, primary key (`id`) ) engine=innodb auto_increment=3 default charset=utf8 -- 子线程插入表数据 265 query use `test3` 266 quit 265 query start transaction 267 quit 265 query /*!40101 set names binary*/ 265 query /*!40014 set foreign_key_checks=0*/ 265 query /*!40103 set time_zone='+00:00' */ 268 quit 265 query insert into `t1` values (1,"mydumper"), (2,"myloader") 265 query commit 265 quit 264 quit
总结下myloader的工作流程:
- 主线程连接mysql,根据指定选项设置会话级参数;
- 创建多个子线程(默认4个),设置会话级参数,关闭自动提交;
- 主线程创建库和表;
- 子线程插入表数据;
- (如有)主线程创建函数、存储过程、触发器和视图;
- import过程结束。
用法示例
- 还原单个库
# myloader -h 192.168.58.3 -u admin -a -p 3306 -s test1 -o -v 3 -d /data/test/
- 还原库并重命名
# myloader -h 192.168.58.3 -u admin -a -p 3306 -s test1 -b test1_bak -o -v 3 -d /data/test/
- 指定还原子线程数
# myloader -h 192.168.58.3 -u admin -a -p 3306 -s test1 -o -v 3 -t 8 -d /data/test/
很遗憾myloader没有针对单表或都多表的还原操作命令,有一种方法是通过mydumper将个别大表进行指定chunk大小切割备份,并将每张表单独目录存放备份数据,必须是每张表的数据单独放在一个目录中,目录中只包含一张表的信息代表着还原时只须还原这一张表,避免影响相同库下其他的表数据,因为myloader只能指定库级别的参数选项,这样就可以间接使用到myloader多线程还原功能,加快还原单表速度。
以下做个测试:
-- test1.t3表有100万行 (root@localhost) [test1] > select count(*) from t3; +----------+ | count(*) | +----------+ | 1000000 | +----------+ 1 row in set (0.00 sec) -- mydumper备份test1.t3,指定chunk大小为3m # mydumper -h 192.168.58.3 -u admin -a -p 3306 -b test1 -t t3 -f 2 -e -o /data/test/ -- 删除test1.t3表 (root@localhost) [test1] > drop table t3; query ok, 0 rows affected (0.04 sec) (root@localhost) [test1] > select * from t3; empty set (0.00 sec) -- myloader还原test1.t3 # myloader -h 192.168.58.3 -u admin -a -p 3306 -s test1 -o -v 3 -d /data/test/ ** message: 4 threads created ** message: dropping table or view (if exists) `test1`.`t3` ** message: creating table `test1`.`t3` ** message: thread 1 restoring `test1`.`t3` part 1 ** message: thread 4 restoring `test1`.`t3` part 3 ** message: thread 2 restoring `test1`.`t3` part 4 ** message: thread 3 restoring `test1`.`t3` part 2 ** message: thread 2 restoring `test1`.`t3` part 5 ** message: thread 3 restoring `test1`.`t3` part 6 ** message: thread 4 restoring `test1`.`t3` part 7 ** message: thread 1 shutting down ** message: thread 4 shutting down ** message: thread 2 shutting down ** message: thread 3 shutting down (root@localhost) [test1] > select count(*) from t3; +----------+ | count(*) | +----------+ | 1000000 | +----------+ 1 row in set (0.00 sec)
由以上结果可见,还原使用到了多线程,提高了还原的效率。
参考
☆〖本人水平有限,文中如有错误还请留言批评指正!〗☆
下一篇: MySQL配置SSL主从复制