(一)binlog2sql介绍html
binlog2sql是国内MySQL大佬danfengcao开发,许多MySQL爱好者参与改进的一款MySQL binlog解析软件。根据不一样选项,能够获得原始SQL、回滚SQL、去除主键的SQL等。python
github地址为:https://github.com/danfengcao/binlog2sqlmysql
该工具主要用于:git
适用MySQL版本:MySQL5.6 、MySQL5.7github
(二)安装binlog2sqlsql
# 安装git shell> yum install -y git # 安装pip工具 shell> yum install -y epel-release shell> yum install -y python-pip # 安装binlog2sql shell> git clone https://github.com/danfengcao/binlog2sql.git && cd binlog2sql shell> pip install -r requirements.txt
(三)使用binlog2sqlshell
要使用binlog2sql,MySQL服务器须要设置如下参数:数据库
[mysqld] server_id = 1 log_bin = /var/log/mysql/mysql-bin.log max_binlog_size = 100M binlog_format = row binlog_row_image = full
binlog2sql在使用时须要链接到数据库上,链接用户的权限为:服务器
select,spuer/replication client,replication slave -- 建议受权: grant select,replication slave,replication client on *.* to user;
binlog2sql的语法为:工具
[root@masterdb binlog2sql]# pwd /root/binlog2sql/binlog2sql [root@masterdb binlog2sql]# python binlog2sql.py --help usage: binlog2sql.py [-h HOST] [-u USER] [-p [PASSWORD [PASSWORD ...]]] [-P PORT] [--start-file START_FILE] [--start-position START_POS] [--stop-file END_FILE] [--stop-position END_POS] [--start-datetime START_TIME] [--stop-datetime STOP_TIME] [--stop-never] [--help] [-d [DATABASES [DATABASES ...]]] [-t [TABLES [TABLES ...]]] [--only-dml] [--sql-type [SQL_TYPE [SQL_TYPE ...]]] [-K] [-B] [--back-interval BACK_INTERVAL]
语法解析:
MySQL连接配置参数
解析模式参数:
范围控制参数:
对象过滤参数:
(四)binlog2sql测试
测试目的:本次实验模拟误删除数据,经过使用binlog2sql将数据找回来。
STEP1:构造测试数据
--构造测试表 create table test01 ( id int primary key, name varchar(30) not null, birthday date not null ); -插入3条数据 insert into test01 values(1,'小明','1993-01-02'); insert into test01 values(2,'小华','1994-08-15'); insert into test01 values(3,'小丽','1995-07-12'); mysql> select * from test01; +----+--------+------------+ | id | name | birthday | +----+--------+------------+ | 1 | 小明 | 1993-01-02 | | 2 | 小华 | 1994-08-15 | | 3 | 小丽 | 1995-07-12 | +----+--------+------------+ 3 rows in set (0.00 sec)
STEP2:模拟误删除数据
mysql> delete from test01; Query OK, 3 rows affected (0.00 sec)
STEP3:确认最后的日志
mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000001 | 1600 | | | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
STEP4:解析出标准SQL,用于定位回滚的开始和结束位置
[root@masterdb binlog2sql]# python binlog2sql.py -h127.0.0.1 -P3306 -uroot -p123456 -dlijiamandb -t test01 --sql-type DELETE --start-file='master-bin.000001' USE lijiamandb; create table test01 ( id int primary key, name varchar(30) not null, birthday date not null ); DELETE FROM `lijiamandb`.`test01` WHERE `birthday`='1993-01-02' AND `id`=1 AND `name`='小明' LIMIT 1; #start 1287 end 1569 time 2020-04-24 13:40:26 DELETE FROM `lijiamandb`.`test01` WHERE `birthday`='1994-08-15' AND `id`=2 AND `name`='小华' LIMIT 1; #start 1287 end 1569 time 2020-04-24 13:40:26 DELETE FROM `lijiamandb`.`test01` WHERE `birthday`='1995-07-12' AND `id`=3 AND `name`='小丽' LIMIT 1; #start 1287 end 1569 time 2020-04-24 13:40:26
值得注意的是,虽然我指定了只解析DELETE语句,但仍是把DDL给解析出来了。
STEP5:解析出回滚SQL
[root@masterdb binlog2sql]# python binlog2sql.py -h127.0.0.1 -P3306 -uroot -p123456 --flashback -dlijiamandb -t test01 --sql-type DELETE --start-file='master-bin.000001' INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1995-07-12', 3, '小丽'); #start 1287 end 1569 time 2020-04-24 13:40:26 INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1994-08-15', 2, '小华'); #start 1287 end 1569 time 2020-04-24 13:40:26 INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1993-01-02', 1, '小明'); #start 1287 end 1569 time 2020-04-24 13:40:26
STEP6:还原到数据库
[root@masterdb binlog2sql]# mysql -uroot -p123456 lijiamandb mysql> INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1995-07-12', 3, '小丽'); #start 1287 end 1569 time 2020-04-24 13:40:26 69 time 2020-04-24 13:40:26 INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1993-01-02', 1, '小明'); #start 1287 end 1569 time 2020-04-24 13:40:26Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1994-08-15', 2, '小华'); #start 1287 end 1569 time 2020-04-24 13:40:26 Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO `lijiamandb`.`test01`(`birthday`, `id`, `name`) VALUES ('1993-01-02', 1, '小明'); #start 1287 end 1569 time 2020-04-24 13:40:26 Query OK, 1 row affected (0.00 sec) mysql> select * from test01; +----+--------+------------+ | id | name | birthday | +----+--------+------------+ | 1 | 小明 | 1993-01-02 | | 2 | 小华 | 1994-08-15 | | 3 | 小丽 | 1995-07-12 | +----+--------+------------+ 3 rows in set (0.00 sec)
(五)总结
使用binlog2sql最大的好处就是解析出来的SQL语句很是直观,而且在注释中还包含了时间,这对于咱们去查找故障发生点很是实用。想想以前用过的mysqlbinlog工具,解析出来的结果中含有大量无关的信息,为咱们排查问题增长了难度,而binlog2sql解析出来的SQL很是干净,便于咱们排查问题,恢复数据。
相关文档集合: 1.MySQL日志--二进制日志(binlog) |