自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL數(shù)據(jù)庫誤刪除后如何恢復(fù)?

數(shù)據(jù)庫 MySQL
在日常運(yùn)維工作中,對于數(shù)據(jù)庫的備份是至關(guān)重要的!數(shù)據(jù)庫對于網(wǎng)站的重要性使得我們對 MySQL 數(shù)據(jù)庫的管理不容有失!然而是人總難免會犯錯誤,說不定哪天大腦短路了,誤操作把數(shù)據(jù)庫給刪除了,怎么辦?

MySQL數(shù)據(jù)庫誤刪除后如何恢復(fù)?

在日常運(yùn)維工作中,對于數(shù)據(jù)庫的備份是至關(guān)重要的!數(shù)據(jù)庫對于網(wǎng)站的重要性使得我們對 MySQL 數(shù)據(jù)庫的管理不容有失!然而是人總難免會犯錯誤,說不定哪天大腦短路了,誤操作把數(shù)據(jù)庫給刪除了,怎么辦?

下面,就 MySQL 數(shù)據(jù)庫誤刪除后的恢復(fù)方案進(jìn)行說明。

一、工作場景

(1)MySQL數(shù)據(jù)庫每晚12:00自動完全備份。

(2)某天早上上班,9點(diǎn)的時候,一同事犯暈drop了一個數(shù)據(jù)庫!

(3)需要緊急恢復(fù)!可利用備份的數(shù)據(jù)文件以及增量的binlog文件進(jìn)行數(shù)據(jù)恢復(fù)。

二、數(shù)據(jù)恢復(fù)思路

(1)利用全備的sql文件中記錄的CHANGE MASTER語句,binlog文件及其位置點(diǎn)信息,找出binlog文件中增量的那部分。

(2)用mysqlbinlog命令將上述的binlog文件導(dǎo)出為sql文件,并剔除其中的drop語句。

(3)通過全備文件和增量binlog文件的導(dǎo)出sql文件,就可以恢復(fù)到完整的數(shù)據(jù)。

三、實(shí)例說明

首先,要確保mysql開啟了binlog日志功能。在/etc/my.cnf文件里的[mysqld]區(qū)塊添加:

 

  1. log-bin=mysql-bin 

然后重啟mysql服務(wù)

(1)在ops庫下創(chuàng)建一張表customers

 

  1. mysql> use ops;  
  2. mysql> create table customers(  
  3. -> id int not null auto_increment,  
  4. -> name char(20) not null 
  5. -> age int not null 
  6. -> primary key(id)  
  7. -> )engine=InnoDB;  
  8. Query OK, 0 rows affected (0.09 sec)   
  9.  
  10. mysql> show tables;  
  11. +---------------+  
  12. | Tables_in_ops |  
  13. +---------------+  
  14. | customers |  
  15. +---------------+  
  16. 1 row in set (0.00 sec)   
  17.  
  18. mysql> desc customers;  
  19. +-------+----------+------+-----+---------+----------------+  
  20. | Field | Type | Null | Key | Default | Extra | 
  21.  +-------+----------+------+-----+---------+----------------+  
  22. | id | int(11) | NO | PRI | NULL | auto_increment |  
  23. name | char(20) | NO | | NULL | |  
  24. | age | int(11) | NO | | NULL | | 
  25. +-------+----------+------+-----+---------+----------------+  
  26. rows in set (0.02 sec)   
  27.  
  28. mysql> insert into customers values(1,"wangbo","24");  
  29. Query OK, 1 row affected (0.06 sec)   
  30.  
  31. mysql> insert into customers values(2,"guohui","22");  
  32. Query OK, 1 row affected (0.06 sec)   
  33.  
  34. mysql> insert into customers values(3,"zhangheng","27");  
  35. Query OK, 1 row affected (0.09 sec)   
  36.  
  37. mysql> select * from customers;  
  38. +----+-----------+-----+  
  39. | id | name | age |  
  40. +----+-----------+-----+  
  41. | 1 | wangbo | 24 |  
  42. | 2 | guohui | 22 |  
  43. | 3 | zhangheng | 27 |  
  44. +----+-----------+-----+ 
  45. rows in set (0.00 sec) 

 

(2)現(xiàn)在進(jìn)行全備份

 

  1. [root@vm-002 ~]# mysqldump -uroot -p -B -F -R -x --master-data=2 ops|gzip >/opt/backup/ops_$(date +%F).sql.gz  
  2. Enter password 
  3. [root@vm-002 ~]# ls /opt/backup/  
  4. ops_2016-09-25.sql.gz 

 

參數(shù)說明:

-B:指定數(shù)據(jù)庫

-F:刷新日志

-R:備份存儲過程等

-x:鎖表

–master-data:在備份語句里添加CHANGE MASTER語句以及binlog文件及位置點(diǎn)信息

(3)再次插入數(shù)據(jù)

 

  1. mysql> insert into customers values(4,"liupeng","21");  
  2. Query OK, 1 row affected (0.06 sec)   
  3.  
  4. mysql> insert into customers values(5,"xiaoda","31");  
  5. Query OK, 1 row affected (0.07 sec)   
  6.  
  7. mysql> insert into customers values(6,"fuaiai","26");  
  8. Query OK, 1 row affected (0.06 sec)   
  9.  
  10. mysql> select * from customers;  
  11. +----+-----------+-----+  
  12. | id | name | age |  
  13. +----+-----------+-----+  
  14. | 1 | wangbo | 24 |  
  15. | 2 | guohui | 22 |  
  16. | 3 | zhangheng | 27 |  
  17. | 4 | liupeng | 21 | 
  18. | 5 | xiaoda | 31 |  
  19. | 6 | fuaiai | 26 |  
  20. +----+-----------+-----+  
  21. rows in set (0.00 sec) 

 

(4)此時誤操作,刪除了test數(shù)據(jù)庫

 

  1. mysql> drop database ops;  
  2. Query OK, 1 row affected (0.04 sec) 

 

此時,全備之后到誤操作時刻之間,用戶寫入的數(shù)據(jù)在binlog中,需要恢復(fù)出來!

(5)查看全備之后新增的binlog文件

 

  1. [root@vm-002 ~]# cd /opt/backup/  
  2. [root@vm-002 backup]# ls  
  3. ops_2016-09-25.sql.gz  
  4. [root@vm-002 backup]# gzip -d ops_2016-09-25.sql.gz  
  5. [root@vm-002 backup]# ls  
  6. ops_2016-09-25.sql  
  7. [root@vm-002 backup]# grep CHANGE ops_2016-09-25.sql  
  8. -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=106; 

這是全備時刻的binlog文件位置,即mysql-bin.000002的106行,因此在該文件之前的binlog文件中的數(shù)據(jù)都已經(jīng)包含在這個全備的sql文件中了

(6)移動binlog文件,并導(dǎo)出為sql文件,剔除其中的drop語句,查看mysql的數(shù)據(jù)存放目錄,有下面可知是在/var/lib/mysql下

 

  1. [root@vm-002 backup]# ps -ef|grep mysql  
  2. root 9272 1 0 01:43 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql  
  3. mysql 9377 9272 0 01:43 pts/1 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock  
  4. [root@vm-002 backup]# cd /var/lib/mysql/  
  5. [root@vm-002 mysql]# ls  
  6. ibdata1 ib_logfile0 ib_logfile1 mysql mysql-bin.000001 mysql-bin.000002 mysql-bin.index mysql.sock test  
  7. [root@vm-002 mysql]# cp mysql-bin.000002 /opt/backup/ 

 

將binlog文件導(dǎo)出sql文件,并vim編輯它刪除其中的drop語句

 

  1. [root@vm-002 backup]# mysqlbinlog -d ops mysql-bin.000002 >002bin.sql  
  2. [root@vm-002 backup]# ls 
  3. 002bin.sql mysql-bin.000002 ops_2016-09-25.sql  
  4. [root@vm-002 backup]# vim 002bin.sql #刪除里面的drop語句 

 

注意:在恢復(fù)全備數(shù)據(jù)之前必須將該binlog文件移出,否則恢復(fù)過程中,會繼續(xù)寫入語句到binlog,最終導(dǎo)致增量恢復(fù)數(shù)據(jù)部分變得比較混亂

(7)恢復(fù)數(shù)據(jù)

 

  1. [root@vm-002 backup]# mysql -uroot -p < ops_2016-09-25.sql  
  2. Enter password 
  3. [root@vm-002 backup]# 

 

查看數(shù)據(jù)庫,看看ops庫在不在

 

  1. mysql> show databases;  
  2. +--------------------+  
  3. Database |  
  4. +--------------------+  
  5. | information_schema |  
  6. | mysql |  
  7. | ops |  
  8. | test |  
  9. +--------------------+  
  10. rows in set (0.00 sec)   
  11.  
  12. mysql> use ops;  
  13. Reading table information for completion of table and column names  
  14. You can turn off this feature to get a quicker startup with -A   
  15.  
  16. Database changed  
  17. mysql> select * from customers;  
  18. +----+-----------+-----+  
  19. | id | name | age |  
  20. +----+-----------+-----+  
  21. | 1 | wangbo | 0 |  
  22. | 2 | guohui | 0 |  
  23. | 3 | zhangheng | 0 |  
  24. +----+-----------+-----+  
  25. rows in set (0.00 sec) 

 

此時恢復(fù)了全備時刻的數(shù)據(jù)。接著,使用002bin.sql文件恢復(fù)全備時刻到刪除數(shù)據(jù)庫之間,新增的數(shù)據(jù)

 

  1. [root@vm-002 backup]# mysql -uroot -p ops <002bin.sql  
  2. Enter password 
  3. [root@vm-002 backup]# 

再次查看數(shù)據(jù)庫,發(fā)現(xiàn)全備份到刪除數(shù)據(jù)庫之間的那部分?jǐn)?shù)據(jù)也恢復(fù)了??!

 

  1. mysql> select * from customers;  
  2. +----+-----------+-----+  
  3. | id | name | age |  
  4. +----+-----------+-----+  
  5. | 1 | wangbo | 24 |  
  6. | 2 | guohui | 22 |  
  7. | 3 | zhangheng | 27 |  
  8. | 4 | liupeng | 21 |  
  9. | 5 | xiaoda | 31 |  
  10. | 6 | fuaiai | 26 |  
  11. +----+-----------+-----+  
  12. rows in set (0.00 sec) 

 

以上就是mysql數(shù)據(jù)庫增量數(shù)據(jù)恢復(fù)的實(shí)例過程!

***,總結(jié)幾點(diǎn):

1)本案例適用于人為SQL語句造成的誤操作或者沒有主從復(fù)制等的熱備情況宕機(jī)時的修復(fù)

2)恢復(fù)條件為mysql要開啟binlog日志功能,并且要全備和增量的所有數(shù)據(jù)

3)恢復(fù)時建議對外停止更新,即禁止更新數(shù)據(jù)庫

 

4)先恢復(fù)全量,然后把全備時刻點(diǎn)以后的增量日志,按順序恢復(fù)成SQL文件,然后把文件中有問題的SQL語句刪除(也可通過時間和位置點(diǎn)),再恢復(fù)到數(shù)據(jù)庫。 

責(zé)任編輯:龐桂玉 來源: IT專家
相關(guān)推薦

2017-04-01 18:30:47

MySQL誤刪除數(shù)據(jù)庫

2024-08-09 10:06:09

2017-04-01 09:00:00

數(shù)據(jù)庫誤刪除案例及建議

2011-08-01 14:50:10

日志挖掘數(shù)據(jù)庫

2017-02-06 10:53:33

2022-11-08 08:11:52

PG數(shù)據(jù)庫防誤

2019-08-20 14:02:07

MongoDB數(shù)據(jù)庫恢復(fù)數(shù)據(jù)

2009-12-21 16:17:01

2020-09-30 06:00:00

Linux誤刪除恢復(fù)文件

2011-03-30 14:08:27

MySQL數(shù)據(jù)庫刪除恢復(fù)

2014-07-02 15:37:49

PLSQL

2010-06-09 15:40:59

MySQL數(shù)據(jù)庫文件

2011-07-04 09:59:01

AD誤刪除

2019-10-11 09:55:53

數(shù)據(jù)工具架構(gòu)

2013-01-18 09:59:35

SQL Server

2010-08-17 11:03:01

DB2恢復(fù)誤刪除表

2010-08-12 14:03:24

DB2恢復(fù)誤刪除表

2010-03-10 15:33:31

Linux誤刪除

2017-10-26 10:25:07

數(shù)據(jù)恢復(fù)服務(wù)

2020-04-20 09:51:10

數(shù)據(jù)恢復(fù)備份
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號