参考,侵权即删: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 #输出到的文件夹,必须提前存在且为空白。