借助的工具主要是美团的 MyFlash。gitHub地址[https://github.com/Meituan-Dianping/MyFlash]。
但是有一些限制要注意:
binlog格式必须为row,且binlog_row_image=full
在my.cnf里面加上如下配置
[mysqld]
server-id=1
log_bin=mysql_bin
binlog_format=ROW
binlog_row_image=full
仅支持5.6与5.7
- 只能回滚DML(增、删、改)
1、安装
直接下载源码解压:
wget https://github.com/Meituan-Dianping/MyFlash/archive/refs/heads/master.zip
unzip master.zip
然后会出现一个MyFlash-master的文件夹
cd MyFlash-master
动态编译
gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback
2. 使用
MyFlash也是基于binlog 日志实现的。所以需要先拉取binlog日志。如果数据库服务就在本地不需要进行拉取。
如果是数据库服务在其他服务,需要先用mysqlbinlog拉取远程binlog日志。
具体可以mysqlbinlog —help
mysqlbinlog -uadmin -hxxx.xxx.xx.xx -padmin -P3306 --read-from-remote-server --raw mysql_bin.000003 -r mysql_bin.000003
-r 就是导出到目标文件位置
--read-from-remote-server 说明是从远程服务拉取
--raw 说明是以二进制日志导出 而不是以sql statement方式导出。因为MyFlash使用的是二进制日志进行还原。
首先你要确定要恢复那个时间段内的的日志,然后定位到是在那个binlog日志里面,然后去拉取 导出到本地。
这个命令我执行的时候会报错。可以直接从远程服务器下载下来。
然后到 MyFlash-master/binary目录下
先导出成还原的sql
./flashback --databaseNames=flash_test --tableNames=student --sqlTypes='DELETE' --binlogFileNames=/usr/data/mysql/mysql_bin.000003 --outBinlogFileNameBase=flash_test_recovery
然后再使用mysqlbinlog进行恢复
ysqlbinlog flash_test_recovery | mysql -uadmin -padmin -hxxx.xxx.xx.xx -P3306
在执行flashback生成回滚sql的时候,如果知道要恢复的位置或者时间段, 可以指定start-position=xx和stop-position=xx。或者start-datetime,stop-datetime。