my2sql 安装使用(回滚,分析大事务长事务)
官方github
https://github.com/liuhr/my2sql
1、安装
本想用源码安装,但一直报错,后来直接下载的二进制文件
wget https://raw.githubusercontent.com/liuhr/my2sql/master/releases/centOS_release_7.x/my2sql chmod +x my2sql ./my2sql
2、使用
语法和 binlog2sql 类似,不过有一些binlog2sql上的bug,在这个软件中没有。
比如timestamp(3)问题:
https://github.com/danfengcao/binlog2sql/issues/90
分析在线binlog,正向解析SQL
./my2sql -user root -password c123456 -host 127.0.0.1 -port 3357 -mode repl -work-type 2sql -start-file mysql-bin.000038 -start-datetime "2020-07-16 10:20:00" -output-dir ./tmpdir
分析在线binlog,生成回滚SQL
./my2sql -user root -password c123456 -host 127.0.0.1 -port 3357 -mode repl -work-type rollback -start-file mysql-bin.000038 -start-datetime "2020-07-16 10:20:00" -output-dir ./tmpdir
正向解析的SQL
root@ip-172-31-30-45:~/czg/tmpdir# cat forward.38.sql UPDATE `ceshi`.`t1` SET `sj`='2022-01-17 10:18:18.188' WHERE (`c1`='a' AND `c2`='b' AND `sj`='2022-01-17 09:41:22.451'); #解析的逆向SQL root@ip-172-31-30-45:~/czg/tmpdir# cat rollback.38.sql UPDATE `ceshi`.`t1` SET `sj`='2022-01-17 09:41:22.451' WHERE (`c1`='a' AND `c2`='b' AND `sj`='2022-01-17 10:18:18.188');
分析已经离线binlog,生成回滚SQL
./my2sql -user root -password c123456 -host 127.0.0.1 -port 3357 -mode file -work-type 2sql -local-binlog-file /usr/local/mysql57/data/mysql-bin.000038 -start-file /usr/local/mysql57/data/mysql-bin.000038 -start-datetime "2020-07-16 10:20:00" -output-dir ./tmpdir
解析时由于binlog内部的开始时间与结束时间采的是操作系统时区,
而binlog内容的timestamp类型存储的是UTC时间戳,在解析binlog时间范围,及binlog内容解析出来后,总会有与北京时间差8小的时问题,
用 -tl 参数可以解决这个问题,查询binlog的时间及生成的SQL都会是北京时间。
./my2sql -user root -password 123456 -host 127.0.0.1 -port 3306 -mode repl -work-type rollback -start-file mysql-bin.000008 -databases ceshi -tables t1 -start-datetime "2022-10-18 15:42:53" -stop-datetime "2022-10-18 15:45:00" -tl "Asia/Shanghai" -full-columns -output-dir ./tmpdir1
分析大事务、长事务:
统计时间范围各个表的DML操作数量,统计一个事务大于500条、时间大于300秒的事务
在线解析binlog
我在其中加入 tl 参数,指定时间时为北京时间就可以
./my2sql -user root -password xxx -host xxx.com -port 3306 -mode repl -work-type stats -start-file mysql-bin-changelog.169378 --stop-file mysql-bin-changelog.169379 -start-datetime "2023-02-09 10:20:00" -stop-datetime "2023-02-10 11:00:00" -big-trx-row-limit 500 -long-trx-seconds 300 -tl "Asia/Shanghai" -output-dir ./tmpdir
离线解析binlog,但还是需要连接数据库
./my2sql -user root -password xxx -host xxx.com -port 3306 -mode file -local-binlog-file ./mysql-bin-changelog.169378 -work-type stats -start-file mysql-bin-changelog.169378 -start-datetime "2023-02-09 10:20:00" -stop-datetime "2023-02-10 11:00:00" -big-trx-row-limit 500 -long-trx-seconds 3 -tl "Asia/Shanghai" -output-dir ./tmpdir