当前位置:网站首页>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 format series (1) -json
- Cess test online line! The first decentralized storage network to provide multiple application scenarios
- plsql查询数据乱码
- SqlServer 完全删除
- 【activiti】流程变量
- 波卡生态发展不设限的奥义——多维解读平行链
- 【vsphere高可用】主机故障切换
- Review the whole process of the 5th Polkadot Hackathon entrepreneurship competition, and uncover the secrets of the winning projects!
- Public chain Sui layer1 network
- 如何快速打通CRM系统和ERP系统,实现业务流程自动化流转
猜你喜欢
随机推荐
数据仓库与数仓建模
达梦数据库_DISQL下各种连接数据库的方法和执行SQL、脚本的方法
渗透测试知识---行业术语
【数据挖掘】零基础入门决策树
Function Closure
【数据挖掘】聚类分析的简要介绍
Analysis of Dao liquidity dual currency pledge mining development principle
Flink task, sub task, task slot and parallelism
多商户商城系统功能拆解14讲-平台端会员等级
Useref create dynamic reference
likeshop | 单商户商城系统代码开源无加密-PHP
达梦数据库_支持数据类型总结
Cess test online line! The first decentralized storage network to provide multiple application scenarios
Creation and generation of SVG format map in Heilongjiang Province
【mycat】mycat介绍
ntp错误: no server suitable for synchronization found
Unknown collation: ‘utf8mb4_0900_ai_ci‘的解决方法
开启Web3,曾经冷门的去中心化身份(DID)
@Async 没有异步执行
ERP+RPA 打通企业信息孤岛,企业效益加倍提升









