mysql decode语句_数据库 | MySQL | DML回滚工具MyFlash

一、简介

MyFlash是由美团点评公司技术工程部开发维护的一个回滚DML操作的工具。该工具通过解析v4版本的binlog,完成回滚操作。相对已有的回滚工具,其增加了更多的过滤选项,让回滚更加容易。 该工具已经在美团点评内部使用

二、详细说明

1.安装:

该工具推荐用户在下载源码之后,进行动态编译链接安装

git clone

cf9879a1caf2d8bce54b02b897e491d1.png

动态编译链接

[root@bogon ~/soft]# cd MyFlash/

gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback

5dd2067642bc5d56566ce0dc37791859.png

解决方法:

yum install gcc glib2-devel -y

gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback

至此,安装完成。

静态编译链接

然而用户不想每次去重新编译,可以选择使用静态链接,但是该方法需要知道glib库的版本和位置,因此对于每台机器略有不同,请谨慎使用

为了保证在一台机器上编译后,可以在其它机器上使用,需要满足以下两个条件 a) 将glib做成静态链接 b)在编译的那台机器的glibc版本(查看方法为ldd --version)要小于等于要运行该软件的那台机器glibc版本 因此需要你在一台glibc版本较低的机器上运行如下命令即可。

gcc -w -g `pkg-config --cflags glib-2.0` source/binlogParseGlib.c -o binary/flashback /usr/lib64/libglib-2.0.a -lrt

2.使用:

1.How to use

cd binary

./flashback --help

Usage:

flashback [OPTION...]

Help Options:

-?, --help Show help options

Application Options:

--databaseNames databaseName to apply. if multiple, seperate by comma(,)

--tableNames tableName to apply. if multiple, seperate by comma(,)

--start-position start position

--stop-position stop position

--start-datetime start time (format %Y-%m-%d %H:%M:%S)

--stop-datetime stop time (format %Y-%m-%d %H:%M:%S)

--sqlTypes sql type to filter . support INSERT, UPDATE ,DELETE. if multiple, seperate by comma(,)

--maxSplitSize max file size after split, the uint is M

--binlogFileNames binlog files to process. if multiple, seperate by comma(,)

--outBinlogFileNameBase output binlog file name base

--logLevel log level, available option is debug,warning,error

--include-gtids gtids to process

--exclude-gtids gtids to skip

2.Parameter explantions

下面的这些参数是可以任意组合的。

· 1.databaseNames

指定需要回滚的数据库名。多个数据库可以用","隔开。如果不指定该参数,相当于指定了所有数据库。

· 2.tableNames

指定需要回滚的表名。多个表可以用","隔开。如果不指定该参数,相当于指定了所有表。

· 3.start-position

指定回滚开始的位置。如不指定,从文件的开始处回滚。请指定正确的有效的位置,否则无法回滚

· 4.stop-position

指定回滚结束的位置。如不指定,回滚到文件结尾。请指定正确的有效的位置,否则无法回滚

· 5.start-datetime

指定回滚的开始时间。注意格式必须是 %Y-%m-%d %H:%M:%S。 如不指定,则不限定时间

· 6.stop-datetime

指定回滚的结束时间。注意格式必须是 %Y-%m-%d %H:%M:%S。 如不指定,则不限定时间

· 7.sqlTypes

指定需要回滚的sql类型。目前支持的过滤类型是INSERT, UPDATE ,DELETE。多个类型可以用","隔开。

· 8.maxSplitSize

一旦指定该参数,对文件进行固定尺寸的分割(单位为M),过滤条件有效,但不进行回滚操作。该参数主要用来将大的binlog文件切割,防止单次应用的binlog尺寸过大,对线上造成压力

· 9.binlogFileNames

指定需要回滚的binlog文件,目前只支持单个文件,后续会增加多个文件支持

· 10.outBinlogFileNameBase

指定输出的binlog文件前缀,如不指定,则默认为binlog_output_base.flashback

· 11.logLevel

仅供开发者使用,默认级别为error级别。在生产环境中不要修改这个级别,否则输出过多

· 12.include-gtids

指定需要回滚的gtid,支持gtid的单个和范围两种形式。

· 13.exclude-gtids

指定不需要回滚的gtid,用法同include-gtids

3.example

1.回滚整个文件

./flashback --binlogFileNames=haha.000041

mysqlbinlog binlog_output_base.flashback | mysql -h -u -p

2.回滚该文件中的所有insert语句

./flashback --sqlTypes='INSERT' --binlogFileNames=haha.000041

mysqlbinlog binlog_output_base.flashback | mysql -h -u -p

3.回滚大文件

回滚

./flashback --binlogFileNames=haha.000042

切割大文件

./flashback --maxSplitSize=1 --binlogFileNames=binlog_output_base.flashback

应用

mysqlbinlog binlog_output_base.flashback.000001 | mysql -h -u -p

...

mysqlbinlog binlog_output_base.flashback. | mysql -h -u -p

三、测试

建表插入测试数据如下:

create database t2;

use t2;

create table t1 (id int primary key,name int);

insert into t1 values(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

(1)单表删除恢复测试

mysql> select * from t1;

+------+------+

| id | name |

+------+------+

| 1 | 1 |

| 2 | 2 |

| 3 | 3 |

| 4 | 4 |

| 5 | 5 |

| 6 | 6 |

| 7 | 7 |

| 8 | 8 |

| 9 | 9 |

| 10 | 10 |

+------+------+

mysql> delete from t1 where id<6;

Query OK, 5 rows affected (0.00 sec)

mysql> select * from t1;

+------+------+

| id | name |

+------+------+

| 6 | 6 |

| 7 | 7 |

| 8 | 8 |

| 9 | 9 |

| 10 | 10 |

+------+------+

5 rows in set (0.00 sec)

进入binlog所在目录:

查看删除操作的位置信息

mysqlbinlog --base64-output=decode-rows -vv mysql-bin.000004|grep -C 100 delete|more

11ad4e0cf7edf72e1548d3d86dc76d3d.png

确认删除的开始和结束的位点信息后,生成flashback文件。

/root/soft/MyFlash/binary/flashback --binlogFileNames=mysql-bin.000004 --outBinlogFileNameBase=mysql-bin-000004 --tableNames=t1 --start-position=329 --stop-position=533

查看并确认即将恢复内容

mysqlbinlog --base64-output=decode-rows -vv mysql-bin-000004.flashback

恢复数据:

mysqlbinlog --skip-gtids mysql-bin-000004.flashback | mysql -uroot -p

查看恢复后数据:

mysql> select * from t1;

+----+------+

| id | name |

+----+------+

| 1 | 1 |

| 2 | 2 |

| 3 | 3 |

| 4 | 4 |

| 5 | 5 |

| 6 | 6 |

| 7 | 7 |

| 8 | 8 |

| 9 | 9 |

| 10 | 10 |

+----+------+

(2)单表更新恢复测试

mysql> update t1 set name=10 where id >5;

Query OK, 4 rows affected (0.01 sec)

Rows matched: 5 Changed: 4 Warnings: 0

mysql> select * from t1;

+----+------+

| id | name |

+----+------+

| 1 | 1 |

| 2 | 2 |

| 3 | 3 |

| 4 | 4 |

| 5 | 5 |

| 6 | 10 |

| 7 | 10 |

| 8 | 10 |

| 9 | 10 |

| 10 | 10 |

+----+------+

10 rows in set (0.00 sec)

进入binlog所在目录:

查看删除操作的位置信息

mysqlbinlog --base64-output=decode-rows -vv mysql-bin.000004|grep -C 100 update|more

40148b343b2de3116e3293ef460d7b8a.png

确认删除的开始和结束的位点信息后,生成flashback文件(flashback文件名不能重复)。

/root/soft/MyFlash/binary/flashback --binlogFileNames=mysql-bin.000004 --outBinlogFileNameBase=mysql-bin-000004 --tableNames=t1 --start-position=2790 --stop-position=3030

查看并确认即将恢复内容

mysqlbinlog --base64-output=decode-rows -vv mysql-bin-000004.flashback

恢复数据:

mysqlbinlog --skip-gtids mysql-bin-000004.flashback | mysql -uroot -p

查看恢复后数据:

mysql> select * from t1;

+----+------+

| id | name |

+----+------+

| 1 | 1 |

| 2 | 2 |

| 3 | 3 |

| 4 | 4 |

| 5 | 5 |

| 6 | 6 |

| 7 | 7 |

| 8 | 8 |

| 9 | 9 |

| 10 | 10 |

+----+------+

(3)单表插入恢复测试

mysql> insert into t1 values(11,11);

Query OK, 1 row affected (0.01 sec)

mysql> select * from t1;

+----+------+

| id | name |

+----+------+

| 1 | 1 |

| 2 | 2 |

| 3 | 3 |

| 4 | 4 |

| 5 | 5 |

| 6 | 6 |

| 7 | 7 |

| 8 | 8 |

| 9 | 9 |

| 10 | 10 |

| 11 | 11 |

+----+------+

进入binlog所在目录:

查看删除操作的位置信息

mysqlbinlog --base64-output=decode-rows -vv mysql-bin.000004|grep -C 100 'insert into t1 values(11,11)'

396a28ac3b9dba2a58d4c3035a8de9ee.png

确认删除的开始和结束的位点信息后,生成flashback文件(flashback文件名不能重复)。

/root/soft/MyFlash/binary/flashback --binlogFileNames=mysql-bin.000004 --outBinlogFileNameBase=mysql-bin-000004 --tableNames=t1 --start-position=3722 --stop-position=3893

查看并确认即将恢复内容

mysqlbinlog --base64-output=decode-rows -vv mysql-bin-000004.flashback

791cd15985b27709bc1e1b23d4b1ae99.png

恢复数据:

mysqlbinlog --skip-gtids mysql-bin-000004.flashback | mysql -uroot -p

查看恢复后数据:

mysql> select * from t1;

+----+------+

| id | name |

+----+------+

| 1 | 1 |

| 2 | 2 |

| 3 | 3 |

| 4 | 4 |

| 5 | 5 |

| 6 | 6 |

| 7 | 7 |

| 8 | 8 |

| 9 | 9 |

| 10 | 10 |

+----+------+

四、限制

1. binlog格式必须为row,且binlog_row_image=full

2. 仅支持5.6与5.7

3. 只能回滚DML(增、删、改)

参考链接:https://github.com/Meituan-Dianping/MyFlash

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值