暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

my2sql闪回工具-通过binlog实现MySQL误操作数据恢复

原创 金同学 2023-05-22
1635

my2sql是一款开源的binlog日志解析工具,使用go语言开发,它通过解析MySQL binlog ,可以生成标准SQL、回滚SQL、DML统计信息等。类似工具有binlog2sql、MyFlash、my2fback等,本工具基于my2fback、binlog_rollback二次开发而来,解析效率非常高。经测试,mysql7、mysql8都可以使用。

功能
  • 数据快速回滚(闪回);
  • 生成DML统计信息;
  • 找出某个时间点数据库是否有大事务或者长事务;
要求&限制
  • 使用回滚/闪回功能时,binlog_format=row,且binlog_row_image=full。另外在mysql8.0 使用回滚/闪回功能时, binlog必须关闭压缩功能(binlog_transaction_compression=OFF);
  • my2sql只能回滚DML,不能回滚DDL;
  • my2sql访问mysql的账号的密码插件必须为mysql_native_password,且用户权限必须包含select, replication slave, replication client;
my.cnf修改以下参数
[mysqld]
binlog_format=row
binlog_row_image=full
default_authentication_plugin=mysql_native_password
binlog_transaction_compression=OFF

再次提示,数据库必须关闭压缩功能,即binlog_transaction_compression=OFF,否则binlog解析获取不到sql语句。

快速部署my2sql工具

方法1  编译好的二进制文件(适用于centos7)

https://www.modb.pro/doc/103586

下载、解压后二进制文件的路径:my2sql-master-centos7/releases/centOS_release_7.x

方法2  go编译

https://github.com/liuhr/my2sql

git clone https://github.com/liuhr/my2sql.git
cd my2sql/
go build .

my2sql参数介绍

-work-type
2sql:生成原始sql,rollback:生成回滚sql,stats:只统计DML、事务信息

-sql
要解析的sql类型,可选参数insertupdatedelete,默认全部解析
示例:--sql update delete

-mode
repl: 伪装成从库解析binlog文件,file: 离线解析binlog文件, 默认repl

-local-binlog-file
当指定-mode=file 参数时,需要指定-local-binlog-file binlog文件相对路径或绝对路径,可以连续解析多个binlog文件,只需要指定起始文件名,程序会自动持续解析下个文件

-add-extraInfo
是否把database/table/datetime/binlogposition...信息以注释的方式加入生成的每条sql前,默认false

-big-trx-row-limit n
transaction with affected rows greater or equal to this value is considerated as big transaction 
找出满足n条sql的事务,默认500条

-doNotAddPrifixDb
Prefix table name witch database name in sql,ex: insert into db1.tb1 (x1, x1) values (y1, y1)
默认生成insert into db1.tb1 (x1, x1) values (y1, y1)类sql,也可以生成不带库名的sql

-file-per-table
为每个表生成一个sql文件

-full-columns
生成的sql是否带全列信息,默认false

-ignorePrimaryKeyForInsert
生成的insert语句是否去掉主键,默认false

使用my2sql工具解析binglog文件

生成标准SQL

./my2sql  -user restore -password 'xxx'  -port 3306 \
-host 127.0.0.1 -databases dbtest  -tables t1 \
-work-type 2sql -add-extraInfo -full-columns -start-file mysql-bin.000001 \
-start-datetime "2023-05-21 13:50:00" --stop-datetime "2023-05-21 14:00:00" \
-output-dir /tmp/sql_tmpdir/

生成回滚SQL

./my2sql  -user restore -password 'xxx' -port 3306 \
-host 127.0.0.1 -databases dbtest  -tables t1 \
-work-type rollback -add-extraInfo -full-columns -start-file mysql-bin.000001 \
-start-datetime "2023-05-21 13:50:00" --stop-datetime "2023-05-21 14:00:00" \
-output-dir /tmp/sql_tmpdir/

生成统计信息

./my2sql -user restore -password 'xxx' -port 3306 \
-host 127.0.0.1 -databases dbtest  -tables t1 \
-work-type stats -start-file mysql-bin.000001 \
-start-datetime "2023-05-21 13:50:00" --stop-datetime "2023-05-21 14:00:00" \
-output-dir /tmp/sql_tmpdir/
最后修改时间:2023-05-23 12:22:40
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论