使用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 #输出到的文件夹,必须提前存在且为空白。

相关推荐
倔强的石头_21 小时前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
jiayou642 天前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
于眠牧北2 天前
MySQL的锁类型,表锁,行锁,MVCC中所使用的临键锁
mysql
李广坤3 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
Turnip12024 天前
深度解析:为什么简单的数据库"写操作"会在 MySQL 中卡住?
后端·mysql
爱可生开源社区4 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1774 天前
《从零搭建NestJS项目》
数据库·typescript
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏5 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
李慕婉学姐5 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端