软件包下载地址:链接:https://pan.baidu.com/s/15dDeGufVWOgVrfATGWBzaA 密码:gnz8
DBA或开发人员,有时会误删或者误更新数据,如果是线上环境会影响较大,这就需要能快速回滚;而MySQL闪回(flashback)利用binlog能直接进行回滚,并且能快速恢复数据(MySQL闪回现在只支持DML语句进行闪回)。
利用binlog闪回需要的条件:
Mysql参数设置:
log_bin = /目录/mysql-bin.log(开启)
binlog_format = row
binlog_row_image = full(默认是full)
工作原理主要是对于delete操作,从binlog取出delete信息,生成insert的回滚语句。对于insert操作,回滚SQL是delete。对于update操作,回滚sql应该交换SET和WHERE的值。
安装:
环境准备安装各种依赖的工具包列表
将python换成python3.6版本 (python版本升级参考博客:https://blog.csdn.net/wwwdaan5com/article/details/78218277)
python-pip ,
PyMySQL ,
python-mysql-replication,
wheel argparse
1、binlog2sql下载
https://github.com/danfengcao/binlog2sql
2、binlog2sql依赖包安装
(1)PyMySQL-0.8.0安装
https://pypi.python.org/pypi/PyMySQL/
[root@node1 binlogsql]# tar -xzvf PyMySQL-0.8.0.tar.gz
[root@node1 binlogsql]# cd PyMySQL-0.8.0
[root@node1 PyMySQL-0.8.0]# python setup.py install
(2)wheel-0.31.0安装
https://pypi.python.org/pypi/wheel/
[root@node1 binlogsql]# tar -xzvf wheel-0.31.0.tar.gz
[root@node1 binlogsql]# cd wheel-0.31.0
[root@node1 wheel-0.31.0]# python setup.py install
(3)python-mysql-replication安装
https://github.com/noplay/python-mysql-replication
[root@node1 binlogsql]# unzip python-mysql-replication-master.zip
[root@node1 binlogsql]# cd python-mysql-replication-master
[root@node1 python-mysql-replication-master]# python setup.py install
(4)可以通过pip安装相应的依赖包
https://pypi.python.org/pypi/pip
[root@node1 tools]# tar -xzvf pip-10.0.1.tar.gz
[root@node1 tools]# cd pip-10.0.1
[root@node1 pip-10.0.1]# python setup.py install
[root@node1 binlog2sql-master]# pip install -r requirements.txt
可能会出错误。
FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/lib/python3.6/site-packages/mysql_replication-0.18-py3.6.egg'
依赖包版本问题。
执行。。pip install mysql-replication
再执行 pip install -r requirements.txt
DSC0000.jpg (37.72 KB, 下载次数: 2)
2018-10-2 08:09 上传
显示都已安装完成。
实战演练:
1.查看测试数据:
MariaDB [test]> select * from t;
+----+--------------+
|> +----+--------------+
| 1 | ga |
| 31 | ga |
| 38 | ga |
| 45 | ga |
| 52 | hg |
| 59 | hh |
| 61 | 规划如果 |
| 68 | 干点啥 |
| 73 | ww |
| 80 | ww |
| 87 | gg |
| 94 | gg |
+----+--------------+
12 rows in set (0.00 sec)
2.删除部分数据:
MariaDB [test]> delete from t where> Query OK, 8 rows affected (0.18 sec)
MariaDB [test]> select * from t;
+----+------+
|> +----+------+
| 1 | ga |
| 31 | ga |
| 38 | ga |
| 45 | ga |
+----+------+
4 rows in set (0.00 sec)
删了8条记录。
3.查看master的日志位置。
MariaDB [test]> show master status;
+------------------+-----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000029 | 468829331 | | |
+------------------+-----------+--------------+------------------+
1 row in set (0.01 sec)
4.根据误操作的大概时间找出相应的误操作语句
[root@localhost binlog2sql]# python binlog2sql.py -h172.168.1.201 -P3306 -uroot -p123456 -dtest -tt --start-file='mysql-bin.000029' --start-datetime='2018-05-10 11:00:00' --stop-datetime='2018-05-10 11:10:00' > a.sql
[root@localhost binlog2sql]# cat a.sql
DELETE FROM `test`.`t` WHERE `id`=52 AND `name`='hg' LIMIT 1; #start 468829113 end 468829304 time 2018-05-10 11:06:52
DELETE FROM `test`.`t` WHERE `id`=59 AND `name`='hh' LIMIT 1; #start 468829113 end 468829304 time 2018-05-10 11:06:52
DELETE FROM `test`.`t` WHERE `id`=61 AND `name`='规划如果' LIMIT 1; #start 468829113 end 468829304 time 2018-05-10 11:06:52
DELETE FROM `test`.`t` WHERE `id`=68 AND `name`='干点啥' LIMIT 1; #start 468829113 end 468829304 time 2018-05-10 11:06:52
DELETE FROM `test`.`t` WHERE `id`=73 AND `name`='ww' LIMIT 1; #start 468829113 end 468829304 time 2018-05-10 11:06:52
DELETE FROM `test`.`t` WHERE `id`=80 AND `name`='ww' LIMIT 1; #start 468829113 end 468829304 time 2018-05-10 11:06:52
DELETE FROM `test`.`t` WHERE `id`=87 AND `name`='gg' LIMIT 1; #start 468829113 end 468829304 time 2018-05-10 11:06:52
DELETE FROM `test`.`t` WHERE `id`=94 AND `name`='gg' LIMIT 1; #start 468829113 end 468829304 time 2018-05-10 11:06:52
从a.sql中可以查到这个事物在binlog中的start和end(binlog2sql对于同一个事务会输出同样的start position)
5.根据上一步中的位置点生成相应的insert恢复语句
使用 -B 选项生成回滚sql,检查回滚sql是否正确。(注:真实场景下,生成的回滚SQL经常会需要进一步筛选,查看里面是否有别的表的dml语句以及本表的非delete操作的语句,结合grep、编辑器等)
[root@localhost binlog2sql]# python binlog2sql.py -h172.168.1.201 -P3306 -uroot -p123456 -dtest -tt --start-file='mysql-bin.000029' --start-position=468829113 --stop-position=468829304 -B > b.sql
[root@localhost binlog2sql]# cat b.sql
INSERT INTO `test`.`t`(`id`, `name`) VALUES (94, 'gg'); #start 468829113 end 468829304 time 2018-05-10 11:06:52
INSERT INTO `test`.`t`(`id`, `name`) VALUES (87, 'gg'); #start 468829113 end 468829304 time 2018-05-10 11:06:52
INSERT INTO `test`.`t`(`id`, `name`) VALUES (80, 'ww'); #start 468829113 end 468829304 time 2018-05-10 11:06:52
INSERT INTO `test`.`t`(`id`, `name`) VALUES (73, 'ww'); #start 468829113 end 468829304 time 2018-05-10 11:06:52
INSERT INTO `test`.`t`(`id`, `name`) VALUES (68, '干点啥'); #start 468829113 end 468829304 time 2018-05-10 11:06:52
INSERT INTO `test`.`t`(`id`, `name`) VALUES (61, '规划如果'); #start 468829113 end 468829304 time 2018-05-10 11:06:52
INSERT INTO `test`.`t`(`id`, `name`) VALUES (59, 'hh'); #start 468829113 end 468829304 time 2018-05-10 11:06:52
INSERT INTO `test`.`t`(`id`, `name`) VALUES (52, 'hg'); #start 468829113 end 468829304 time 2018-05-10 11:06:52
和删除语句进行对比 刚好8条。
6、与开发确认回滚sql没问题后,执行回滚语句并确认回滚成功。
MariaDB [test]> source /home/binlog2sql-master/binlog2sql/b.sql
Query OK, 1 row affected (0.06 sec)
Query OK, 1 row affected (0.06 sec)
Query OK, 1 row affected (0.09 sec)
Query OK, 1 row affected (0.05 sec)
Query OK, 1 row affected (0.05 sec)
Query OK, 1 row affected (0.05 sec)
Query OK, 1 row affected (0.05 sec)
Query OK, 1 row affected (0.05 sec)
MariaDB [test]> select * from t;
+----+--------------+
|> +----+--------------+
| 1 | ga |
| 31 | ga |
| 38 | ga |
| 45 | ga |
| 52 | hg |
| 59 | hh |
| 61 | 规划如果 |
| 68 | 干点啥 |
| 73 | ww |
| 80 | ww |
| 87 | gg |
| 94 | gg |
+----+--------------+
12 rows in set (0.00 sec)
7。
(1)闪回的关键是快速筛选出真正需要回滚的SQL。
(2)先根据库、表、时间做一次过滤,再根据位置做更准确的过滤。
(3)由于数据一直在写入,要确保回滚sql中不包含其他数据。可根据是否是同一事务、误操作行数、字段值的特征等等来帮助判断。
(4)执行回滚sql时如有报错,需要查实具体原因,一般是因为对应的数据已发生变化。由于是严格的行模式,只要有唯一键(包括主键)存在,就只会报某条数据不存在的错,不必担心会更新不该操作的数据。
(5)如果待回滚的表与其他表有关联,要与开发说明回滚和不回滚各自的副作用,再确定方案。
(6)最重要的两点:筛选出正确SQL!与开发沟通清楚!