關(guān)于mysql數(shù)據(jù)庫誤刪除后的數(shù)據(jù)恢復(fù)操作說明
在日常運(yùn)維工作中,對(duì)于mysql數(shù)據(jù)庫的備份是至關(guān)重要的!數(shù)據(jù)庫對(duì)于網(wǎng)站的重要性使得我們對(duì)mysql數(shù)據(jù)的管理不容有失!
然后,是人總難免會(huì)犯錯(cuò)誤,說不定哪天大腦短路了來個(gè)誤操作把數(shù)據(jù)庫給刪除了,怎么辦???
下面,就mysql數(shù)據(jù)庫誤刪除后的恢復(fù)方案進(jìn)行說明。
一、工作場(chǎng)景
(1)MySQL數(shù)據(jù)庫每晚12:00自動(dòng)完全備份。
(2)某天早上上班,9點(diǎn)的時(shí)候,一同事犯暈drop了一個(gè)數(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ū)塊添加:
log-bin=mysql-bin
然后重啟mysql服務(wù)
----------------------------------------
(1)在ops庫下創(chuàng)建一張表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)現(xiàn)在進(jìn)行全備份
[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
-----------------
參數(shù)說明:
-B:指定數(shù)據(jù)庫
-F:刷新日志
-R:備份存儲(chǔ)過程等
-x:鎖表
--master-data:在備份語句里添加CHANGE MASTER語句以及binlog文件及位置點(diǎn)信息
-----------------
(3)再次插入數(shù)據(jù)
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)此時(shí)誤操作,刪除了test數(shù)據(jù)庫
mysql> drop database ops;
Query OK, 1 row affected (0.04 sec)
此時(shí),全備之后到誤操作時(shí)刻之間,用戶寫入的數(shù)據(jù)在binlog中,需要恢復(fù)出來!
(5) 查看全備之后新增的binlog文件
[root@vm-002 ~]# cd /opt/backup/ [root@vm-002 backup]# ls ops_2016-09-25.sql.gz [root@vm-002 backup]# gzip -d ops_2016-09-25.sql.gz [root@vm-002 backup]# ls ops_2016-09-25.sql [root@vm-002 backup]# grep CHANGE ops_2016-09-25.sql -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=106;
這是全備時(shí)刻的binlog文件位置
即mysql-bin.000002的106行,因此在該文件之前的binlog文件中的數(shù)據(jù)都已經(jīng)包含在這個(gè)全備的sql文件中了
(6)移動(dòng)binlog文件,并導(dǎo)出為sql文件,剔除其中的drop語句
查看mysql的數(shù)據(jù)存放目錄,有下面可知是在/var/lib/mysql下
[root@vm-002 backup]# ps -ef|grep mysql 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 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 [root@vm-002 backup]# cd /var/lib/mysql/ [root@vm-002 mysql]# ls ibdata1 ib_logfile0 ib_logfile1 mysql mysql-bin.000001 mysql-bin.000002 mysql-bin.index mysql.sock test [root@vm-002 mysql]# cp mysql-bin.000002 /opt/backup/
將binlog文件導(dǎo)出sql文件,并vim編輯它刪除其中的drop語句
[root@vm-002 backup]# mysqlbinlog -d ops mysql-bin.000002 >002bin.sql [root@vm-002 backup]# ls 002bin.sql mysql-bin.000002 ops_2016-09-25.sql [root@vm-002 backup]# vim 002bin.sql #刪除里面的drop語句
注意:
在恢復(fù)全備數(shù)據(jù)之前必須將該binlog文件移出,否則恢復(fù)過程中,會(huì)繼續(xù)寫入語句到binlog,最終導(dǎo)致增量恢復(fù)數(shù)據(jù)部分變得比較混亂
(7)恢復(fù)數(shù)據(jù)
[root@vm-002 backup]# mysql -uroot -p < ops_2016-09-25.sql
Enter password:
[root@vm-002 backup]#
查看數(shù)據(jù)庫,看看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 names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from customers; +----+-----------+-----+ | id | name | age | +----+-----------+-----+ | 1 | wangbo | 0 | | 2 | guohui | 0 | | 3 | zhangheng | 0 | +----+-----------+-----+ 3 rows in set (0.00 sec)
此時(shí)恢復(fù)了全備時(shí)刻的數(shù)據(jù)
接著,使用002bin.sql文件恢復(fù)全備時(shí)刻到刪除數(shù)據(jù)庫之間,新增的數(shù)據(jù)
[root@vm-002 backup]# mysql -uroot -p ops <002bin.sql
Enter password:
[root@vm-002 backup]#
再次查看數(shù)據(jù)庫,發(fā)現(xiàn)全備份到刪除數(shù)據(jù)庫之間的那部分?jǐn)?shù)據(jù)也恢復(fù)了??!
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數(shù)據(jù)庫增量數(shù)據(jù)恢復(fù)的實(shí)例過程!
**********************************************
最后,總結(jié)幾點(diǎn):
1)本案例適用于人為SQL語句造成的誤操作或者沒有主從復(fù)制等的熱備情況宕機(jī)時(shí)的修復(fù)
2)恢復(fù)條件為mysql要開啟binlog日志功能,并且要全備和增量的所有數(shù)據(jù)
3)恢復(fù)時(shí)建議對(duì)外停止更新,即禁止更新數(shù)據(jù)庫
4)先恢復(fù)全量,然后把全備時(shí)刻點(diǎn)以后的增量日志,按順序恢復(fù)成SQL文件,然后把文件中有問題的SQL語句刪除(也可通過時(shí)間和位置點(diǎn)),再恢復(fù)到數(shù)據(jù)庫。
以上這篇關(guān)于mysql數(shù)據(jù)庫誤刪除后的數(shù)據(jù)恢復(fù)操作說明就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- MySQL數(shù)據(jù)庫誤刪恢復(fù)的超詳細(xì)教程
- Mysql恢復(fù)誤刪庫表數(shù)據(jù)完整場(chǎng)景演示
- MySQL誤刪后使用binlog恢復(fù)數(shù)據(jù)的實(shí)現(xiàn)方法
- MySQL使用MyFlash快速恢復(fù)誤刪除和修改的數(shù)據(jù)
- MYSQL?Binlog恢復(fù)誤刪數(shù)據(jù)庫詳解
- MySQL恢復(fù)誤刪數(shù)據(jù)圖文教程
- mysql誤刪數(shù)據(jù)后快速恢復(fù)的辦法推薦
- MySQL數(shù)據(jù)庫誤刪恢復(fù)的幾種方式實(shí)現(xiàn)
相關(guān)文章
MySQL存儲(chǔ)數(shù)據(jù)亂碼的問題解析
這篇文章主要介紹了MySQL存儲(chǔ)數(shù)據(jù)亂碼的問題解析,作者從實(shí)際使用中的多個(gè)方面定位其原因然后解決,需要的朋友可以參考下2015-05-05
Linux環(huán)境下安裝mysql5.7.36數(shù)據(jù)庫教程
大家好,本篇文章主要講的是Linux環(huán)境下安裝mysql5.7.36數(shù)據(jù)庫教程,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Mysql四種分區(qū)方式以及組合分區(qū)落地實(shí)現(xiàn)詳解
對(duì)用戶來說,分區(qū)表是一個(gè)獨(dú)立的邏輯表,但是底層由多個(gè)物理子表組成,下面這篇文章主要給大家介紹了關(guān)于Mysql四種分區(qū)方式以及組合分區(qū)落地實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-04-04
阿里云Linux CentOS 7.2下自建MySQL的root密碼忘記的解決方法
這篇文章主要介紹了阿里云Linux CentOS 7.2下自建MySQL的root密碼忘記的解決方法,需要的朋友可以參考下2017-07-07
MySQL索引類型總結(jié)和使用技巧以及注意事項(xiàng)
索引是快速搜索的關(guān)鍵。MySQL索引的建立對(duì)于MySQL的高效運(yùn)行是很重要的。下面介紹幾種常見的MySQL索引類型2014-04-04

