您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關MySQL數據庫誤刪除后如何恢復,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
首先,要確保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:指定數據庫
-F:刷新日志
-R:備份存儲過程等
-x:鎖表
–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)此時誤操作,刪除了test數據庫
mysql> drop database ops; Query OK, 1 row affected (0.04 sec)
此時,全備之后到誤操作時刻之間,用戶寫入的數據在binlog中,需要恢復出來!
(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;
這是全備時刻的binlog文件位置,即mysql-bin.000002的106行,因此在該文件之前的binlog文件中的數據都已經包含在這個全備的sql文件中了
(6)移動binlog文件,并導出為sql文件,剔除其中的drop語句,查看mysql的數據存放目錄,有下面可知是在/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文件導出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語句
注意:在恢復全備數據之前必須將該binlog文件移出,否則恢復過程中,會繼續寫入語句到binlog,最終導致增量恢復數據部分變得比較混亂
(7)恢復數據
[root@vm-002 backup]# mysql -uroot -p < ops_2016-09-25.sql Enter password: [root@vm-002 backup]#
查看數據庫,看看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)
此時恢復了全備時刻的數據。接著,使用002bin.sql文件恢復全備時刻到刪除數據庫之間,新增的數據
[root@vm-002 backup]# mysql -uroot -p ops <002bin.sql Enter password: [root@vm-002 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數據庫誤刪除后如何恢復就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。