看腻文章了就来听听视频演示吧:https://www.bilibili.com/video/BV1Zj411k7VW/
binlog2sql是美团大众点评开源的一款用于解析binlog的工具。可用于提取操作的SQL及生成回滚SQL。
github项目:
https://github.com/danfengcao/binlog2sql
github打不开可去gitee下载:
https://gitee.com/damned_gentleness/binlog2sql/tree/master/
unzip binlog2sql-master.zip
cd binlog2sql-master/
# 需要安装的Python依赖
[root@db01 binlog2sql-master]# cat requirements.txt
PyMySQL==0.7.11
wheel==0.29.0
mysql-replication==0.13
# 指定使用阿里云的镜像(能连网的方式)
pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
MySQL server必须设置以下参数:
[mysqld]
server_id = 1
log_bin = /var/log/mysql/mysql-bin.log
max_binlog_size = 1G
binlog_format = row
binlog_row_image = full
最小权限集合:
-- 授权语句
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO xxoo;
由于是伪装成slave来获取主的二进制事件,故无需对binlog有可读权限。
先切进python脚步文件(binlog2sql.py)所在目录
[root@dba binlog2sql-master]# cd binlog2sql
[root@dba binlog2sql]# ll
total 36
-rwxr-xr-x 1 root root 7747 Oct 12 2018 binlog2sql.py
-rwxr-xr-x 1 root root 11581 Oct 12 2018 binlog2sql_util.py
-rw-r--r-- 1 root root 92 Oct 12 2018 __init__.py
解析出标准SQL
[root@dba binlog2sql]# python binlog2sql.py -h127.0.0.1 -P3306 -uroot -proot -dmdb -t t_student --start-file='mysql-bin.000011'
USE mdb;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `t_view` AS select * from heartbeat;
USE mdb;
create table test2 (id int,name text);
USE mdb;
DROP TABLE `test2` /* generated by server */;
USE db_test;
create table tblpky(id int primary key auto_increment,name text);
USE mdb;
create table t_student(id int,name varchar(18),class int,score varchar(18));
INSERT INTO `mdb`.`t_student`(`class`, `score`, `id`, `name`) VALUES (1, '66', 1, 'a'); #start 2418 end 2638 time 2023-02-23 02:22:10
INSERT INTO `mdb`.`t_student`(`class`, `score`, `id`, `name`) VALUES (1, '58', 2, 'b'); #start 2418 end 2638 time 2023-02-23 02:22:10
INSERT INTO `mdb`.`t_student`(`class`, `score`, `id`, `name`) VALUES (2, '86', 3, 'c'); #start 2418 end 2638 time 2023-02-23 02:22:10
INSERT INTO `mdb`.`t_student`(`class`, `score`, `id`, `name`) VALUES (2, '78', 4, 'd'); #start 2418 end 2638 time 2023-02-23 02:22:10
UPDATE `mdb`.`t_student` SET `class`=2, `score`='89', `id`=3, `name`='c' WHERE `class`=2 AND `score`='86' AND `id`=3 AND `name`='c' LIMIT 1; #start 2734 end 2927 time 2023-02-23 02:28:38
DELETE FROM `mdb`.`t_student` WHERE `class`=1 AND `score`='58' AND `id`=2 AND `name`='b' LIMIT 1; #start 3023 end 3201 time 2023-02-23 02:28:55
INSERT INTO `mdb`.`t_student`(`class`, `score`, `id`, `name`) VALUES (1, '48', 5, 'e'); #start 3297 end 3475 time 2023-02-23 02:29:32
参数选项
python binlog2sql.py --help
解析模式:
--stop-never 持续解析binlog。可选。默认False,同步至执行命令时最新的binlog位置。
-K, --no-primary-key 对INSERT语句去除主键。可选。默认False
-B, --flashback 生成回滚SQL,可解析大文件,不受内存限制。可选。默认False。与stop-never或no-primary-key不能同时添加。
--back-interval -B模式下,每打印一千行回滚SQL,加一句SLEEP多少秒,如不想加SLEEP,请设为0。可选。默认1.0。
解析范围控制:
--start-file 起始解析文件,只需文件名,无需全路径 。必须。
--start-position/--start-pos 起始解析位置。可选。默认为start-file的起始位置。
--stop-file/--end-file 终止解析文件。可选。默认为start-file同一个文件。若解析模式为stop-never,此选项失效。
--stop-position/--end-pos 终止解析位置。可选。默认为stop-file的最末位置;若解析模式为stop-never,此选项失效。
--start-datetime 起始解析时间,格式'%Y-%m-%d %H:%M:%S'。可选。默认不过滤。
--stop-datetime 终止解析时间,格式'%Y-%m-%d %H:%M:%S'。可选。默认不过滤。
对象过滤:
-d, --databases 只解析目标db的sql,多个库用空格隔开,如-d db1 db2。可选。默认为空。
-t, --tables 只解析目标table的sql,多张表用空格隔开,如-t tbl1 tbl2。可选。默认为空。
--only-dml 只解析dml,忽略ddl。可选。默认False。
--sql-type 只解析指定类型,支持INSERT, UPDATE, DELETE。多个类型用空格隔开,如--sql-type INSERT DELETE。可选。默认为增删改都解析。用了此参数但没填任何类型,则三者都不解析。
忘带where条件的误UPDATE整张表
mysql> select * from t_student;
+------+------+-------+-------+
| id | name | class | score |
+------+------+-------+-------+
| 1 | a | 1 | 66 |
| 3 | c | 2 | 89 |
| 4 | d | 2 | 78 |
| 5 | e | 1 | 48 |
+------+------+-------+-------+
4 rows in set (0.00 sec)
mysql> update t_student set score='failure';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from t_student;
+------+------+-------+---------+
| id | name | class | score |
+------+------+-------+---------+
| 1 | a | 1 | failure |
| 3 | c | 2 | failure |
| 4 | d | 2 | failure |
| 5 | e | 1 | failure |
+------+------+-------+---------+
4 rows in set (0.00 sec)
找到误操作记录的binlog文件
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000011
Position: 3899
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 0ee6241a-f240-11ec-9388-080027be95b2:1-169719
1 row in set (0.00 sec)
根据误操作人提供的大致误操作时间过滤数据
[root@dba binlog2sql]# python binlog2sql.py -h127.0.0.1 -P3306 -uroot -p'root' -dmdb -tt_student --start-file='mysql-bin.000011' --start-datetime='2023-02-23 02:36:17' --stop-datetime='2023-02-23 02:38:17'
UPDATE `mdb`.`t_student` SET `class`=1, `score`='failure', `id`=1, `name`='a' WHERE `class`=1 AND `score`='66' AND `id`=1 AND `name`='a' LIMIT 1; #start 3571 end 3868 time 2023-02-23 02:36:27
UPDATE `mdb`.`t_student` SET `class`=2, `score`='failure', `id`=3, `name`='c' WHERE `class`=2 AND `score`='89' AND `id`=3 AND `name`='c' LIMIT 1; #start 3571 end 3868 time 2023-02-23 02:36:27
UPDATE `mdb`.`t_student` SET `class`=2, `score`='failure', `id`=4, `name`='d' WHERE `class`=2 AND `score`='78' AND `id`=4 AND `name`='d' LIMIT 1; #start 3571 end 3868 time 2023-02-23 02:36:27
UPDATE `mdb`.`t_student` SET `class`=1, `score`='failure', `id`=5, `name`='e' WHERE `class`=1 AND `score`='48' AND `id`=5 AND `name`='e' LIMIT 1; #start 3571 end 3868 time 2023-02-23 02:36:27
可以知道误操作的位置点在3571-3868之间和时间点,再用flashback模式( -B )生成回滚sql,检查回滚sql是否正确
[root@dba binlog2sql]# python binlog2sql.py -h127.0.0.1 -P3306 -uroot -p'root' -dmdb -tt_student --start-file='mysql-bin.000011' --start-datetime='2023-02-23 02:36:17' --stop-datetime='2023-02-23 02:38:17' -B > tb_student_rb.sql
[root@dba binlog2sql]# cat tb_student_rb.sql
UPDATE `mdb`.`t_student` SET `class`=1, `score`='48', `id`=5, `name`='e' WHERE `class`=1 AND `score`='failure' AND `id`=5 AND `name`='e' LIMIT 1; #start 3571 end 3868 time 2023-02-23 02:36:27
UPDATE `mdb`.`t_student` SET `class`=2, `score`='78', `id`=4, `name`='d' WHERE `class`=2 AND `score`='failure' AND `id`=4 AND `name`='d' LIMIT 1; #start 3571 end 3868 time 2023-02-23 02:36:27
UPDATE `mdb`.`t_student` SET `class`=2, `score`='89', `id`=3, `name`='c' WHERE `class`=2 AND `score`='failure' AND `id`=3 AND `name`='c' LIMIT 1; #start 3571 end 3868 time 2023-02-23 02:36:27
UPDATE `mdb`.`t_student` SET `class`=1, `score`='66', `id`=1, `name`='a' WHERE `class`=1 AND `score`='failure' AND `id`=1 AND `name`='a' LIMIT 1; #start 3571 end 3868 time 2023-02-23 02:36:27
确认回滚sql语句无误并回滚。登录mysql确认,检查数据回滚成功。
mysql> select * from t_student;
+------+------+-------+---------+
| id | name | class | score |
+------+------+-------+---------+
| 1 | a | 1 | failure |
| 3 | c | 2 | failure |
| 4 | d | 2 | failure |
| 5 | e | 1 | failure |
+------+------+-------+---------+
4 rows in set (0.00 sec)
mysql> source /root/binlog2sql-master/binlog2sql/tb_student_rb.sql
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t_student;
+------+------+-------+-------+
| id | name | class | score |
+------+------+-------+-------+
| 1 | a | 1 | 66 |
| 3 | c | 2 | 89 |
| 4 | d | 2 | 78 |
| 5 | e | 1 | 48 |
+------+------+-------+-------+
4 rows in set (0.00 sec)
限制:
优点:
参考链接:https://www.cnblogs.com/ivictor/p/6418409.html