在日常运维工作中,对于mysql数据库的备份是至关重要的!数据库对于网站的重要性使得我们对mysql数据的管理不容有失!
然后,是人总难免会犯错误,说不定哪天大脑短路了来个误操作把数据库给删除了,怎么办???
下面,就mysql数据库误删除后的恢复方案进行说明。
一、工作场景
(1)MySQL数据库每晚12:00自动完全备份。
(2)某天早上上班,9点的时候,一同事犯晕drop了一个数据库!
(3)需要紧急恢复!可利用备份的数据文件以及增量的binlog文件进行数据恢复。
二、数据恢复思路
(1)利用全备的sql文件中记录的CHANGE MASTER语句,binlog文件及其位置点信息,找出binlog文件中增量的那部分。
(2)用mysqlbinlog命令将上述的binlog文件导出为sql文件,并剔除其中的drop语句。
(3)通过全备文件和增量binlog文件的导出sql文件,就可以恢复到完整的数据。
三、实例说明

在/etc/my.cnf文件里的[mysqld]区块添加:
log-bin=mysql-bin
然后重启mysql服务
(1)建库建表
mysql> create database ops;
Query OK, 1 row affected (0.11 sec)
​
mysql> use ops 
Database changed
mysql> create table customers(
    -> id int not null auto_increment,
    -> name char(20) not null,
    -> age int not null,
    -> primary key(id)
    -> ) engine=innoDB;
Query OK, 0 rows affected (0.12 sec)
​
mysql> show tables;
+---------------+
| Tables_in_ops |
+---------------+
| customers    |
+---------------+
1 row in set (0.00 sec)
​
​
mysql> desc customers;
+-------+----------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO  | PRI | NULL    | auto_increment |
| name  | char(20) | NO  |    | NULL    |                |
| age  | int(11)  | NO  |    | NULL    |                |
+-------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
​
mysql> 
插入数据
mysql> insert into customers values(1,"wangbo",24);
Query OK, 1 row affected (0.03 sec)
​
mysql> insert into customers values(2,"xiaoli",25);
Query OK, 1 row affected (0.01 sec)
mysql> insert into customers values(3,"lida",26);
Query OK, 1 row affected (0.01 sec)
mysql> select * from customers;
+----+--------+-----+
| id | name  | age |
+----+--------+-----+
|  1 | wangbo |  24 |
|  2 | xiaoli |  25 |
|  3 | lida  |  26 |
+----+--------+-----+
3 rows in set (0.00 sec)
mysql> 
(2)进行全备
[root@db02 opt]# mysqldump -B -F -A  --master-data=2 >/opt/ops_$(date +%F).sql
(3)在插入数据
mysql> insert into customers values(4,"bapbao",20);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into customers values(5,"paoap",20);
Query OK, 1 row affected (0.00 sec)
​
mysql> insert into customers values(6,"ppoiu",18);
Query OK, 1 row affected (0.01 sec)
​
mysql> select * from customers;
+----+--------+-----+
| id | name  | age |
+----+--------+-----+
|  1 | wangbo |  24 |
|  2 | xiaoli |  25 |
|  3 | lida  |  26 |
|  4 | bapbao |  20 |
|  5 | paoap  |  20 |
|  6 | ppoiu  |  18 |
+----+--------+-----+
6 rows in set (0.00 sec)
​
mysql> 
(4)删除数据库
mysql> drop database ops;
Query OK, 1 row affected (0.10 sec)
(5)恢复数据库
mysql> source /opt/ops_2017-12-04.sql
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy             |
| ops                |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)
mysql> select * from ops.customers;
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
|  1 | wangbo |  24 |
|  2 | xiaoli |  25 |
|  3 | lida   |  26 |
+----+--------+-----+
3 rows in set (0.00 sec)
mysql>