本文主要是介绍mysql binlog 闪回_MySQL误操作之快速闪回binlog2sql,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目作者:danfengcao
项目地址:https://github.com/danfengcao/binlog2sql
1、环境限制
mysql server必须开启,离线模式下不能解析binlog
binlog格式必须是行模式
软件版本:Python 2.7 MySQL 5.6
MySQL设置一下参数:
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
max_binlog_size = 1000M
binlog-format = row
2、构建数据
建库
root@localhost [mysql]>create databasetest;
Query OK, 1 row affected (0.10 sec)
建表
root@localhost [mysql]>create tabletest01( id int(4) not null auto_increment, name varchar(3), age int(4),primarykey(id))engine=innodb charset=utf8;
Query OK, 0 rows affected (0.13 sec)
插入测试数据
insert into test01(id,name,age)values(1,'tom',25);
insert into test01(id,name,age) values(2,'小丫',23);
insert into test01(id,name,age) values(3,'飞飞',28);
insert into test01(id,name,age) values(4,'参参',16);
insert into test01(id,name,age) values(5,'顺子',330);
insert into test01(id,name,age) values(6,'炸弹',48);
检查测试数据
root@localhost [mysql]>select * fromtest01;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 |tom | 25 |
| 2 | 小丫 | 23|
| 3 | 飞飞 | 28|
| 4 | 参参 | 16|
| 5 | 顺子 | 330|
| 6 | 炸弹 | 48|
+----+--------+------+
6 rows in set (0.00 sec)
3、安装binlog2sql
git clone https://github.com/danfengcao/binlog2sql.git
pip install -r requirements.txt
4、模拟删除数据恢复
删除数据
root@localhost [test]>select * fromtest01;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 |tom | 25 |
| 2 | 小丫 | 23|
| 3 | 飞飞 | 28|
| 4 | 参参 | 16|
| 5 | 顺子 | 330|
| 6 | 炸弹 | 48|
+----+--------+------+
6 rows in set (0.14 sec)
root@localhost [test]>delete from test01;
Query OK, 6 rows affected (0.09 sec)
root@localhost [test]>select * fromtest01;
Empty set (0.00 sec)
登陆MySQL,查看目前的binlog文件
root@localhost [test]>show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 65396 |
| mysql-bin.000002 | 1179102 |
| mysql-bin.000003 | 3401 |
+------------------+-----------+
3 rows in set (0.03 sec)
最新的binlog文件是mysql-bin.000002,我们再定位误操作SQL的binlog位置
[root@bogon ~]# pythonbinlog2sql/binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -dtest -ttest01--start-file='mysql-bin.000004'
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (25, 1, 'tom'); #start 239 end 412
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (23, 2, '小丫'); #start491 end 667
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (28, 3, '飞飞'); #start746 end 922
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (16, 4, '参参'); #start1001 end 1177
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (330, 5, '顺子'); #start1256 end 1432
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (48, 6, '炸弹'); #start1511 end 1687
DELETE FROM `test`.`test01` WHERE `age`=25AND `id`=1 AND `name`='tom' LIMIT 1; #start 1766 end 2019
DELETE FROM `test`.`test01` WHERE `age`=23AND `id`=2 AND `name`='小丫' LIMIT 1;#start 1766 end 2019
DELETE FROM `test`.`test01` WHERE `age`=28AND `id`=3 AND `name`='飞飞' LIMIT 1;#start 1766 end 2019
DELETE FROM `test`.`test01` WHERE `age`=16AND `id`=4 AND `name`='参参' LIMIT 1;#start 1766 end 2019
DELETE FROM `test`.`test01` WHERE `age`=330AND `id`=5 AND `name`='顺子' LIMIT 1;#start 1766 end 2019
DELETE FROM `test`.`test01` WHERE `age`=48AND `id`=6 AND `name`='炸弹' LIMIT 1;#start 1766 end 2019
生成回滚sql,并检查回滚sql是否正确
[root@bogon ~]# pythonbinlog2sql/binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -dtest -ttest01--start-file='mysql-bin.000004' --start-pos=1766 --end-pos=2019 -B
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (48, 6, '炸弹'); #start1766 end 2019
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (330, 5, '顺子'); #start1766 end 2019
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (16, 4, '参参'); #start1766 end 2019
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (28, 3, '飞飞'); #start1766 end 2019
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (23, 2, '小丫'); #start1766 end 2019
INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (25, 1, 'tom'); #start 1766 end 2019
确认回滚sql正确,执行回滚语句。
[root@bogon ~]# pythonbinlog2sql/binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -dtest -ttest01--start-file='mysql-bin.000004' --start-pos=1766 --end-pos=2019 -B | mysql-h127.0.0.1 -P3306
登录mysql,数据回滚成功
root@localhost [test]>select * fromtest01;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 |tom | 25 |
| 2 | 小丫 | 23|
| 3 | 飞飞 | 28|
| 4 | 参参 | 16|
| 5 | 顺子 | 330|
| 6 | 炸弹 | 48|
+----+--------+------+
(END)
PS:本软件适用于delete和updete误操作快速闪回,其他功能本人还没有测试过,再次感谢作者的开源。
为了方便大家交流,本人开通了微信公众号和QQ群,QQ群:291519319,喜欢技术的一起来交流吧
这篇关于mysql binlog 闪回_MySQL误操作之快速闪回binlog2sql的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!