MySQL误删库恢复实战

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL误删库恢复实战相关的知识,希望对你有一定的参考价值。

创建测试库、表

create database test;
use test;
create table  leo (id int,name varchar(10));

插入数据

insert into leo values (1,"liufeng");
insert into leo values (2,"zhangsan");
insert into leo values (3,"liufeng");
insert into leo values (4,"zhangsan");
mysql> select * from leo;
+------+----------+
| id   | name     |
+------+----------+
|    1 | liufeng  |
|    2 | zhangsan |
|    3 | liufeng  |
|    4 | zhangsan |
+------+----------+
.......

全备数据库

#!/bin/sh
bak_path=/home/mysql/backup
file_name=bak_$(date +%F)
if  [ ! -e  $bak_path  ];then
 mkdir  -p  $bak_path
fi
 mysqldump -uroot -proot -S /home/mysql/3306/data/mysql.sock -A --single-transaction --master-data=2  >  $bak_path/$file_name.sql
md5sum $bak_path/$file_name.sql > $bak_path/$file_name.flag
find $BakPath  -name "*.sql.gz"  -mtime +7|xargs rm -f

确认备份状态

[[email protected] home]# cd mysql/backup/
[[email protected] backup]# ll
total 792
-rw-r--r-- 1 root root     72 Jun 28 22:18 bak_2019-06-28.flag
-rw-r--r-- 1 root root 804496 Jun 28 22:18 bak_2019-06-28.sql
[[email protected] backup]# more bak_2019-06-28.flag 
db297e95d491ae3b85ed2b5d2496e527  /home/mysql/backup/bak_2019-06-28.sql
[[email protected] backup]#  cd ..
[[email protected] mysql]# 
[[email protected] mysql]# md5sum -c /home/mysql/backup/bak_2019-06-28.flag
/home/mysql/backup/bak_2019-06-28.sql: OK

插入数据

mysql> insert into leo values (5,"liufeng");
Query OK, 1 row affected (0.01 sec)

mysql> insert into leo values (6,"zhangsan");
Query OK, 1 row affected (0.01 sec)

mysql> select * from leo;
+------+----------+
| id   | name     |
+------+----------+
|    1 | liufeng  |
|    2 | zhangsan |
|    3 | liufeng  |
|    4 | zhangsan |
|    5 | liufeng  |
|    6 | zhangsan |
+------+----------+

删除数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| leo                |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database test;
Query OK, 0 rows affected (0.01 sec)
mysql>  drop database test;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

备份bin—log

[[email protected] backup]# cd /home/mysql/3306/data/
[[email protected] data]# ll
total 123740
-rw-r-----. 1 mysql mysql       56 Jun 18 00:59 auto.cnf
-rw-r-----  1 mysql mysql      456 Jun 28 17:41 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jun 29 02:45 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Jun 29 02:45 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jun 18 00:59 ib_logfile1
-rw-r-----  1 mysql mysql 12582912 Jun 29 02:42 ibtmp1
drwxr-x---. 2 mysql mysql     4096 Jun 25 23:09 mysql
-rw-r-----  1 mysql mysql      177 Jun 25 22:54 mysql-bin.000003
-rw-r-----  1 mysql mysql   815245 Jun 25 23:17 mysql-bin.000004
-rw-r-----  1 mysql mysql      642 Jun 28 17:41 mysql-bin.000005
-rw-r-----  1 mysql mysql     5909 Jun 29 02:45 mysql-bin.000006
-rw-r-----  1 mysql mysql       76 Jun 28 19:36 mysql-bin.index
srwxrwxrwx  1 mysql mysql        0 Jun 28 19:36 mysql.sock
-rw-------  1 mysql mysql        5 Jun 28 19:36 mysql.sock.lock
drwxr-x---. 2 mysql mysql     4096 Jun 18 00:59 performance_schema
-rw-r-----  1 mysql mysql        5 Jun 28 19:36 resourcepool-0559.pid
drwxr-x---. 2 mysql mysql    12288 Jun 18 00:59 sys
[[email protected] data]# cp -a mysql-bin.* /home/mysql/backup/
[[email protected] data]# ll /home/mysql/backup/
total 1612
-rw-r--r-- 1 root  root      72 Jun 29 02:42 bak_2019-06-29.flag
-rw-r--r-- 1 root  root  804515 Jun 29 02:42 bak_2019-06-29.sql
-rw-r----- 1 mysql mysql    177 Jun 25 22:54 mysql-bin.000003
-rw-r----- 1 mysql mysql 815245 Jun 25 23:17 mysql-bin.000004
-rw-r----- 1 mysql mysql    642 Jun 28 17:41 mysql-bin.000005
-rw-r----- 1 mysql mysql   5909 Jun 29 02:45 mysql-bin.000006
-rw-r----- 1 mysql mysql     76 Jun 28 19:36 mysql-bin.index

恢复步骤
1、停止数据库对外访问,防止数据库因update导致数据破坏
2、由于备份语句中加入--master-data=2,可以记录全备后的binlog对应恢复点

[[email protected] data]# cd /home/mysql/backup/
[[email protected] backup]# ll
total 1612
-rw-r--r-- 1 root  root      72 Jun 29 02:42 bak_2019-06-29.flag
-rw-r--r-- 1 root  root  804515 Jun 29 02:42 bak_2019-06-29.sql
-rw-r----- 1 mysql mysql    177 Jun 25 22:54 mysql-bin.000003
-rw-r----- 1 mysql mysql 815245 Jun 25 23:17 mysql-bin.000004
-rw-r----- 1 mysql mysql    642 Jun 28 17:41 mysql-bin.000005
-rw-r----- 1 mysql mysql   5909 Jun 29 02:45 mysql-bin.000006
-rw-r----- 1 mysql mysql     76 Jun 28 19:36 mysql-bin.index
[[email protected] backup]# sed -n ‘22p‘ bak_2019-06-29.sql 
-- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000006‘, MASTER_LOG_POS=5181;

3、将binlog转换SQL

[[email protected] backup]# mysqlbinlog -d test mysql-bin.000006  --start-position=5181 -r test.sql
[[email protected] backup]# cat test.sql 

4、实际环境中后续还有 mysql-bin.000007 .... 需要陆续转换为SQL
5、删除test.sql中drop语句

[[email protected] backup]# grep  -w drop test.sql 
drop database test
[[email protected] backup]# sed -i ‘/drop database test/d‘ test.sql 
[[email protected] backup]# grep  -w drop test.sql 

6、全备恢复数据库

[[email protected] backup]# mysql -u root -p -S /home/mysql/3306/data/mysql.sock < bak_2019-06-29.sql 
Enter password: 
[[email protected] backup]# mysql -u root -p -S /home/mysql/3306/data/mysql.sock -e "select * from test.leo";
Enter password: 
+------+----------+
| id   | name     |
+------+----------+
|    1 | liufeng  |
|    2 | zhangsan |
|    3 | liufeng  |
|    4 | zhangsan |
+------+----------+

7、恢复binlog

[[email protected] backup]# mysql -u root -p -S /home/mysql/3306/data/mysql.sock < test.sql
Enter password: 
[[email protected] backup]# mysql -u root -p -S /home/mysql/3306/data/mysql.sock -e "select * from test.leo";
Enter password: 
+------+----------+
| id   | name     |
+------+----------+
|    1 | liufeng  |
|    2 | zhangsan |
|    3 | liufeng  |
|    4 | zhangsan |
|    5 | liufeng  |
|    6 | zhangsan |
+------+----------+

8、校验数据,恢复数据库对外访问

以上是关于MySQL误删库恢复实战的主要内容,如果未能解决你的问题,请参考以下文章

删库不跑路:我含泪写下了 MySQL 数据恢复大法…

删库不跑路:我含泪写下了 MySQL 数据恢复大法…

恢复mysql数据库误删数据

删库不跑路!我含泪写下了 MySQL 数据恢复大法…

删库不跑路!我含泪写下了 MySQL 数据恢复大法…

顺丰删库工程师遭开除,难道他不会恢复误删数据?