1)插入测试数据
[root@localhost:mytest1]>select * from user;
+----+--------+------------+
| id | name | addtime |
+----+--------+------------+
| 1 | 小赵 | 2013-11-11 |
| 2 | 小钱 | 2014-11-11 |
| 3 | 小孙 | 2016-11-11 |
| 4 | 小李 | 2013-11-11 |
+----+--------+------------+
4 rows in set (0.00 sec)
删除两行数据后:
mysql> select * from user;
+----+--------+------------+
| id | name | addtime |
+----+--------+------------+
| 1 | 小赵 | 2013-11-11 |
| 4 | 小李 | 2013-11-11 |
+----+--------+------------+
2 rows in set (0.00 sec)
记录binlog的位置
[root@localhost:mytest1]>show master status;
+------------------+----------+--------------+------------------+--------------------------------------------------------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+--------------------------------------------------------------------------------------------------+
| mysql-bin.000026 | 2463 |
2)安装binlog2sql
使用mysql用户解压binlog2sql.zip 到/home/mysql目录
unzip binlog2sql.zip
cd binlog2sql
使用root安装binlog2sql.zip里自带的pymysql等三个包,否则会报ImportError: No module named pymysql等错误
[root@t3-dtpoc-dtpoc-web04 binlog2sql]# ls
binlog2sql-master mysql-replication-0.13 PyMySQL-0.7.11 wheel-0.29.0
cd PyMySQL-0.7.11
python setup.py install
cd mysql-replication-0.13
python setup.py install
cd wheel-0.29.0
python setup.py install
cd binlog2sql-master
cd binlog2sql
3)使用root执行binlog2sql
python binlog2sql.py -h127.0.0.1 -P3306 -uroot -p'1234' -dmytest -tuser --start-file='mysql-bin.000026' --start-datetime='2023-09-18 14:00:00' --stop-datetime='2023-09-18 14:25:00'> /tmp/raw.sql
可以看到删除表user数据的sql
vi /tmp/raw.sql
USE mytest;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`addtime` date,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `mytest`.`user`(`addtime`, `id`, `name`) VALUES ('2013-11-11', 1, '小赵'); #start 9463 end 9721 time 2023-09-18 14:17:24
INSERT INTO `mytest`.`user`(`addtime`, `id`, `name`) VALUES ('2014-11-11', 2, '小钱'); #start 9817 end 10075 time 2023-09-18 14:18:04
INSERT INTO `mytest`.`user`(`addtime`, `id`, `name`) VALUES ('2016-11-11', 3, '小孙'); #start 10171 end 10429 time 2023-09-18 14:18:31
INSERT INTO `mytest`.`user`(`addtime`, `id`, `name`) VALUES ('2013-11-11', 4, '小李'); #start 10525 end 10783 time 2023-09-18 14:18:58
DELETE FROM `mytest`.`user` WHERE `addtime`='2014-11-11' AND `id`=2 AND `name`='小钱' LIMIT 1; #start 10879 end 11138 time 2023-09-18 14:19:38
DELETE FROM `mytest`.`user` WHERE `addtime`='2016-11-11' AND `id`=3 AND `name`='小孙' LIMIT 1; #start 10879 end 11138 time 2023-09-18 14:19:38
4. )根据位置信息,我们确定了误操作sql来自同一个事务,准确位置在10879-11138之间(binlog2sql对于同一个事务会输出同样的start position)。再根据位置过滤,使用 **-B** 选项生成回滚sql,检查回滚sql是否正确。(注:真实场景下,生成的回滚SQL经常会需要进一步筛选。结合grep、编辑器等)
DELETE FROM `mytest`.`user` WHERE `addtime`='2014-11-11' AND `id`=2 AND `name`='小钱' LIMIT 1; #start 10879 end 11138 time 2023-09-18 14:19:38
DELETE FROM `mytest`.`user` WHERE `addtime`='2016-11-11' AND `id`=3 AND `name`='小孙' LIMIT 1; #start 10879 end 11138 time 2023-09-18 14:19:38
python binlog2sql.py -h127.0.0.1 -P3306 -uroot -p'1234' -dmytest -tuser --start-file='mysql-bin.000026' --start-position=10879 --stop-position=11138 -B > /tmp/rollback.sql
vi rollback.sql
INSERT INTO `mytest`.`user`(`addtime`, `id`, `name`) VALUES ('2016-11-11', 3, '小孙'); #start 10879 end 11138 time 2023-09-18 14:19:38
INSERT INTO `mytest`.`user`(`addtime`, `id`, `name`) VALUES ('2014-11-11', 2, '小钱'); #start 10879 end 11138 time 2023-09-18 14:19:38
5)与业务方确认回滚sql没问题,执行回滚语句。登录mysql,确认回滚成功。
mysql> source /tmp/rollback.sql;
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql> select * from mytest.user;
+----+--------+------------+
| id | name | addtime |
+----+--------+------------+
| 1 | 小赵 | 2013-11-11 |
| 2 | 小钱 | 2014-11-11 |
| 3 | 小孙 | 2016-11-11 |
| 4 | 小李 | 2013-11-11 |
+----+--------+------------+
4 rows in set (0.00 sec)
mysql>