这篇文章主要介绍了多线程备份工具mydumper怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
安装
点击(此处)折叠或打开
-
依赖安装
-
debian
-
apt-get install libglib2.0-dev
-
apt-get install libmysqlclient-dev
-
apt-get install libghc-zlib-dev
-
apt-get install libpcre++-dev
-
centos
-
yum install glib2-devel mysql-devel zlib-devel pcre-devel
-
# wget https://launchpad.net/ubuntu/+archive/primary/+files/mydumper_0.9.1.orig.tar.gz
-
# tar xzvf mydumper_0.9.1.orig.tar.gz
-
# cd mydumper-0.9.1/
-
# cmake .
-
# make && make
说明
点击(此处)折叠或打开
-
# mydumper –help
-
Usage:
-
mydumper [OPTION…] multi-threaded MySQL dumping
-
Help Options:
-
-?, –help Show help options
-
Application Options:
-
-B, –database (指定要导出的数据库)
-
-T, –tables-list (指定要导出的表,多个表逗号分隔,不支持正则表达式)
-
-o, –outputdir (指定输出目录)
-
-s, –statement-size (导出的insert语句长度指定,单位字节,默认100wziji)
-
-r, –rows (即水平拆分表进行存储,单位行数,这个选项会使–chunk-filesize失效)
-
-F, –chunk-filesize (即水平拆分表进行存储,单位 MB)
-
-c, –compress (压缩输出文件)
-
-e, –build-empty-files (空数据的表也生成文件)
-
-x, –regex (正则表达式 匹配’db.table’)
-
-i, –ignore-engines (设定需要忽略的存储引擎表,多个以逗号分隔)
-
-m, –no-schemas (不要导出schema库中有数据的表)
-
-d, –no-data (仅仅导出表结构,不要数据)
-
-G, –triggers (导出触发器)
-
-E, –events (导出event)
-
-R, –routines (导出存储过程)
-
-k, –no-locks (导出数据过程中不要临时使用读锁). WARNING: This will cause inconsistent backups
-
–less-locking Minimize locking time on InnoDB tables.
-
-l, –long-query-guard (定义超过多久的查询为long querY ,默认60,单位 s)
-
-K, –kill-long-queries (kill掉long query)(instead of aborting)
-
-D, –daemon (使用守护进程的模式)
-
-I, –snapshot-interval (dump快照之间的间隔设置,需要带上–daemon参数启动,默认60,单位minutes)
-
-L, –logfile Log file name to use, by default stdout is used
-
–tz-utc SET TIME_ZONE=’+00:00′ at top of dump to allow dumping of TIMESTAMP data when a server has data in different time zones or data is being moved between servers with different time zones, defaults to on use –skip-tz-utc to disable.
-
–skip-tz-utc
-
–use-savepoints Use savepoints to reduce metadata locking issues, needs SUPER privilege
-
–success-on-1146 Not increment error count and Warning instead of Critical in case of table doesn
-
–lock-all-tables Use LOCK TABLE for all, instead of FTWRL (锁定所有表)
-
-U, –updated-since Use Update_time to dump only tables updated in the last U days
-
–trx-consistency-only Transactional consistency only (事务一致)
-
-t, –threads Number of threads to use, default 4(备份线程数,默认为4)
-
-C, –compress-protocol Use compression on the MySQL connection(连接压缩)
-
-v, –verbose Verbosity of output, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2
默认连接参数
-h, –host The host to connect to
-u, –user Username with privileges to run the dump
-p, –password User password
-P, –port TCP/IP port to connect to
-S, –socket UNIX domain socket file to use for connection
测试
点击(此处)折叠或打开
-
mydumper -u root -p xxx -B db_slave -e -o db_slave_dir
目录文件如下:
其中各文件说明
建库文件 db_slave-schema-create.sql (通用格式dbname-schema-create.sql)
点击(此处)折叠或打开
-
# cat db_slave-schema-create.sql
-
CREATE DATABASE `db_slave` /*!40100 DEFAULT CHARACTER SET latin1 */;
建表文件 db_slave.t_check_flag-schema.sql(通用格式dbname.tbname-schema.sql)
点击(此处)折叠或打开
-
# cat db_slave.t_check_flag-schema.sql
-
/*!40101 SET NAMES binary*/;
-
/*!40014 SET FOREIGN_KEY_CHECKS=0*/;
-
CREATE TABLE `t_check_flag` (
-
`db_ip` char(16) NOT NULL DEFAULT ”,
-
`check_flag` tinyint(4) DEFAULT ‘0’ COMMENT ‘1 monitor 0 not ‘
-
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
数据文件 db_slave.t_check_flag.sql(通用格式dbname.tbname.sql)
点击(此处)折叠或打开
-
# cat db_slave.t_check_flag.sql
-
/*!40101 SET NAMES binary*/;
-
/*!40014 SET FOREIGN_KEY_CHECKS=0*/;
-
/*!40103 SET TIME_ZONE=’+00:00′ */;
-
INSERT INTO `t_check_flag` VALUES
-
(“192.168.111.129”,1),
-
(“192.168.111.130”,1),
-
(“192.168.111.26”,1);
整体导出信息文件 metadata(这里记录了开始时间,结束时间,以及导出开始时的binlog信息,对于建立从库比较方便)
点击(此处)折叠或打开
-
# cat metadata
-
Started dump at: 2016-11-19 13:38:56
-
SHOW MASTER STATUS:
-
Log: 2104.014976
-
Pos: 85965870
-
GTID:(null)
-
Finished dump at: 2016-11-19 13:38:56
过程描述
打开general_log可以看到如下日志
点击(此处)折叠或打开
-
148901 Connect root@localhost on db_slave
-
148901 Query SET SESSION wait_timeout = 2147483
-
148901 Query SET SESSION net_write_timeout = 2147483
-
148901 Query SHOW PROCESSLIST
-
148901 Query FLUSH TABLES WITH READ LOCK
-
148901 Query SELECT @@tokudb_version
-
148901 Query START TRANSACTION /*!40108 WITH CONSISTENT SNAPSHOT */
-
148901 Query /*!40101 SET NAMES binary*/
-
148901 Query SHOW MASTER STATUS
-
148901 Query SELECT @@gtid_current_pos
-
148901 Query SELECT @@default_master_connection
-
148901 Query SHOW SLAVE STATUS
-
148902 Connect root@localhost on
-
148902 Query SET SESSION wait_timeout = 2147483
-
148902 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
-
148902 Query START TRANSACTION /*!40108 WITH CONSISTENT SNAPSHOT */
-
148902 Query /*!40103 SET TIME_ZONE=’+00:00′ */
-
148902 Query /*!40101 SET NAMES binary*/
-
148903 Connect root@localhost on
-
148903 Query SET SESSION wait_timeout = 2147483
-
148903 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
-
148903 Query START TRANSACTION /*!40108 WITH CONSISTENT SNAPSHOT */
-
148903 Query /*!40103 SET TIME_ZONE=’+00:00′ */
-
148903 Query /*!40101 SET NAMES binary*/
-
148904 Connect root@localhost on
-
148904 Query SET SESSION wait_timeout = 2147483
-
148904 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
-
148904 Query START TRANSACTION /*!40108 WITH CONSISTENT SNAPSHOT */
-
148904 Query /*!40103 SET TIME_ZONE=’+00:00′ */
-
148904 Query /*!40101 SET NAMES binary*/
-
148905 Connect root@localhost on
-
148905 Query SET SESSION wait_timeout = 2147483
-
148905 Query SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ
-
148905 Query START TRANSACTION /*!40108 WITH CONSISTENT SNAPSHOT */
-
148905 Query /*!40103 SET TIME_ZONE=’+00:00′ */
-
148905 Query /*!40101 SET NAMES binary*/
-
148901 Init DB db_slave
-
148901 Query SHOW TABLE STATUS
-
148901 Query SHOW CREATE DATABASE `db_slave`
-
148901 Query UNLOCK TABLES /* FTWRL */
-
148902 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `db_slave`.`t_check_flag`
-
148903 Query SELECT /*!40001 SQL_NO_CACHE */ * FROM `db_slave`.`t_master_instance`
-
148901 Quit
-
148904 Query SHOW CREATE TABLE `db_slave`.`t_check_flag`
-
148905 Query SHOW CREATE TABLE `db_slave`.`t_master_instance`
①连接数据库
②判断长查询相关,–long-query-guard和–kill-long-queries
③flush tables with read locks ,备份mysiam表完成会立即unlock tables; 对于innodb表,mydumper·使用single transaction的方式进行备份。因此mysiam表的大小及数量影响着mysiam的备份时间进而影响着业务是否只读。
④通过 -t 参数指定的线程数量进行创建,并进行工作worker子线程
⑤确定当前要导出的表,并把待导出表加入到队列中。
⑥备份完成退出(其中mysiam完成会unlock tables;)
总结:
①可以使用 -t 参数指定线程数,多线程同时备份速度可以提升,但是也依赖于磁盘的IO性能
②导出过程要加读锁,建议在业务低峰期进行。
③在从库备份的时候,metadata中也是记录当前从库已执行到的binlog文件以及位置
④定时任务备份可使用 -v 参数输出相应等级的信息,作为备份日志。
⑤当库过于庞大的时候,建议使用 -c 参数对结果进行压缩。
⑥单表过于庞大的时候,可以使用 -r 或者 -F 参数分解,避免单表备份时间过长(不拆的话就是 one thread 进行导出)
感谢你能够认真阅读完这篇文章,希望小编分享的“多线程备份工具mydumper怎么用”这篇文章对大家有帮助,同时也希望大家多多支持云搜网,关注云搜网行业资讯频道,更多相关知识等着你来学习!