mysql小白系列_06 备份与恢复 - mydumper与myloader
http://blog.csdn.net/leshami/article/details/46815553
https://www.cnblogs.com/zhoujinyi/p/3423641.html
编译安装mydumper
https://launchpad.net/mydumper
https://github.com/maxbube/mydumper
wget https://launchpad.net/mydumper/0.9/0.9.1/+download/mydumper-0.9.1.tar.gz
cmake .
make
make install
在/usr/local/bin下面产生2个文件:mydumper和myloader
备份结果集
- 所有备份在一个目录,不指定目录则以日期事件命名:export-20180223-102900
- 目录中有metadata文件,记录备份时的主库binlog文件和位置
[root@docker02 mydumper]# cat metadata
Started dump at: 2018-02-23 11:56:52
SHOW MASTER STATUS:
Log: binlog.000009
Pos: 403849416
GTID:
Finished dump at: 2018-02-23 11:56:52
- 备份包含建库sql,建表sql,表内容sql
mydumper工作过程
- 连接目标数据库
- 通过show processlist判断是否有长查询,根据long-query-guard和kill-long-queries来决定是否退出或者杀掉长查询
- 锁定myisam表,FTWRL
- 针对innodb引擎表开启事务,start transaction
- 创建worker子线程
- 确定候选表,根据类别分别插入innodb_table,non_innodb_table,table_schemas链表(表结构)
- 将候选表通过g_async_queue_push加入任务队列,有worker子线程从队列中读取表信息并执行数据导出
- 执行unlock tables,处理完myisam表后立即解锁,以减少锁定时间
- 等待worker退出
mydumper命令选项
[root@docker02 mydumper-0.9.1]# mydumper --help
Usage:
mydumper [OPTION...] multi-threaded MySQL dumping
Help Options:
-?, --help Show help options
Application Options:
-B, --database Database to dump
指定备份的数据库,不指定则备份所有数据库,包含mysql,但是不含performance_schema
-T, --tables-list Comma delimited table list to dump (does not exclude regex option)
指定备份的表,多个表以逗号隔开
-o, --outputdir Directory to output files to
指定目录输出为止,不指定则在当前路径生成目录:export-20180223-102900
-s, --statement-size Attempted size of INSERT statement in bytes, default 1000000
指定生成insert语句的字节数,默认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
不备份表结构,只备份数据
-d, --no-data Do not dump table data
不备份表数据,只备份表结构
-G, --triggers Dump triggers
备份触发器
-E, --events Dump events
备份事件
-R, --routines Dump stored procedures and functions
备份存储过程和函数
-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
设置最长查询时间,默认60s,防止锁
-K, --kill-long-queries Kill long running queries (instead of aborting)
kill掉长查询
-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
-U, --updated-since Use Update_time to dump only tables updated in the last U days
--trx-consistency-only Transactional consistency only
-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
默认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
常用mydumper备份命令
备份所有数据库
mydumper --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --outputdir=/backup/mydumper --compress --verbose=3 --logfile=/backup/mydumper/mydumper.log
备份指定库指定表 -b
mydumper --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex '^(?!(mysql))' -B db1 -T t1,t2 --outputdir=/backup/mydumper --compress --verbose=3 --logfile=/backup/mydumper/mydumper.log
不带表结构备份表-只有数据,只有dbname.tabname.sql -m
mydumper --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex '^(?!(mysql))' -B db1 -m --outputdir=/backup/mydumper --verbose=3 --logfile=/backup/mydumper/mydumper.log
不带表数据备份表-只有表结构,只有dbname-schema-create.sql和dbname.tabname-schema.sql -d
mydumper --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex '^(?!(mysql))' -B db1 -d --outputdir=/backup/mydumper --verbose=3 --logfile=/backup/mydumper/mydumper.log
远程压缩及使用压缩协议 -c
mydumper --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex '^(?!(mysql))' -B db1 -d --outputdir=/backup/mydumper -c -C tar --verbose=3 --logfile=/backup/mydumper/mydumper.log
匹配多张表备份
mydumper --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex '^(?!(mysql))' -B db1 --regex=db* --outputdir=/backup/mydumper --verbose=3 --logfile=/backup/mydumper/mydumper.log
过滤多个库
mydumper --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex '^(?!(mysql|db2))' --outputdir=/backup/mydumper --verbose=3 --logfile=/backup/mydumper/mydumper.log
即使是空表,也备份结构 -e
mydumper --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex '^(?!(mysql))' -B db1 -e --outputdir=/backup/mydumper --verbose=3 --logfile=/backup/mydumper/mydumper.log
-rw-r--r--. 1 root root 0 Feb 23 11:48 db1.t3.sql
myloader恢复
myloader命令参数
[root@docker02 mydumper]# 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 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
单库备份与恢复
- 备份一个库
mydumper --user=root --password='123456' --host=172.16.2.154 --socket=/data/my3306/run/mysql.sock -B db1 --outputdir=/backup/mydumper --compress -C --verbose=3 --logfile=/backup/mydumper/mydumper.log
- 恢复一个库到原来的实例
myloader --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --directory=/backup/mydumper --verbose=3 -s db1 -B db11
[root@docker02 mydumper]# myloader --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --directory=/backup/mydumper --verbose=3 -B db1 -o
** Message: 4 threads created
** Message: Dropping table or view (if exists) `db1`.`t1`
** Message: Creating table `db1`.`t1`
** Message: Dropping table or view (if exists) `db1`.`t2`
** Message: Creating table `db1`.`t2`
** Message: Dropping table or view (if exists) `db1`.`t3`
** Message: Creating table `db1`.`t3`
** Message: Thread 2 restoring `db1`.`t2` part 0
** Message: Thread 1 restoring `db1`.`t1` part 0
** Message: Thread 4 shutting down
** Message: Thread 3 shutting down
** Message: Thread 2 shutting down
** Message: Thread 1 shutting down
- 恢复一个库到其他实例
myloader --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --directory=/backup/mydumper --verbose=3 -B db11
从全备恢复一个库
- 备份全库
mydumper --user=root --password='123456' --host=172.16.2.154 --socket=/data/my3306/run/mysql.sock --outputdir=/backup/mydumper --compress -C --verbose=3 --logfile=/backup/mydumper/mydumper.log
- 恢复指定库
myloader --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --directory=/backup/mydumper --verbose=3 -s db3
- 恢复指定库并改名
myloader --user=root --password='123456' --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --directory=/backup/mydumper --verbose=3 -s db3 -B db33
以上两条如果同时导入同一个实例,后导入的库会报错
mysql> select * from t1;
ERROR 1412 (HY000): Table definition has changed, please retry transaction
从全备恢复一张表
mysql> drop table t1;
Query OK, 0 rows affected (0.02 sec)
mysql> source /backup/mydumper/db3.t1-schema.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.05 sec)
mysql> source /backup/mydumper/db3.t1.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+------+-------+-------+
| id | name1 | name2 |
+------+-------+-------+
| 1 | yzw1 | yzw11 |
| 2 | yzw2 | yzw22 |
| 3 | yzw3 | yzw33 |
| 4 | yzw4 | yzw44 |
+------+-------+-------+
4 rows in set (0.00 sec)
mysql小白系列_06 备份与恢复 - mydumper与myloader的更多相关文章
- mysql小白系列_06 备份与恢复
1.使用mydumper工具全库备份. 1)源码编译安装 2)全库备份 2.误操作truncate table gyj_t1;利用mysqldump的备份和binlog日志对表gyj_t1做完全恢复. ...
- mysql小白系列_14 线上故障分析与排错
1.重现故障5---线上执行update报错,并处理.(表结构和UPDATE语句自己构造,请给出详细步骤) 1)update故障出现ERROR 1206 (HY000): The total numb ...
- mysql小白系列_13 Online DDL
Online DDL: 一.FIC之前的添加删除二级索引: 1.首先创建临时表,定义目标新表的选项和索引 2.逐行拷贝数据到临时表 3.插入行时更新索引信息 4.数据全部被拷贝到新表后,删除旧表,re ...
- mysql小白系列_12 sysbench
压测 1.查看机器负载load top - load average 1m 5m 15m cat /proc/loadavg 与CPU个数有关,1个load表示1个CPUcat /proc/cpuin ...
- mysql小白系列_11 MHA
一.MHA是什么?能干什么的 (1)以Perl语言写的一套Mysql故障切换方案,一个脚本管理工具 (2)保障数据库的高可用性 (3)修复多个slave之间的差异日志,最终使所有的slave保持数据一 ...
- mysql小白系列_10 mysql主从复制原理
1.如何解决主从复制延迟的问题? (1)主从复制原理 http://www.cnblogs.com/jenvid/p/8410922.html 1.salve执行slave start,salve服务 ...
- mysql小白系列_09 mysql性能优化关键点
一 服务器参数调优,有哪些关键点? 1. 应用访问优化 优化方法 性能提升效果 优化成本 说明 减少数据访问能不访问就不访问-减少磁盘IO 1~1000 低 缓存服务器缓存mysql数据,Redis. ...
- mysql小白系列_08 zabbix添加自定义监控项items和触发器
监控mysql存活 1.配置agent自定义参数 vi /usr/local/zabbix/etc/zabbix_agentd.conf Include=/usr/local/zabbix/etc/z ...
- mysql小白系列_08 zabbix3.2.6概念及部署
一 zabbix功能简介 1.zabbix三大监控组件 zabbix server web gui database zabbix_server zabbix proxy agent client 2 ...
随机推荐
- java中使用Semaphore构建阻塞对象池
java中使用Semaphore构建阻塞对象池 Semaphore是java 5中引入的概念,叫做计数信号量.主要用来控制同时访问某个特定资源的访问数量或者执行某个操作的数量. Semaphore中定 ...
- jax-rs下载文件
@Path("/file") public class FileService { private static final String FILE_PATH = "c: ...
- 多方法解决设置width:100%再设置margin或padding溢出的问题
2019独角兽企业重金招聘Python工程师标准>>> 当设置了父元素的宽度,子元素设置宽度为100%后再在加上子元素上添加padding或margin值就会溢出.举个例子: < ...
- HDU-1857 畅通工程再续
畅通工程再续 Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定 ...
- 日常开发中常用的linux命令
本文并不将linux的常用命令全部罗列出来,列出一下常用.容易忘记的命令. 更详细的说明见:https://www.cnblogs.com/xuxinstyle/p/9609551.html 文件相关 ...
- JS中由闭包引发内存泄露的深思
目录 一个存在内存泄露的闭包实例 什么是内存泄露 JS的垃圾回收机制 什么是闭包 什么原因导致了内存泄露 参考 1.一个存在内存泄露的闭包实例 var theThing = null; var rep ...
- Spring Framework 之AOP
Spring Framework 之AOP 目录 Spring Framework 之AOP 问题 AOP概述 AOP知识 1.连接点(Joinpoint) 2.切点(PointCut) 3.增强(A ...
- Python第三方库之Numpy库
概述 Numpy 最基本的库,是用于处理含有同种元素的多维数组运算的第三方库 —科学计算包,python数据分析及科学计算的基础库,几乎支撑所有其他库 —支持N维数组运算.处理大型矩阵.成熟的广播函 ...
- System.Linq.Dynamic字符串转委托
以前一直想着有没有一个方法能够把字符串直接转化成函数的,刚好有需求就找了下,还真有. 微软地址:https://docs.microsoft.com/en-us/previous-versions/b ...
- C:简单实现BaseCode64编码
What is Base64? 前言 目前来看遇到过Base 16.Base 32.Base 64的编解码,这种编码格式是二进制和文本编码转化,是对称并且可逆的转化.Base 64总共有64个ASCI ...