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

以下是按照你提供的 CSDN 文章整理的 Markdown 文档,严格按照原文内容逐字还原,并在 CSDN 编辑器中可直接渲染显示:


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

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

一、安装my2sql

bash 复制代码
chmod +x my2sql
mv my2sql /usr/local/sbin/

二、命名参数详解

回答一个疑问:为什么解析binlog文件还需要连接数据库???

主要原因在于它需要从数据库实例中获取表结构等元数据,才能正确地将二进制日志转换为可读的SQL语句。binlog文件本身并不完整存储表结构信息(如表名、列名、数据类型等)。它主要记录行数据的变更(在 ROW 格式下),这些变更通常通过表ID 和列ID 来标识,而不是直接使用表名和列名。简单来说,离线binlog文件 + 在线数据库的元数据 = 完整可读的SQL语句。my2sql 即使处理本地 binlog 文件,通常也需要一个"在线"的数据库连接来获取这些关键的元信息。

  1. 映射表ID到表名:binlog 中的每个事件都包含一个表ID。my2sql 需要查询数据库的information_schema或其它系统表,才能将这个ID映射回具体的数据库名和表名。
  2. 获取列信息:为了将行数据(可能只是值的数组)转换成带有列名的INSERT、UPDATE或 DELETE语句,my2sql 需要知道表的列结构、数据类型(例如,是否正确处理字符串或二进制数据)以及主键信息。
  3. 字符集处理:正确解析字符串数据需要知道表或列的字符集设置,这些信息也存储在数据库的元数据中。

my2sql 连接数据库所需的权限并不高,遵循最小权限原则即可:

  • 推荐权限:SELECT, REPLICATION CLIENT。
  • SELECT权限用于查询 information_schema或其它系统表以获取表结构。
  • REPLICATION CLIENT权限允许工具查询 binlog 相关的状态信息(在某些情况下可能需要)。
sql 复制代码
-- 创建一个专门用于解析的用户
CREATE USER 'binlog_parser'@'%' IDENTIFIED BY 'StrongPassword!123';
-- 授予必要的最小权限
GRANT SELECT, REPLICATION CLIENT ON *.* TO 'binlog_parser'@'%';
FLUSH PRIVILEGES;

参数列表

参数 含义
-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"
-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"
-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

三、命令使用详解

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

bash 复制代码
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文件)

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