在日常運維工作中,對于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文件,就可以恢復到完整的數據。
三、實例說明
----------------------------------------
首先,要確保mysql開啟了binlog日志功能
在/etc/my.cnf文件里的[mysqld]區塊添加:
log-bin=mysql-bin
然后重啟mysql服務
----------------------------------------
(1)在ops庫下創建一張表customers
| mysql> use ops;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.09 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.02 sec)mysql> insert into customers values(1,"wangbo","24");Query OK, 1 row affected (0.06 sec)mysql> insert into customers values(2,"guohui","22");Query OK, 1 row affected (0.06 sec)mysql> insert into customers values(3,"zhangheng","27");Query OK, 1 row affected (0.09 sec)mysql> select * from customers;+----+-----------+-----+| id | name | age |+----+-----------+-----+| 1 | wangbo | 24 || 2 | guohui | 22 || 3 | zhangheng | 27 |+----+-----------+-----+3 rows in set (0.00 sec) | 
(2)現在進行全備份
[root@vm-002 ~]# mysqldump -uroot -p -B -F -R -x --master-data=2 ops|gzip >/opt/backup/ops_$(date +%F).sql.gz
Enter password: 
[root@vm-002 ~]# ls /opt/backup/
ops_2016-09-25.sql.gz
-----------------
參數說明:
-B:指定數據庫
新聞熱點
疑難解答