binlog2sql使用

来源:互联网 发布:录音制作手机软件知乎 编辑:程序博客网 时间:2024/05/16 00:34

安装

shell> git clone https://github.com/danfengcao/binlog2sql.git && cd binlog2sqlshell> pip install -r requirements.txt

必备参数

[mysqld]server_id = 1 #需要设置serveridlog_bin = /var/log/mysql/mysql-bin.logmax_binlog_size = 1Gbinlog_format = rowbinlog_row_image = full #默认就是full

权限

select, super/replication client, replication slave建议授权GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 

权限说明

  • select:需要读取server端information_schema.COLUMNS表,获取表结构的元信息,拼接成可视化的sql语句
  • super/replication client:两个权限都可以,需要执行’SHOW MASTER STATUS’, 获取server端的binlog列表
  • replication slave:通过BINLOG_DUMP协议获取binlog内容的权限

解析出标准SQL

python binlog2sql.py -h172.16.120.131 -P3306 -umysql -pmysql -dfandb -t dept1 --start-file='mysql-bin.000004' --start-position='1719' --stop-position='2001'DELETE FROM `fandb`.`dept1` WHERE `dname`='ACCOUNTING' AND `loc`='NEW YORK' AND `deptno`=10 LIMIT 1; #start 1719 end 1970 time 2017-06-23 17:34:35DELETE FROM `fandb`.`dept1` WHERE `dname`='RESEARCH' AND `loc`='DALLAS' AND `deptno`=20 LIMIT 1; #start 1719 end 1970 time 2017-06-23 17:34:35DELETE FROM `fandb`.`dept1` WHERE `dname`='SALES' AND `loc`='CHICAGO' AND `deptno`=30 LIMIT 1; #start 1719 end 1970 time 2017-06-23 17:34:35DELETE FROM `fandb`.`dept1` WHERE `dname`='OPERATIONS' AND `loc`='BOSTON' AND `deptno`=40 LIMIT 1; #start 1719 end 1970 time 2017-06-23 17:34:35

解析出回滚SQL

python binlog2sql.py --flashback -h172.16.120.131 -P3306 -umysql -pmysql -dfandb -t dept1 --start-file='mysql-bin.000004' --start-position='1719' --stop-position='2001'INSERT INTO `fandb`.`dept1`(`dname`, `loc`, `deptno`) VALUES ('OPERATIONS', 'BOSTON', 40); #start 1719 end 1970 time 2017-06-23 17:34:35INSERT INTO `fandb`.`dept1`(`dname`, `loc`, `deptno`) VALUES ('SALES', 'CHICAGO', 30); #start 1719 end 1970 time 2017-06-23 17:34:35INSERT INTO `fandb`.`dept1`(`dname`, `loc`, `deptno`) VALUES ('RESEARCH', 'DALLAS', 20); #start 1719 end 1970 time 2017-06-23 17:34:35INSERT INTO `fandb`.`dept1`(`dname`, `loc`, `deptno`) VALUES ('ACCOUNTING', 'NEW YORK', 10); #start 1719 end 1970 time 2017-06-23 17:34:35

选项

--stop-never如果不指定stop-never,那么在解析完-start-file指定的binlog后就会停止break否则会解析到最后一个binlog.-K, --no-primary-key 对INSERT语句去除主键。可选。-B, --flashback 生成回滚语句,可解析大文件,不受内存限制,每打印一千行加一句SLEEP SELECT(1)。可选。与stop-never或no-primary-key不能同时添加。解析范围控制--start-file 起始解析文件。必须。--start-position/--start-pos start-file的起始解析位置。可选。默认为start-file的起始位置。--stop-file/--end-file 末尾解析文件。可选。默认为start-file同一个文件。若解析模式为stop-never,此选项失效。--stop-position/--end-pos stop-file的末尾解析位置。可选。默认为stop-file的最末位置;若解析模式为stop-never,此选项失效。--start-datetime 从哪个时间点的binlog开始解析,格式必须为datetime,如'2016-11-11 11:11:11'。可选。默认不过滤。--stop-datetime 到哪个时间点的binlog停止解析,格式必须为datetime,如'2016-11-11 11:11:11'。可选。默认不过滤。对象过滤-d, --databases 只输出目标db的sql。可选。默认为空。-t, --tables 只输出目标tables的sql。可选。默认为空。

https://github.com/danfengcao/binlog2sql

原创粉丝点击