下面进行实战演练:
binlog2sql工具的下载地址: https://github.com/danfengcao/binlog2sql
第一步:环境准备安装各种依赖的工具包列表
python-pip ,
PyMySQL ,
python-mysql-replication,
wheel argparse
第二步:解压binlog2sql软件,命令如下
unzip binlog2sql-master.zip
cd binlog2sql-master
pip install –r requirements.txt
第三步:通过python binlog2sql.py --help命令,来查看重要参数的使用;
-B, --flashback 生成回滚语句
--start-file 需要解析的binlog文件
--start-position 解析binlog的起始位置
--stop-position解析binlog的结束位置
--start-datetime 从哪个时间点的binlog开始解析,格式必须为datetime
--stop-datetime 到哪个时间点的binlog停止解析,格式必须为datetime
-d, --databases 只输出目标db的sql
-t, --tables 只输出目标tables的sql
第四步:开始模拟数据删除
首先删除掉zs库下,t表中的数据
[email protected] 14:26: [zs]> select * from t;
+----+------+---------+
| id | name | address |
+----+------+---------+
| 1 | aaa | bj |
| 2 | bbb | sh |
| 3 | ccc | gz |
| 4 | ddd | sy |
| 5 | eee | fj |
+----+------+---------+
5 rows in set (0.00 sec)
[email protected] 14:26: [zs]> delete from t;
Query OK, 5 rows affected (0.04 sec)
[email protected] 14:27: [zs]> select * from t;
Empty set (0.00 sec)
第五步:需要创建一个闪回用户
create user ‘zs_test‘@‘%‘ identified by ‘123456‘;
grant select,replication slave,replication client on *.* to ‘zs_test‘@‘%‘ ;
flush privileges;
第六步:确定当前binlog文件和position位置
[email protected] 14:41: [zs]> show master status;
+---------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+------------------------------------------+
| mysql-binlog.000002 | 2091 | | | e10f0ead-d595-11e7-82cb-080027cd683a:1-8 |
+---------------------+----------+--------------+------------------+------------------------------------------+
可以看到当前binlog是:mysql-binlog.000002
位置偏移量:2091
第七步:需要预估下时间,误操作的时间范围应该在下午2点20分到2点30分之间。命令如下:
python binlog2sql.py -h192.168.56.102 -P3306 -uzs_test -p123456 -dzs -tt --start-file=‘mysql-binlog.000002‘ --start-datetime=‘2017-11-30 14:20:00‘ --stop-datetime=‘2017-11-30 14:30:00‘
输出结果:
DELETE FROM zs.t WHERE address=‘bj‘ AND id=1 AND name=‘aaa‘ LIMIT 1; #start 1214 end 1427 time 2017-11-30 14:27:46
DELETE FROM zs.t WHERE address=‘sh‘ AND id=2 AND name=‘bbb‘ LIMIT 1; #start 1214 end 1427 time 2017-11-30 14:27:46
DELETE FROM zs.t WHERE address=‘gz‘ AND id=3 AND name=‘ccc‘ LIMIT 1; #start 1214 end 1427 time 2017-11-30 14:27:46
DELETE FROM zs.t WHERE address=‘sy‘ AND id=4 AND name=‘ddd‘ LIMIT 1; #start 1214 end 1427 time 2017-11-30 14:27:46
DELETE FROM zs.t WHERE address=‘fj‘ AND id=5 AND name=‘eee‘ LIMIT 1; #start 1214 end 1427 time 2017-11-30 14:27:46
从解析结果中我们了解到,误操作sql的位置是在1214~1427之间;这样就可以进一步过滤,使用flashback模式生成回滚sql;
命令如下:
python binlog2sql.py -h192.168.56.102 -P3306 -uzs_test -p123456 -dzs -tt --start-file=‘mysql-binlog.000002‘ --start-position=1214 --stop-position=1427 -B >t_rollback.sql
查看闪回导出文件:
[[email protected] binlog2sql]# cat t_rollback.sql
INSERT INTO zs
.t
(address
, id
, name
) VALUES (‘fj‘, 5, ‘eee‘); #start 1214 end 1427 time 2017-11-30 14:27:46
INSERT INTO zs
.t
(address
, id
, name
) VALUES (‘sy‘, 4, ‘ddd‘); #start 1214 end 1427 time 2017-11-30 14:27:46
INSERT INTO zs
.t
(address
, id
, name
) VALUES (‘gz‘, 3, ‘ccc‘); #start 1214 end 1427 time 2017-11-30 14:27:46
INSERT INTO zs
.t
(address
, id
, name
) VALUES (‘sh‘, 2, ‘bbb‘); #start 1214 end 1427 time 2017-11-30 14:27:46
INSERT INTO zs
.t
(address
, id
, name
) VALUES (‘bj‘, 1, ‘aaa‘); #start 1214 end 1427 time 2017-11
第八步:应用回滚文件,恢复数据。命令如下:
/usr/local/mysql/bin/mysql -uroot -proot123 zs < t_rollback.sql
第九步:检验恢复数据是否成功
[email protected] 15:09: [zs]> select * from t;
+----+------+---------+
| id | name | address |
+----+------+---------+
| 1 | aaa | bj |
| 2 | bbb | sh |
| 3 | ccc | gz |
| 4 | ddd | sy |
| 5 | eee | fj |
+----+------+---------+
5 rows in set (0.00 sec)
验证恢复数据成功!
原文地址:https://blog.51cto.com/hsuing/2383375