当前位置:网站首页>Technology sharing | how to recover the erroneously deleted table and the data in the table?
Technology sharing | how to recover the erroneously deleted table and the data in the table?
2022-07-28 16:58:00 【ActionTech】
author : Yang Xiaoyun
Aikesheng Database Engineer , be responsible for MySQL Routine maintenance and DMP Product support . Good at mysql Fault handling .
In this paper, the source : Original contribution
* Produced by aikesheng open source community , Original content is not allowed to be used without authorization , For reprint, please contact the editor and indicate the source .
scene :
The customer deleted a table by mistake , therefore Want to recover the data of a table , Expect to restore the data before deletion .
Premise :
The recovery method of deleting a table by mistake in the database , The method described below is for the database with backup every day and opening binlog The log .
explain : The test library in this article is test database , The test table is test in student surface .
One 、 Turn on binlog journal , And back up the data
1. Check whether the database is open binlog journal

If it's not on , You need the following methods to start
(1) stay linux Under the system , modify /etc/my.cnf file
# Edit mode enters /etc/my.cnf
vi /etc/my.cnf
# i Start editing
# stay #log bin Add the following
server_id=2
log_bin = mysql-bin
binlog_format = ROW
expire_logs_days = 30
log_bin_basename= / Database instance installation directory /log/binlog/ Port number /mysql-bin
log_bin_index=/ Database instance installation directory /log/binlog/ Port number /mysql-bin.index
# esc Exit the editor ,shift+: preservation
(2) restart mysql service
systemctl restart mysqld
It has been opened here binlog 了 , You can use the check command to check whether it is enabled
2. Check the data in the data table

3. The backup data
Backup command format :
mysqldump [ Options ] Database name [ Table name ] > Script name
mysqldump [ Options ] -- Database name [ Options Table name ] > Script name
mysqldump [ Options ] --all-databases [ Options ] > Script name
(1) Back up all databases
mysqldump -h10.186.63.4 -P4149 -uu1 [email protected] -all-databases > /test1.sql
(2) Backup single library ( Multiple libraries are separated by spaces )
mysqldump -h10.186.63.4 -P4149 -uu1 [email protected] database > test2.sql
(3) Backup list ( Multiple tables are spaced with spaces )
mysqldump -h10.186.63.4 -P4149 -uu1 [email protected] database table > test3.sql
(4) Backup the specified database to exclude some tables
mysqldump -h10.186.63.4 -P4149 -uu1 [email protected] database --ignore-table=db.tb --ignore-table=db.tb2 > /test4.sql
4. Delete the database after inserting data
insert data , Generate binlog journal
mysql> insert into student values('201215130',' Zhang San ',' male ',21,'IS');
Query OK, 1 row affected (0.03 sec)
mysql> insert into student values('201215131',' Li Si ',' Woman ',20,'MA');
Query OK, 1 row affected (0.02 sec)
Delete database
mysql> drop database test;
Query OK, 1 row affected (0.10 sec)
Remember not to do anything at this time !!!
Two 、 Restore data
1. View the current binlog
mysql> show master status\G;
************************ 1. row ***************************
File: mysql-bin.000021
Position: 68403303
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 130df5fa-01c1-11ed-916a-02000aba3f04:1-446151
1 row in set (0.00 sec)
ERROR:
No query specified
notes :mysql-bin.000021 Files will be used as one of the sources for recovering deleted data
2. Copy binlog journal
Change the current binlog Copy logs to other directories , To avoid subsequent operations on binlog Logs have an impact
cp /test/data/mysql/log/binlog/4149/mysql-bin.000021 /root
3. Will convert binlog Log for sql
The command format is :mysqlbinlog -d database mysql-bin file > xx.sql
Such as :
/data/mysql/base/5.7.25/bin/mysqlbinlog -d test mysql-bin.000021 > 0021bin.sql
[[email protected] 4149]# /data/mysql/base/5.7.25/bin/mysqlbinlog -d test mysql-bin.000022 > 0022.sql
WARNING: The option --database has been used. It may filter parts of transactions, but will include the GTIDs in any case. If you want to exclude or include transactions, you should use the options --exclude-gtids or --include-gtids, respectively, instead.
Edit the converted sql file
vi 0021bin.sql
The inside Misoperation command (DROP command ) Delete all
Start restoring data after saving
4. Restore backup files
/data/mysql/base/5.7.25/bin/mysql -h10.186.63.4 -P4149 -uu1 [email protected] < test.sql
Check whether the database backup file is restored
mysql> show databases;
mysql> use test;
mysql> show tables;
mysql>select * from table;
5. Restore the data deleted after the backup
Comment out binlog Transformed sql This line in the file
SET @@GLOBAL.GTID_PURGED=
/*SET @@GLOBAL.GTID_PURGED=XXXX*/;
Specify the database where the deleted table is located , Import data
/data/mysql/base/5.7.25/bin/mysql -h10.186.63.4 -P4149 -uu1 [email protected] test < 0021bin.sql
View the recovered data
mysql> select * from test.student;
+-----------+-----------+------+------+-------+
| Sno | Sname | Ssex | Sage | Sdept |
+-----------+-----------+------+------+-------+
| 201215121 | Li Yong | male | 20 | CS |
| 201215122 | Liu Chen | Woman | 19 | CS |
| 201215123 | Wang min. | Woman | 18 | MA |
| 201215125 | Zhang Li | male | 19 | IS |
| 201215126 | Li Yiping | male | 18 | IS |
| 201215127 | Zhang Qin | Woman | 19 | CS |
| 201215128 | Wang Fang | Woman | 20 | MA |
| 201215129 | Huang Linlin | male | 21 | IS |
| 201215130 | Li Si | Woman | 20 | MA |
| 201215131 | Zhang San | male | 21 | IS |
+-----------+-----------+------+------+-------+
10 rows in set (0.00 sec)
边栏推荐
- MySQL CDC if the binlog log file is incomplete, can you read all the data in the full volume stage
- Quickly master kotlin set functions
- MySQL安装教程
- Re14:读论文 ILLSI Interpretable Low-Resource Legal Decision Making
- 【JS】1394- ES2022 的 8 个实用的新功能
- PostgreSQL每周新闻—2022年7月20日
- 大学生参加六星教育PHP培训,找到了薪水远超同龄人的工作
- Exercise note 5 (square of ordered array)
- 【深度学习】:《PyTorch入门到项目实战》第四天:从0到1实现logistic回归(附源码)
- Jsonarray traversal
猜你喜欢

Im im development optimization improves connection success rate, speed, etc

有趣的 Kotlin 0x07:Composition

Brother Ali teaches you how to correctly understand the problem of standard IO buffer

Leetcode daily practice - the number of digits in the offer 56 array of the sword finger

Alibaba cloud MSE supports go language traffic protection

Re11:读论文 EPM Legal Judgment Prediction via Event Extraction with Constraints

Ansa secondary development - two methods of drawing the middle surface

Microsoft: edge browser has built-in disk cache compression technology, which can save space and not reduce system performance

有趣的 Kotlin 0x09:Extensions are resolved statically

【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)
随机推荐
Re13: read the paper gender and racial stereotype detection in legal opinion word embeddings
【深度学习】:《PyTorch入门到项目实战》第五天:从0到1实现Softmax回归(含源码)
mysql cdc 如果binlog日志文件不全,全量阶段能读到所有数据吗
MySQL安装教程
Mysql与Oracle的13点区别
【深度学习】:《PyTorch入门到项目实战》第四天:从0到1实现logistic回归(附源码)
Analysis of echo service model in the first six chapters of unp
Understanding of asmlinkage
Alibaba cloud - Wulin headlines - site building expert competition
综合设计一个OPPE主页--页面服务部分
Debugging methods of USB products (fx3, ccg3pa)
PHP calculate coordinate distance
Leetcode learn to insert and sort unordered linked lists (detailed explanation)
【深度学习】:《PyTorch入门到项目实战》:简洁代码实现线性神经网络(附代码)
Some opinions on bug handling
Im im development optimization improves connection success rate, speed, etc
Interesting kotlin 0x08:what am I
Tcp/ip related
Oracle system composition
Design direction of daily development plan