使用my2sql进行mysql的binlog恢复数据

参考,侵权即删:https://blog.csdn.net/weixin_48154829/article/details/134695953

一、安装my2sql

#wget https://github.com/liuhr/my2sql/raw/master/releases/centOS_release_7.x/my2sql

#chmod+x my2sql

#mv my2sql /usr/local/sbin/

二、命名参数详解

#我不理解的是为什么解析binlog文件需要连接数据库,直接解析不行么?有没有大佬知道

|---------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------|
| 参数 | | 含义 |
| -host string | mysql host, default 127.0.0.1 . (default "127.0.0.1") | 实例IP 默认127.0.0.1 |
| -port uint | mysql port, default 3306. (default 3306) | 端口 默认3306 |
| -user string | mysql user | 用户 |
| -password string | mysql user password. | 用户密码 |
| -databases string | only parse these databases, comma seperated, default all. | 只解析这些数据库,逗号分隔,默认全部。 |
| -tables string | only parse these tables, comma seperated, DONOT prefix with schema, default all. | 只解析这些表,逗号分隔,不要以schema为前缀,默认全部。 |
| -mysql-type string | valid options are: mysql,mariadb. server of binlog, mysql or mariadb, default mysql (default "mysql") | 有效选项是:mysql,mariadb. server of binlog, mysql or mariadb,默认mysql(默认"mysql") |
| -output-dir string | result output dir, default current work dir. Attension, result files could be large, set it to a dir with large free space | 结果输出目录,默认当前工作目录。 注意,结果文件可能很大,将其设置为可用空间大的目录 |
| -mode string | valid options are: repl,file. repl: as a slave to get binlogs from master. file: get binlogs from local filesystem. default repl (default "repl") | 有效的选项有:repl, file。 repl:作为从服务器从主服务器获取二进制日志。 file:从本地文件系统获取二进制日志。 默认值为 repl(默认 "repl") |
| -sql string | valid options are: insert,update,delete. only parse these types of sql, comma seperated, valid types are: insert, update, delete; default is all(insert,update,delete) | 有效选项为: insert,update,delete。仅解析这些类型的SQL语句,以逗号分隔,有效类型为: insert,update,delete;默认为全部( insert,update,delete)。 |
| -start-datetime string | Start reading the binlog at first event having a datetime equal or posterior to the argument, it should be like this: "2020-01-01 01:00:00" | "2020-01-01 01:00:00" |
| -stop-datetime string | Stop reading the binlog at first event having a datetime equal or posterior to the argument, it should be like this: "2020-12-30 01:00:00" | "2020-12-30 01:00:00" |
| -start-file string | binlog file to start reading | 从哪个 binlog 文件开始读取 |
| -stop-file string | binlog file to stop reading | 从哪个 binlog 文件结束读取 |
| -start-pos uint | start reading the binlog at position (default 4) | 从 binlog 的位置开始读取(默认为 4) |
| -stop-pos uint | Stop reading the binlog at position (default 4) | 从 binlog 的位置结束读取(默认为 4) |
| -work-type string | valid options are: 2sql,rollback,stats. 2sql: convert binlog to sqls, rollback: generate rollback sqls, stats: analyze transactions. default: 2sql (default "2sql") | 有效选项为:2sql、rollback、stats。 2sql:将binlog转换为SQL语句; rollback:生成回滚SQL语句; stats:分析事务。 默认值:2sql(默认值为"2sql") |
| -local-binlog-file string | local binlog files to process, It works with -mode=file | 待处理的本地binlog文件,适用于-mode=file模式 |
| -add-extraInfo | Works with -work-type=2sql|rollback. Print database/table/datetime/binlogposition...info on the line before sql, default false | 与 -work-type=2sql|rollback 一起使用。 在sql前一行打印database/table/datetime/binlogposition...i等信息,默认false |

三、命令使用详解

1、生成原始删除语句(根据离线binlog文件)

复制代码
my2sql -host 10.0.0.1 -user root -password 'XXXX' -mode file -databases isdm -tables t_process -add-extraInfo -sql delete --start-datetime="2025-09-11 15:30:00" --stop-datetime="2025-09-11 15:40:00" -start-file ceshi6772-bin.003812 -work-type 2sql -local-binlog-file ceshi6772-bin.003812 -output-dir /tmp/my2sql/

2、生成回滚语句(根据离线binlog文件)

复制代码
my2sql -host 10.0.0.1 -user root -password 'XXXX' -mode file -databases isdm -tables t_process -add-extraInfo -sql delete --start-datetime="2025-09-11 15:30:00" --stop-datetime="2025-09-11 15:40:00" -start-file ceshi6772-bin.003812 -work-type rollback -local-binlog-file ceshi6772-bin.003812 -output-dir /tmp/my2sql/

重点参数:

-mode file #使用binlog文件

-add-extraInfo #打印binlog的position位置(可不用)

-sql delete #只输出关于delete语句的sql

-start-file #起始binlog文件

-work-type 2sql #生成原始的语句,rollback为生成回滚语句

-local-binlog-file #本地binlog文件

-output-dir #输出到的文件夹,必须提前存在且为空白。

相关推荐
IvorySQL5 小时前
从双解析器到循环工程:IvorySQL 五年技术演进路线的深度观察
大数据·数据库·人工智能·postgresql·开源
wefg15 小时前
【MySQL】事务
数据库·mysql
黑白极客5 小时前
mysql的高可用性
数据库·mysql
一十九的酒6 小时前
Oracle 12c 标准版无缝升级企业版实战记录
数据库·oracle·标准版切换到企业版
ZhengEnCi6 小时前
S02-SpringBoot实体类新增字段对已有数据的影响及自动DDL同步机制详解
数据库·spring boot
妙码生花6 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(三十二):增加 admin_rule 模型及数据表迁移
数据库·go·ai编程
千维百策6667 小时前
SRE 中如何消除琐务:提升工程效率与服务可靠性的实践方法
数据库
卓怡学长7 小时前
w266基于spring boot + vue 圣地延安美食乐享系统
java·数据库·vue.js·spring boot·spring·intellij-idea
名不经传的养虾人8 小时前
从0到1:企业级AI项目迭代日记 Vol.65|最危险的故障不是崩溃,是悄悄换掉了正确答案
数据库·人工智能·ai编程·ai-agent·企业ai
程序员佳佳8 小时前
模型网关灰度不是调百分比:把放量、观测和回滚做成账本
java·数据库·人工智能·redis·gpt·aigc·embedding