Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > MySQL误删恢复

MySQL数据库误删恢复的超详细教程

作者:你才是臭弟弟

MySQL误删数据库,造成了数据的丢失,这是非常尴尬的,但是有许多方案可以用来尝试恢复丢失的数据库,这篇文章主要给大家介绍了关于MySQL数据库误删恢复的超详细教程,需要的朋友可以参考下

前言

经常听说删库跑路这真的不只是一句玩笑话,若不小心删除了数据库,事情很严重。你一个不小心可能会给公司删没。建议研发不要直连生成环境,一般的话都会分配账号权限,生产环境的账号尽量是只读,以防你一个不经意给库或表删除。一定要备份,这很重要,这是一个血的教训。

1、创建表

DROP TABLE IF EXISTS `user_misjudge`; --如果表存在则删除,然后执行如下语句重新创建。
CREATE TABLE `user_misjudge`  (
   `id` bigint(50) NOT NULL AUTO_INCREMENT,
   `attr1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
   `attr2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
   `attr3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
   `attr4` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
   `attr5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
   PRIMARY KEY (`id`) USING BTREE
 ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

2、BigLog日志相关 

2.1、检查biglog状态是否开启 

声明: 当前为mysql版本5.7  当前为mysql版本5.7  当前为mysql版本5.7 

2.1.1、Navicat工具执行

SHOW VARIABLES LIKE 'LOG_BIN%';

2.1.2、命令行执行

mysql -hlocalhost -uroot -proot

2.2、开启biglog

SET GLOBAL log_bin = 'ON';

执行结果:

注意: 报错了! 报错了! 报错不可怕,可怕的是报错没有征兆。如果要永久修改log_bin的值,需要修改MySQL的配置文件(my.cnf或my.ini),并重启MySQL服务器使修改生效。( 只读变量,不能使用set修改,只能通过修改my.cnf或my.ini文件再重启生效 )

# 开启BigLog用于备份和恢复数据库,以及进行数据复制等操作。
log-bin=mysql-bin  
# 实例都必须有一个唯一的 server-id 如:server-id=1 ,以便 MySQL 集群中的各个节点能够相互识别和通信。
server-id=1 

2.3、查看biglog日志文件

show binary logs;

2.3.1、查看master状态

show master status;

 2.3.2、查看第一个binlog文件内容

show binlog events;

 2.3.3、查看指定binlog文件的内容

show binlog events in 'mysql-bin.000002';

注意: 上一个事件的结束位置,就是下一个事件的开始位置。如下↓↓↓

2.3.4、刷新log日志

flush logs;

2.3.5、删除日志文件

MySQL删除日志的方式有以下几种:

show binary logs;

purge master logs to 'mysql-bin.000001';

purge binary logs before '2023-5-29 23:59:59';

 reset master;

2.3.6、查看和修改日志文件有效期

 说明:

show variables like '%expire_logs_days%';

set global expire_logs_days=7;

3、误删恢复

3.1、查看数据库

show databases;

3.2、查看表中的数据

 --切换到指定数据库。
use text; 
--显示当前数据库中的所有表名。
show tables;
--查询表数据
select * from user_misjudge;

3.3、查看用户表相关操作日志

说明: show binary logs; 和 show master logs; 都是显示所有可用的binlog日志文件列表。

show master logs;

 注意: 可以看到我之前删除的表数据已经被记录了 ,由于之前演示删除日志,我的日志是不完整的不完整的日志是不能恢复的 。(开启日志后 重新创一个库 详情查看:3.4重新创建库) ↓↓↓

3.4、重新创建库

drop database text
 reset master;
 CREATE DATABASE `text` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'
show binlog events in 'mysql-bin.000001';

3.4、删除数据库 text 

 drop database text;

可以看到列表text库已经被删除 

3.5、恢复数据库

3.5.1、查看日志文件中的信息

show binlog events in 'mysql-bin.000001';

3.5.2、利用事件开始结束位置进行恢复

mysqlbinlog --start-position=154 --stop-position=427 mysql-bin.000001 | mysql -uroot -p

以上该命令是一个从MySQL二进制日志文件中提取数据并导入到MySQL数据库的命令。具体解释如下:

3.5.3、查看mysql-bin.000001文件日志细节

说明

mysqlbinlog "mysql-bin.000001">"xj.sql"

3.5.4、利用事件时间节点进行恢复

mysqlbinlog --start-datetime="2023-06-01 11:32:34" --stop-datetime="2023-06-01 11:47:46" mysql-bin.000001 | mysql -uroot -p

3.6、恢复数据库表

3.6.1、进入库

3.6.2、库下创建表

3.6.3、库下的表中添加数据

insert user_misjudge(attr1) values('CSDN臭弟弟');
insert user_misjudge(attr1) values('这是一条数据');

3.6.4、删表之间查看日志信息

3.6.4、删除表

drop tables user_misjudge;

3.6.5、恢复表

mysqlbinlog --start-position=720 --stop-position=1579 mysql-bin.000001 | mysql -uroot -p

3.6.7、查看恢复的表

 show binlog events in 'mysql-bin.000001';

 可以看到 我们只恢复了表 并没有恢复数据 。为什么???  往下继续↓↓

3.6.8、分析日志

原因:

       在导航{3.6.5、恢复表} 我们执行的语句mysqlbinlog --start-position=720 --stop-position=1579 mysql-bin.000001 | mysql -uroot -p, 事件开始位置720 ,事件结束位置1579,说明我们位置不对呗,这个位置只能恢复表不能恢复数据。(想要恢复数据应该在 事件的结束位置应该在 删除表之前的最后连接的位置才对)

mysql> show binlog events in 'mysql-bin.000001';
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| mysql-bin.000001 |    4 | Format_desc    |         1 |         123 | Server ver: 5.7.28-log, Binlog ver: 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 |  123 | Previous_gtids |         1 |         154 |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| mysql-bin.000001 |  154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 |  219 | Query          |         1 |         362 | CREATE DATABASE `text` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 |  362 | Anonymous_Gtid |         1 |         427 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 |  427 | Query          |         1 |         512 | drop database text                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| mysql-bin.000001 |  512 | Anonymous_Gtid |         1 |         577 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 |  577 | Query          |         1 |         720 | CREATE DATABASE `text` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 |  720 | Anonymous_Gtid |         1 |         785 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 |  785 | Query          |         1 |        1532 | use `text`; CREATE TABLE `user_misjudge`  (   `id` bigint(50) NOT NULL AUTO_INCREMENT,   `attr1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr4` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic |
| mysql-bin.000001 | 1532 | Anonymous_Gtid |         1 |        1597 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 | 1597 | Query          |         1 |        1669 | BEGIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 | 1669 | Table_map      |         1 |        1740 | table_id: 163 (text.user_misjudge)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| mysql-bin.000001 | 1740 | Write_rows     |         1 |        1799 | table_id: 163 flags: STMT_END_F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| mysql-bin.000001 | 1799 | Xid            |         1 |        1830 | COMMIT /* xid=2016 */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 | 1830 | Anonymous_Gtid |         1 |        1895 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 | 1895 | Query          |         1 |        1967 | BEGIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 | 1967 | Table_map      |         1 |        2038 | table_id: 163 (text.user_misjudge)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| mysql-bin.000001 | 2038 | Write_rows     |         1 |        2102 | table_id: 163 flags: STMT_END_F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| mysql-bin.000001 | 2102 | Xid            |         1 |        2133 | COMMIT /* xid=2017 */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 | 2133 | Anonymous_Gtid |         1 |        2198 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 | 2198 | Query          |         1 |        2324 | use `text`; DROP TABLE `user_misjudge` /* generated by server */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| mysql-bin.000001 | 2324 | Anonymous_Gtid |         1 |        2389 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 | 2389 | Query          |         1 |        3136 | use `text`; CREATE TABLE `user_misjudge`  (   `id` bigint(50) NOT NULL AUTO_INCREMENT,   `attr1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr4` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic |
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
24 rows in set (0.00 sec)

创建表的起始位置是720 ,在日志中删除表的结束之前最后连接的位置是2190,这样就可以恢复我们删表之前的表和两条数据。

3.6.9、恢复表中数据

mysql> show binlog events in 'mysql-bin.000001';
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| mysql-bin.000001 |    4 | Format_desc    |         1 |         123 | Server ver: 5.7.28-log, Binlog ver: 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 |  123 | Previous_gtids |         1 |         154 |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| mysql-bin.000001 |  154 | Anonymous_Gtid |         1 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 |  219 | Query          |         1 |         362 | CREATE DATABASE `text` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 |  362 | Anonymous_Gtid |         1 |         427 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 |  427 | Query          |         1 |         512 | drop database text                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| mysql-bin.000001 |  512 | Anonymous_Gtid |         1 |         577 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 |  577 | Query          |         1 |         720 | CREATE DATABASE `text` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 |  720 | Anonymous_Gtid |         1 |         785 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 |  785 | Query          |         1 |        1532 | use `text`; CREATE TABLE `user_misjudge`  (   `id` bigint(50) NOT NULL AUTO_INCREMENT,   `attr1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr4` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic |
| mysql-bin.000001 | 1532 | Anonymous_Gtid |         1 |        1597 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 | 1597 | Query          |         1 |        1669 | BEGIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 | 1669 | Table_map      |         1 |        1740 | table_id: 163 (text.user_misjudge)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| mysql-bin.000001 | 1740 | Write_rows     |         1 |        1799 | table_id: 163 flags: STMT_END_F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| mysql-bin.000001 | 1799 | Xid            |         1 |        1830 | COMMIT /* xid=2016 */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 | 1830 | Anonymous_Gtid |         1 |        1895 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 | 1895 | Query          |         1 |        1967 | BEGIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 | 1967 | Table_map      |         1 |        2038 | table_id: 163 (text.user_misjudge)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| mysql-bin.000001 | 2038 | Write_rows     |         1 |        2102 | table_id: 163 flags: STMT_END_F                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| mysql-bin.000001 | 2102 | Xid            |         1 |        2133 | COMMIT /* xid=2017 */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| mysql-bin.000001 | 2133 | Anonymous_Gtid |         1 |        2198 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 | 2198 | Query          |         1 |        2324 | use `text`; DROP TABLE `user_misjudge` /* generated by server */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| mysql-bin.000001 | 2324 | Anonymous_Gtid |         1 |        2389 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| mysql-bin.000001 | 2389 | Query          |         1 |        3136 | use `text`; CREATE TABLE `user_misjudge`  (   `id` bigint(50) NOT NULL AUTO_INCREMENT,   `attr1` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr2` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr3` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr4` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   `attr5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,   PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic |
+------------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
24 rows in set (0.00 sec)

mysql>
mysqlbinlog --start-position=1532 --stop-position=2189 mysql-bin.000001 | mysql -uroot -p

总结 

到此这篇关于MySQL数据库误删恢复的文章就介绍到这了,更多相关MySQL误删恢复内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文