当前位置:网站首页>How to quickly recover data after MySQL misoperation
How to quickly recover data after MySQL misoperation
2022-07-24 05:43:00 【My benefits remain the same】
Original address :MySQL How to recover data quickly after misoperation
Basically every programmer who works with a database ( Of course, it could be your colleagues ) There's a problem ,MySQL How to quickly roll back after misoperation ? such as ,delete A watch , Forget the restrictions , The whole table is gone . If this is still the core business data of online environment , That's a big deal . After misoperation , It is very important to rollback data quickly .
binlog2sql Fast rollback
First , Confirm your MySQL server Open the binlog, The following parameters are set (mysql Installation directory my.ini):
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
max_binlog_size = 1000M
binlog-format = rowIf it's not on binlog, There is no pre generated rollback SQL, That really can't roll back quickly . To store important business data MySQL, It is strongly recommended to turn on binlog.
And then , Install open source tools binlog2sql.binlog2sql It's easy to use binlog Parsing tool , One of the functions is to generate rollback SQL.
git clone https://github.com/danfengcao/binlog2sql.git
pip install -r requirements.txtNot installed git You can directly visit the address to download
pip Need to install python Environmental Science
then , We can generate rollback SQL 了 .
background : Deleted by mistake test library tbl Data of the whole table , Emergency rollback is required .
test library tbl Table original data
mysql> select * from tbl;
+----+--------+---------------------+
| id | name | addtime |
+----+--------+---------------------+
| 1 | Xiao zhao | 2016-12-10 00:04:33 |
| 2 | penny | 2016-12-10 00:04:48 |
| 3 | Little grandson | 2016-12-10 00:04:51 |
| 4 | petty thief | 2016-12-10 00:04:56 |
+----+--------+---------------------+
4 rows in set (0.00 sec)
mysql> delete from tbl;
Query OK, 4 rows affected (0.00 sec)
tbl The watch is empty
mysql> select * from tbl;
Empty set (0.00 sec)Recovery steps :
Sign in mysql, Check out the current binlog file
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000046 | 12262268 |
| mysql-bin.000047 | 3583 |
+------------------+-----------+Abreast of the times binlog File is mysql-bin.000047, We re locate the misoperation SQL Of binlog Location
$ python binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -ttbl --start-file='mysql-bin.000047'
Output :
DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-10 00:04:33' AND `id`=1 AND `name`=' Xiao zhao ' LIMIT 1; #start 3346 end 3556
DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-10 00:04:48' AND `id`=2 AND `name`=' penny ' LIMIT 1; #start 3346 end 3556
DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-10 00:04:51' AND `id`=3 AND `name`=' Little grandson ' LIMIT 1; #start 3346 end 3556
DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-10 00:04:56' AND `id`=4 AND `name`=' petty thief ' LIMIT 1; #start 3346 end 3556Generate rollback sql, And check rollback sql Whether it is right
$ python binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -ttbl --start-file='mysql-bin.000047' --start-pos=3346 --end-pos=3556 -B
Output :
INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-10 00:04:56', 4, ' petty thief '); #start 3346 end 3556
INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-10 00:04:51', 3, ' Little grandson '); #start 3346 end 3556
INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-10 00:04:48', 2, ' penny '); #start 3346 end 3556
INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-10 00:04:33', 1, ' Xiao zhao '); #start 3346 end 3556Confirm rollback sql correct , Execute rollback statement . Sign in mysql confirm , Data rollback succeeded
$ python binlog2sql.py -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -ttbl --start-file='mysql-bin.000047' --start-pos=3346 --end-pos=3556 -B | mysql -h127.0.0.1 -P3306 -uadmin -p'admin'
mysql> select * from tbl;
+----+--------+---------------------+
| id | name | addtime |
+----+--------+---------------------+
| 1 | Xiao zhao | 2016-12-10 00:04:33 |
| 2 | penny | 2016-12-10 00:04:48 |
| 3 | Little grandson | 2016-12-10 00:04:51 |
| 4 | petty thief | 2016-12-10 00:04:56 |
+----+--------+---------------------+
thus , Don't worry about getting fired anymore .
common problem
Someone will ask. , I DDL How to roll back quickly if you misoperate ? such as drop A big watch .
It's hard to do . Because even in row In mode ,DDL The operation will not record the changes of each row of data to binlog, therefore DDL Unable to get binlog Roll back . Realization DDL Roll back , Must be executed in DDL Back up old data first . Someone did modify mysql server The source code realizes DDL Fast rollback , I found Ali's xiaobin lin Submitted a patch. But as far as I know , Few domestic Internet companies have applied this feature . If the reason is , I think the main thing is to be lazy , There is no need to do this low-frequency function , The secondary reason is that some additional storage will be added .
therefore ,DDL In case of misoperation, it can only be recovered through backup . If the company can't even use backup , I really suggest buying a plane ticket . Why? ? Run
mysql except binlog2sql, Are there any other rollback tools ?
Of course. . Ali Peng Lixun is right mysqlbinlog Added flashback Characteristics of , This should be mysql The earliest flashback function , What Peng solved is DML Roll back of , And explains the use of binlog Conduct DML Flashback design ideas .DDL The rollback feature is also proposed and implemented by Alibaba team . These two functions are innovative , The flashback tools that have appeared since then are basically imitations of the above two . in addition , Where to open source Inception It's a set MySQL Automatic operation and maintenance tools , This is heavier , Support DML Roll back , Not yet from binlog Rolled back , Is rolled back from backup , Also support DDL Rollback table structure , Data cannot be rolled back ~
边栏推荐
猜你喜欢

Flink sql-client.sh使用

Penetration testing knowledge - industry terminology

【mycat】mycat分库分表

Canal+kafka实战(监听mysql binlog实现数据同步)

【activiti】activiti入门
![利用流媒体将RSTP流转成WEB端播放(二)[可回看]](/img/b9/2c0e6eb19acaa2356ff49f6e272826.png)
利用流媒体将RSTP流转成WEB端播放(二)[可回看]
![Brief introduction of [data mining] cluster analysis](/img/9b/3484cf1353686d38dcf32e845b1903.jpg)
Brief introduction of [data mining] cluster analysis

likeshop单商户SAAS商城系统搭建,代码开源无加密。

Node connects to MySQL and uses Navicat for visualization

likeshop | 单商户商城系统代码开源无加密-PHP
随机推荐
Canvas - rotate
[data mining] zero foundation entry decision tree
Tree structure + node
读《悟道:一位IT高管20年的职场心经》
【activiti】流程实例
【vsphere高可用】主机和虚拟机故障监测工作原理
Vulnhub solidstate: 1 target penetration test
[vSphere high availability] working principle of host and virtual machine fault monitoring
达梦数据库_逻辑架构基础
Authorized access to MySQL database
【数据挖掘】零基础入门决策树
Flink format series (1) -json
助力传统游戏转型GameFi,Web3Games推动游戏发展新航向
Creation and generation of SVG format map in Heilongjiang Province
highcharts使用自定义矢量地图
Interpretation of the randomness of POS mechanism, how does poca's randomness principle work?
Substrate 技术及生态5月大事记 | Square One 计划启动,波卡上线 XCM!
Flink函数(2):CheckpointedFunction
Cess test online line! The first decentralized storage network to provide multiple application scenarios
Insanity:1 (insanity hosting) target penetration vulnhub