mydumper&myloader
介绍
MySQL自身的mysqldump工具支持单线程工作,依次一个个导出多个表,没有一个并行的机制,这就使得它无法迅速的备份数据。
mydumper作为一个实用工具,能够良好支持多线程工作,可以并行的多线程的从表中读入数据并同时写到不同的文件里,这使得它在处理速度方面快于传统的mysqldump。其特征之一是在处理过程中需要对列表加以锁定,因此如果我们需要在工作时段执行备份工作,那么会引起DML阻塞。但一般现在的MySQL都有主从,备份也大部分在从上进行,所以锁的问题可以不用考虑。这样,mydumper能更好的完成备份任务
下载安装
yum -y install cmake glib2 pcre pcre-devel mysql-devel
tar xf mydumper-0.6.2.tar.gz
cd mydumper-0.6.2
cmake .
make && make install
注释:
安装之后生成两个二进制命令/usr/local/bin/mydumper(备份)和/usr/local/bin/myloader(恢复)
mydumper特性
-
多线程备份
-
因为是多线程逻辑备份,备份后会生成多个备份文件
-
备份时对MyISAM表施加FTWRL(FLUSH TABLES WITH READ LOCK),会阻塞DML语句
-
保证备份数据的一致性
-
支持文件压缩
-
支持导出binlog
-
支持多线程恢复
-
支持以守护进程模式工作,定时快照和连续二进制日志
-
支持将备份文件切块
mydumper备份机制

主要步骤概括
-
主线程 FLUSH TABLES WITH READ LOCK, 施加全局只读锁,以阻止DML语句写入,保证数据的一致性
-
读取当前时间点的二进制日志文件名和日志写入的位置并记录在metadata文件中,以供即使点恢复使用
-
N个(线程数可以指定,默认是4)dump线程 START TRANSACTION WITH CONSISTENT SNAPSHOT; 开启读一致的事物
-
dump non-InnoDB tables, 首先导出非事物引擎的表
-
主线程 UNLOCK TABLES 非事物引擎备份完后,释放全局只读锁
-
dump InnoDB tables, 基于事物导出InnoDB表
-
事务结束
备份所生成的文件
-
所有的备份文件在一个目录中,目录可以自己指定
-
目录中包含一个metadata文件
记录了备份数据库在备份时间点的二进制日志文件名,日志的写入位置,
如果是在从库进行备份,还会记录备份时同步至从库的二进制日志文件及写入位置
3. 每个表有两个备份文件:
database.table-schema.sql 表结构文件
database.table.sql 表数据文件
如果对表文件分片,将生成多个备份数据文件,可以指定行数或指定大小分片
mydumper参数详解
-B, --database 要备份的数据库,不指定则备份所有库
-T, --tables-list 需要备份的表,名字用逗号隔开
-o, --outputdir 备份文件输出的目录
-s, --statement-size 生成的insert语句的字节数,默认1000000
-r, --rows 将表按行分块时,指定的块行数,指定这个选项会关闭 --chunk-filesize
-F, --chunk-filesize 将表按大小分块时,指定的块大小,单位是 MB
-c, --compress 压缩输出文件
-e, --build-empty-files 如果表数据是空,还是产生一个空文件(默认无数据则只有表结构文件)
-x, --regex 是同正则表达式匹配 'db.table'
-i, --ignore-engines 忽略的存储引擎,用逗号分割
-m, --no-schemas 不备份表结构
-k, --no-locks 不使用临时共享只读锁,使用这个选项会造成数据不一致
--less-locking 减少对InnoDB表的锁施加时间(减少锁等待的时间)
-l, --long-query-guard 设定阻塞备份的长查询超时时间,单位是秒,默认是60秒(超时后默认mydumper将会退出)
--kill-long-queries 杀掉长查询 (不退出)
-b, --binlogs 导出binlog
-D, --daemon 启用守护进程模式,守护进程模式以某个间隔不间断对数据库进行备份
-I, --snapshot-interval dump快照间隔时间,默认60s,需要在daemon模式下
-L, --logfile 使用的日志文件名(mydumper所产生的日志), 默认使用标准输出
--tz-utc 跨时区是使用的选项,不解释了
--skip-tz-utc 同上
--use-savepoints 使用savepoints来减少采集metadata所造成的锁时间,需要 SUPER 权限
--success-on-1146 Not increment error count and Warning instead of Critical in case of table doesn't exist
-h, --host 连接的主机名
-u, --user 备份所使用的用户
-p, --password 密码
-P, --port 端口
-S, --socket 使用socket通信时的socket文件
-t, --threads 开启的备份线程数,默认是4
-C, --compress-protocol 压缩与mysql通信的数据
-V, --version 显示版本号
-v, --verbose 输出信息模式, 0 = silent, 1 = errors, 2 = warnings, 3 = info, 默认为 2
myloader参数详解
-d, --directory 备份文件的目录
-q, --queries-per-transaction 每次事物执行的查询数量,默认是1000
-o, --overwrite-tables 如果要恢复的表存在,则先drop掉该表,使用该参数,需要备份时候要备份表结构
-B, --database 需要还原的数据库
-e, --enable-binlog 启用还原数据的二进制日志
-h, --host 主机
-u, --user 还原的用户
-p, --password 密码
-P, --port 端口
-S, --socket socket文件
-t, --threads 还原所使用的线程数,默认是4
-C, --compress-protocol 压缩协议
-V, --version 显示版本
-v, --verbose 输出模式, 0 = silent, 1 = errors, 2 = warnings, 3 = info, 默认为2
使用示例
Mydumper
-B, --database Database to dump
-T, --tables-list Comma delimited table list to dump (does not exclude regex option)
-o, --outputdir Directory to output files to
-s, --statement-size Attempted size of INSERT statement in bytes, default 1000000
-r, --rows Try to split tables into chunks of this many rows. This option turns off --chunk-filesize
-F, --chunk-filesize Split tables into chunks of this output file size. This value is in MB
-c, --compress Compress output files
-e, --build-empty-files Build dump files even if no data available from table
-x, --regex Regular expression for 'db.table' matching
-i, --ignore-engines Comma delimited list of storage engines to ignore
-m, --no-schemas Do not dump table schemas with the data
-k, --no-locks Do not execute the temporary shared read lock. WARNING: This will cause inconsistent backups
--less-locking Minimize locking time on InnoDB tables.
-l, --long-query-guard Set long query timer in seconds, default 60
-K, --kill-long-queries Kill long running queries (instead of aborting)
-D, --daemon Enable daemon mode
-I, --snapshot-interval Interval between each dump snapshot (in minutes), requires --daemon, default 60
-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't exist
--lock-all-tables Use LOCK TABLE for all, instead of FTWRL
-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
-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
Myloader
-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
-e, --enable-binlog Enable binary logging of the restore data
-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
-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
1.导出test库下面的table1和table2表结构和数据
/usr/local/bin/mydumper -h 10.1.1.1 -u root -p root -B test1 -T table1,table2 -t 8 -r 100000 -c --less-locking -v 3 -D -L /var/log/mydumper.log -o /geekwolf/test_table1_table2/
-B 数据库
-t 并行备份的线程数
-r 将表按行分块时,指定的块行数
-c 压缩输出文件
-v 版本号
-D 启用守护进程模式
-L 备份日志的文件
-o 备份文件的存放目录
若只导出数据增加-m参数
2.将导出的数据导入到10.1.1.2服务器的test2库中
/usr/local/bin/myloader -h 10.1.1.2 -u root -p root -B test2 -e -t 8 -d /geekwolf/test_table1_table2/ --overwrite-tables -v 3
注释:后台记录导出导入的时间
输入screen命令
然后(time myloader -h 10.1.1.2 -u user -p 'ap' -B mnew_gz -e -t 8 -d /geekwolf/m_members/ --overwrite-tables -v 3) 2>/geekwolf/im.log
最后Ctrl+A+D
三、使用方法
备份出来的sql文件,文件以数据库开头.表名[-schema].sql,有schema的为表的表结构创建语句,没有的为表数据插入语句。
[root@mysql.dmc.com bin]$./mydumper --database=tools --outputdir=/root/mydumper-0.6.2/test/
[root@mysql.dmc.com bin]$cd ot/mydumper-0.6.2/test/
[root@mysql.dmc.com test]$ll -rth
total 112K
-rw-r--r--. 1 root root 972 Jan 2 18:09 tools.django_session.sql
-rw-r--r--. 1 root root 1.1K Jan 2 18:09 tools.t_guid.sql
-rw-r--r--. 1 root root 423 Jan 2 18:09 tools.t_ftpserver-schema.sql
-rw-r--r--. 1 root root 325 Jan 2 18:09 tools.django_session-schema.sql
-rw-r--r--. 1 root root 1.6K Jan 2 18:09 tools.t_serverinfo-schema.sql-rw-r--r--. 1 root root 133 Jan 2 18:09 metadata
-rw-r--r--. 1 root root 133 Jan 2 18:09 metadata
metadata这个文件记录的是当里的binlog文件及pos,可以使用这个信息搭建slave.
[root@mysql.dmc.com test]$cat metadata
Started dump at: 2015-01-02 18:09:40
SHOW MASTER STATUS:
Log: mysql-bin.000006
Pos: 4196075Finished dump at: 2015-01-02 18:09:40
Finished dump at: 2015-01-02 18:09:40
只备份t_task和t_guid表
[root@mysql.dmc.com bin]$./mydumper --database=tools --outputdir=/root/mydumper-0.6.2/test/ --tables-list=t_task,t_guid
只备份以t_server开通的表
[root@mysql.dmc.com bin]$./mydumper --database=tools --outputdir=/root/mydumper-0.6.2/test/ --regex="tools.t_server*"
-B,--database只能指定一个库备份,如果需要同时备份多个数据库,可能这样:
[root@mysql.dmc.com bin]$./mydumper --outputdir=/root/mydumper-0.6.2/test/ --regex="beebol.*|tools.*"
--threads并发度
[root@mysql.dmc.com bin]$./mydumper --outputdir=/root/mydumper-0.6.2/test/
创建时间:2021-12-22 11:55
넶浏览量:0