当前位置:网站首页>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)
边栏推荐
- Signal and slot mechanism of QT learning
- Redis系列4:高可用之Sentinel(哨兵模式)
- Epoll horizontal departure, which edge triggers
- Tcp/ip related
- [learn slam from scratch] publish the coordinate system transformation relationship to topic TF
- Interesting kotlin 0x07:composition
- Fx3 development board and schematic diagram
- Ruoyi's solution to error reporting after integrating flyway
- 传英伟达已与软银展开会谈,将出价超过320亿美元收购Arm
- 在AD中添加差分对及连线
猜你喜欢

Tcp/ip related

微软:Edge 浏览器已内置磁盘缓存压缩技术,可节省空间占用且不降低系统性能

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

【深度学习】:《PyTorch入门到项目实战》第五天:从0到1实现Softmax回归(含源码)

TCP handshake, waving, time wait connection reset and other records

【深度学习】:《PyTorch入门到项目实战》第六天:多层感知机(含代码)

Ruoyi's solution to error reporting after integrating flyway

【深度学习】:《PyTorch入门到项目实战》第七天之模型评估和选择(上):欠拟合和过拟合(含源码)

大学生参加六星教育PHP培训,找到了薪水远超同龄人的工作

阿里大哥教你如何正确认识关于标准IO缓冲区的问题
随机推荐
记录开发问题
leetcode70假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
parseJson
Debugging methods of USB products (fx3, ccg3pa)
Redis系列4:高可用之Sentinel(哨兵模式)
Given positive integers n and m, both between 1 and 10 ^ 9, n < = m, find out how many numbers have even digits between them (including N and m)
go语言慢速入门——流程控制语句
First day of QT study
快速掌握 Kotlin 集合函数
Leetcode647. Palindrome substring
asmlinkage的理解
Text filtering skills
Design direction of daily development plan
【从零开始学习SLAM】将坐标系变换关系发布到 topic tf
TCP handshake, waving, time wait connection reset and other records
Applet: scroll view slides to the bottom by default
Best cow fences solution
MySQL安装教程
Introduction and implementation of stack (detailed explanation)
深入理解 DeepSea 和 Salt 部署工具 – Storage6