mysql資料庫誤刪除後的資料恢複操作說明

來源:互聯網
上載者:User

標籤:run   ast   err   完全備份   順序   you   auto   技能   功能   

在日常營運工作中,對於mysql資料庫的許可權的規避,SQL審核最佳化、資料備份恢複就變成了,工作必備技能;
資料庫對於網站的重要性使得我們對mysql資料的管理不容有失!
然後,是人總難免會犯錯誤,說不定哪天大腦短路了來個誤操作把資料庫給刪除了,怎麼辦???

下面,就mysql資料庫誤刪除後的恢複方案進行說明。

一、MySQL資料恢複方法總結:

1、使用Mysql 資料閃回工具恢複資料的文章:http://blog.51cto.com/qiuyt/2095758
2、今天的主角 【Mysqldump】,原生內建

二、工作情境

(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)現在進行全備份

[[email protected] ~]# mysqldump -uroot -p -B -F -R -x --master-data=2 ops|gzip >/opt/backup/ops_$(date +%F).sql.gzEnter password: [[email protected] ~]# ls /opt/backup/ops_2018-07-03.sql.gz
### 參數說明:-B:指定資料庫(備份中帶有創庫語句)-F:重新整理日誌-R:備份預存程序等-x:鎖表 (鎖表過程會造成業務無法讀寫,可能會出現502)--master-data:在備份語句裡添加CHANGE MASTER語句以及binlog檔案及位置點資訊

(3)再次插入資料

mysql> insert into customers values(4,"liupeng","21");Query OK, 1 row affected (0.06 sec)mysql> insert into customers values(5,"xiaoda","31");Query OK, 1 row affected (0.07 sec)mysql> insert into customers values(6,"fuaiai","26");Query OK, 1 row affected (0.06 sec)mysql> select * from customers;+----+-----------+-----+| id | name | age |+----+-----------+-----+| 1 | wangbo | 24 || 2 | guohui | 22 || 3 | zhangheng | 27 || 4 | liupeng | 21 || 5 | xiaoda | 31 || 6 | fuaiai | 26 |+----+-----------+-----+6 rows in set (0.00 sec)

(4)此時誤操作,刪除了OPS資料庫

mysql> drop database ops;Query OK, 1 row affected (0.04 sec)###此時,全備之後到誤操作時刻之間,使用者寫入的資料在binlog中,需要恢複出來!

(5)查看全備之後新增的binlog檔案

[[email protected] ~]# cd /opt/backup/[[email protected] backup]# lsops_2018-07-03.sql.gz[[email protected] backup]# gzip -d ops_2018-07-03.sql.gz [[email protected] backup]# lsops_2018-07-03.sql[[email protected] backup]# grep CHANGE ops_2018-07-03.sql -- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000002‘, MASTER_LOG_POS=106;#這是全備時刻的binlog檔案位置#即mysql-bin.000002的106行,因此在該檔案之前的binlog檔案中的資料都已經包含在這個全備的sql檔案中了

(6)移動binlog檔案,並匯出為sql檔案,剔除其中的drop語句

查看mysql的資料存放目錄,由下面可知是在/var/lib/mysql下[[email protected] backup]# ps -ef|grep mysqlroot 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=mysqlmysql 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[[email protected] backup]# cd /var/lib/mysql/[[email protected] mysql]# lsibdata1 ib_logfile0 ib_logfile1 mysql mysql-bin.000001 mysql-bin.000002 mysql-bin.index mysql.sock test[[email protected] mysql]# cp mysql-bin.000002 /opt/backup/將binlog檔案匯出sql檔案,並vim編輯它刪除其中的drop語句[[email protected] backup]# mysqlbinlog -d ops mysql-bin.000002 >002bin.sql[[email protected] backup]# ls002bin.sql mysql-bin.000002 ops_2018-07-03.sql[[email protected] backup]# vim 002bin.sql #刪除裡面的drop語句## 注意:在恢複全備資料之前必須將該binlog檔案移出,否則恢複過程中,會繼續寫入語句到binlog,最終導致增量恢複資料部分變得比較混亂

(7)恢複資料

[[email protected] backup]# mysql -uroot -p < ops_2018-07-03.sql Enter password: [[email protected] backup]#

(8) 檢查“全備” 是否恢複成功

查看資料庫,看看ops庫在不在mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || ops || test |+--------------------+4 rows in set (0.00 sec)mysql> use ops;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from customers;+----+-----------+-----+| id | name | age |+----+-----------+-----+| 1 | wangbo | 0 || 2 | guohui | 0 || 3 | zhangheng | 0 |+----+-----------+-----+3 rows in set (0.00 sec)
此時恢複了全備時刻的資料

接著,使用002bin.sql檔案恢複全備時刻到刪除資料庫之間,新增的資料

[[email protected] backup]# mysql -uroot -p ops <002bin.sqlEnter password: [[email protected] backup]#再次查看資料庫,發現全備份到刪除資料庫之間的那部分資料也恢複了!!mysql> select * from customers;+----+-----------+-----+| id | name | age |+----+-----------+-----+| 1 | wangbo | 24 || 2 | guohui | 22 || 3 | zhangheng | 27 || 4 | liupeng | 21 || 5 | xiaoda | 31 || 6 | fuaiai | 26 |+----+-----------+-----+6 rows in set (0.00 sec)

以上就是mysql資料庫增量資料恢複的執行個體過程!

最後,總結幾點:

1)本案例適用於人為SQL語句造成的誤操作或者沒有主從複製等的熱備情況宕機時的修複
2)恢複條件為mysql要開啟binlog日誌功能,並且要全備和增量的所有資料
3)恢複時建議對外停止更新,即禁止更新資料庫
4)先恢複全量,然後把全備時刻點以後的增量日誌,按順序恢複成SQL檔案,然後把檔案中有問題的SQL語句刪除(也可通過時間和位置點),再恢複到資料庫。

mysql資料庫誤刪除後的資料恢複操作說明

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.