下载
git clone https://github.com/danfengcao/binlog2sql.git
原理
使用python连接到指定的库,读取要恢复表的表结构和对应的binlog日志,在binlog 为row格式并且DML记录所有字段值的情况下,将set 与where后字段值对换位置,拼接成的SQL就是回滚SQL。
安装
unzip binlog2sql-master.zip
cd binlog2sql-master/
pip install -r requirements.txt
误操作
mysql -uautomng -p’Automng_123′ -P3319
update sbtest7 set pad=’wa ka ka ‘ where id=3;
update sbtest8 set pad=’wa ka ka ‘ where id=3;
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000035
# python binlog2sql.py -h127.0.0.1 -P3319 -uautomng -p'Automng_123' -dtxdb -t sbtest7 sbtest8 --start-file='mysql-bin.000035' --start-datetime='2018-08-01 15:50:00' --stop-datetime='2018-08-01 16:01:00' UPDATE `txdb`.`sbtest7` SET `c`='39314051591-30329702885-35198042800-16393519874-56867884250-41805987558-63125675500-93376317385-90603675036-63992232134', `k`=149507, `pad`='wa ka ka', `id`=3 WHERE `c`='39314051591-30329702885-35198042800-16393519874-56867884250-41805987558-63125675500-93376317385-90603675036-63992232134' AND `k`=149507 AND `pad`='90728107484-01984250703-04244069858-02683578329-82506775849' AND `id`=3 LIMIT 1; #start 4 end 758 time 2018-08-01 16:00:12 UPDATE `txdb`.`sbtest8` SET `c`='22165230283-89382372870-64352117725-29359509089-24558560067-39564369546-34463527363-05997343623-26127428609-19766153460', `k`=202756, `pad`='wa ka ka', `id`=3 WHERE `c`='22165230283-89382372870-64352117725-29359509089-24558560067-39564369546-34463527363-05997343623-26127428609-19766153460' AND `k`=202756 AND `pad`='54879921302-82844293345-80647833951-55849410697-97809519145' AND `id`=3 LIMIT 1; #start 785 end 1393 time 2018-08-01 16:00:21 #start 4 end 758 time 2018-08-01 16:00:12 #start 785 end 1393 time 2018-08-01 16:00:21 # python binlog2sql.py --flashback -h127.0.0.1 -P3319 -uautomng -p'Automng_123' -dtxdb -t sbtest7 sbtest8 --start-file='mysql-bin.000035' --start-position=4 --stop-position=1393 UPDATE `txdb`.`sbtest8` SET `c`='22165230283-89382372870-64352117725-29359509089-24558560067-39564369546-34463527363-05997343623-26127428609-19766153460', `k`=202756, `pad`='54879921302-82844293345-80647833951-55849410697-97809519145', `id`=3 WHERE `c`='22165230283-89382372870-64352117725-29359509089-24558560067-39564369546-34463527363-05997343623-26127428609-19766153460' AND `k`=202756 AND `pad`='wa ka ka' AND `id`=3 LIMIT 1; #start 785 end 1393 time 2018-08-01 16:00:21 UPDATE `txdb`.`sbtest7` SET `c`='39314051591-30329702885-35198042800-16393519874-56867884250-41805987558-63125675500-93376317385-90603675036-63992232134', `k`=149507, `pad`='90728107484-01984250703-04244069858-02683578329-82506775849', `id`=3 WHERE `c`='39314051591-30329702885-35198042800-16393519874-56867884250-41805987558-63125675500-93376317385-90603675036-63992232134' AND `k`=149507 AND `pad`='wa ka ka' AND `id`=3 LIMIT 1; #start 4 end 758 time 2018-08-01 16:00:12
回滚
python binlog2sql.py –flashback -h127.0.0.1 -P3319 -uautomng -p’Automng_123′ -dtxdb -t sbtest7 sbtest8 –start-file=’mysql-bin.000035′ –start-position=4 –stop-position=1393 > /tmp/rollback.sql | cat
事务号一定要看清楚并写对了,如果……如果不小心写错了一位数,你可以想象一下后果……
mysql -uautomng -p’Automng_123′ -P3319 txdb < /tmp/rollback.sql
在尝试通过远程的方式操作时,失败了,不知是不支持远程还是哪里遇到了问题,正在排查中……如果不能通过远程的方式操作,那么就需要提前在服务器上安装这个脚本。
转载于:https://www.cnblogs.com/perfei/p/9402477.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/101511.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...