安装
wget https://github.com/liuhr/my2sql/raw/master/releases/centOS_release_7.x/my2sql
my2sql简介
go版MySQL binlog解析工具,通过解析MySQL binlog ,可以生成原始SQL、回滚SQL、去除主键的INSERT SQL等,也可以生成DML统计信息。类似工具有binlog2sql、MyFlash、my2fback等。
用途
- 数据快速回滚(闪回)
- 主从切换后新master丢数据的修复
- 从binlog生成标准SQL,带来的衍生功能
- 生成DML统计信息,可以找到哪些表更新的比较频繁
- IO高TPS高, 查出哪些表在频繁更新
- 找出某个时间点数据库是否有大事务或者长事务
- 主从延迟,分析主库执行的SQL语句
- 除了支持常规数据类型,对大部分工具不支持的数据类型做- 了支持,比如json、blob、text、emoji等数据类型sql生成
工具优势
功能丰富,参考上文
基于golang实现,速度快,全量解析1.1Gbinlog只需要1分30秒左右,当前其他类似开源工具一般要几十分钟
限制
- 使用回滚/闪回功能时,binlog格式必须为row,且binlog_row_image=full, DML统计以及大事务分析不受影响
- 只能回滚DML, 不能回滚DDL
- 支持指定–tl时区来解释binlog中time/datetime字段的内容。开始时间–start–datetime与结束时间-stop–datetime也会使用此指定的时区, 但注意此开始与结束时间针对的是binlog event header中保存的unix timestamp。结果中的额外的datetime时间信息都是binlog event header中的unix timestamp
- 此工具是伪装成从库拉取binlog,需要连接数据库的用户有SELECT, REPLICATION SLAVE, REPLICATION CLIENT权限
账号所需权限
grant REPLICATION CLIENT,REPLICATION SLAVE on *.* to my2sql@’%’ identified by ‘9WJKXw8e‘;
grant select on dhr_organization_service.* TO my2sql@’%’;
参数解析
参数 |
含义 |
|
实例IP 默认127.0.0.1 |
||
this program replicates from mysql as slave to read binlogs. Must set this server id unique from other slaves, default 1113306 (default 1113306) |
||
用户 |
||
用户密码 |
||
only parse these tables, comma seperated, DONOT prefix with schema, default all. |
||
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”) |
|
result output dir, default current work dir. Attension, result files could be large, set it to a dir with large free space |
||
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 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 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” |
||
Works with -workType=2sql|rollback. threads to run (default 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。 默认本地(默认“本地”) |
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 |
|
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 |
|
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 |
|
-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 |
|
for insert statement when -workType=2sql, ignore primary key |
||
–local-binlog-file string |
||
-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) |
|
场景
SELECT, REPLICATION SLAVE, REPLICATION CLIENT
grant SELECT, REPLICATION SLAVE, REPLICATION CLIENT on *.* to my2sql identified by 'my2sql';
场景1 回滚
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 ‘添加的时间’, 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” |
场景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” |
场景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 |
场景4 解析本地
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” |
与binlog2sql性能对比
1.2G binlog
my2sql |
binlog2sql |
||
正向本地 |
2线程 |
89秒 |
|
8线程 |
72秒 |
||
16线程 |
77秒 |
||
正向SQL |
8线程 |
62秒 |
|
回滚SQL |
8线程 |
35秒 |
2601 秒 |
参考
原文地址:https://blog.csdn.net/weixin_48154829/article/details/134695953
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_11847.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!