目录
安装
my2sql简介
用途
工具优势
限制
账号所需权限
参数解析
场景
场景1 回滚
安装比较简单 直接下载二进制命令即可使用
wget https://github.com/liuhr/my2sql/raw/master/releases/centOS_release_7.x/my2sql
go版MySQL binlog解析工具,通过解析MySQL binlog ,可以生成原始SQL、回滚SQL、去除主键的INSERT SQL等,也可以生成DML统计信息。类似工具有binlog2sql、MyFlash、my2fback等。
功能丰富,参考上文
基于golang实现,速度快,全量解析1.1Gbinlog只需要1分30秒左右,当前其他类似开源工具一般要几十分钟
grant REPLICATION CLIENT,REPLICATION SLAVE on *.* to my2sql@'%' identified by '9WJKXw8e';
grant select on dhr_organization_service.* TO my2sql@'%';
参数 |
含义 |
|
-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 |
-server-id uint |
this program replicates from mysql as slave to read binlogs. Must set this server id unique from other slaves, default 1113306 (default 1113306) |
|
-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为前缀,默认全部。 |
-ignore-databases string |
ignore parse these databases, comma seperated, default null |
忽略解析的数据库,逗号分隔,默认为空 |
-ignore-tables string |
ignore parse these tables, comma seperated, default null |
忽略解析的表,逗号分隔,默认为空 |
-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 |
结果输出目录,默认当前工作目录。 注意,结果文件可能很大,将其设置为可用空间大的目录 |
-output-toScreen |
Just output to screen,do not write to file |
只输出到屏幕,不写入文件 |
-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:作为从获取二进制日志。 文件:从本地文件系统获取二进制日志。 默认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) |
|
-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 |
|
-stop-file string |
binlog file to stop reading |
|
-start-pos uint |
start reading the binlog at position (default 4) |
|
-stop-pos uint |
Stop reading the binlog at position (default 4) |
|
-threads uint |
Works with -workType=2sql|rollback. threads to run (default 2) |
与 -workType=2sql|rollback 一起使用。 要运行的线程(默认 2) |
-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转换为sqls,rollback:生成回滚sqls,stats:分析事务。 默认值:2sql(默认“2sql”) |
-tl string |
time location to parse timestamp/datetime column in binlog, such as Asia/Shanghai. default Local (default "Local") |
解析binlog中timestamp/datetime列的时区,如Asia/Shanghai。 默认本地(默认“本地”) |
-print-interval int |
works with -w='stats', print stats info each PrintInterval. Valid values range from 1 to 600, default 30 (default 30) |
与 -w='stats' 一起使用,打印每个 PrintInterval 的统计信息。 有效值范围从 1 到 600,默认 30(默认 30) |
-U |
prefer to use unique key instead of primary key to build where condition for delete/update sql |
优先使用唯一键而不是主键来构建删除/更新 sql 的 where 条件 |
-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 |
-big-trx-row-limit int |
受影响的行大于或等于此值的事务被视为大事务。 有效值范围从 1 到 10,默认 30000(默认 30000) |
|
-do-not-add-prifixDb |
Prefix table name witch database name in sql,ex: insert into db1.tb1 (x1, x1) values (y1, y1). |
为表名添加库名 |
-file-per-table |
One file for one table if true, else one file for all tables. default false. Attention, always one file for one binlog |
如果为 true,则为一个表一个文件,否则为所有表一个文件。 默认false。 注意,一个binlog总是一个文件 |
-full-columns |
For update sql, include unchanged columns. for update and delete, use all columns to build where condition. default false, this is, use changed columns to build set part, use primary/unique key to build where condition |
对于更新 sql,包括未更改的列。 对于更新和删除,使用所有列来构建 where 条件。 默认 false,即使用更改的列来构建集合部分,使用主键/唯一键来构建 where 条件 |
-ignore-primaryKey-forInsert |
for insert statement when -workType=2sql, ignore primary key |
对于 -workType=2sql 时的插入语句,忽略主键 |
-local-binlog-file string |
local binlog files to process, It works with -mode=file |
要处理的本地 binlog 文件,它与 -mode=file 一起使用 |
-long-trx-seconds int |
transaction with duration greater or equal to this value is considerated as long transaction. Valid values range from 0 to 1, default 3600 (default 3600) |
持续时间大于或等于该值的事务被看做长事务。 有效值范围从 0 到 1,默认 3600(默认 3600) |
创建账号
最小权限
SELECT, REPLICATION SLAVE, REPLICATION CLIENT
grant SELECT, REPLICATION SLAVE, REPLICATION CLIENT on *.* to my2sql identified by 'my2sql';
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `number` int(11) NOT NULL, `name` varchar(255) DEFAULT NULL, `add_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加的时间', `content` json DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `idx_name` (`number`,`name`) ) ENGINE=InnoDB AUTO_INCREMENT=1234 DEFAULT CHARSET=utf8 INSERT INTO `my2sql_test`.`student` (`id`,`number`,`name`,`add_time`,`content`) VALUES (1233,26,'ranran','2020-07-15 19:06:03',null); INSERT INTO `my2sql_test`.`student` (`id`,`number`,`name`,`add_time`,`content`) VALUES (1232,134,'asdf','2020-07-12 11:08:41',null); INSERT INTO `my2sql_test`.`student` (`id`,`number`,`name`,`add_time`,`content`) VALUES (1231,21,'chenxi','2020-07-12 10:12:45',null); INSERT INTO `my2sql_test`.`student` (`id`,`number`,`name`,`add_time`,`content`) VALUES (1229,20,'chenxi','2020-07-11 16:20:50',null); INSERT INTO `my2sql_test`.`student` (`id`,`number`,`name`,`add_time`,`content`) VALUES (1227,18,'hanran','2020-07-06 21:55:48','{\"age\":13,\"author\":\"liuhan\"}'); delete from student; my2sql -user my2sql -password -port 3307 \ -host 127.0.0.1 -databases my2sql_test -tables student \ -work-type rollback -start-file mysql-bin.000326 -add-extraInfo -sql delete \ -start-datetime "2022-07-08 10:30:00" --stop-datetime "2022-07-08 10:35:00" \ -output-dir /home/backups/my2sql |
场景2 生成正向SQL
my2sql -host 127.0.0.1 -user my2sql -password -port 3307 \ -databases my2sql_test -tables student \ -work-type 2sql -start-file mysql-bin.000326 -add-extraInfo -sql delete \ -start-datetime "2022-07-08 10:30:00" --stop-datetime "2022-07-08 10:35:00" \ -output-dir /home/backups/my2sql |
场景3 DML与事务统计
my2sql -user my2sql -password -port 3307 \ -host 127.0.0.1 \ -work-type stats -start-file mysql-bin.000325 stop-file mysql-bin.000325 \ -big-trx-row-limit 5 -long-trx-seconds 3 \ -output-dir /home/backups/my2sql |
场景4 解析本地
需要连接数据库 server
my2sql -user my2sql -password -port 3307 \ -host 127.0.0.1 -databases objective_stage_20220707 -tables objective \ -work-type 2sql -start-file mysql-bin.000326 -add-extraInfo -sql delete \ -start-datetime "2022-07-07 17:30:00" --stop-datetime "2022-07-07 17:35:00" \ -output-dir /home/backups/ |
性能对比
1.2G binlog
my2sql |
binlog2sql |
||
正向本地 |
2线程 |
89秒 |
|
8线程 |
72秒 |
||
16线程 |
77秒 |
||
正向SQL |
8线程 |
62秒 |
|
回滚SQL |
8线程 |
35秒 |
2601 秒 |
参考
GitHub - liuhr/my2sql