詳解MySQL誤操作后怎樣進行數(shù)據(jù)恢復
一、開啟binlog。
首先查看binlog是否開啟
mysql> show variables like "log_bin"; +---------------+-------+ |Variable_name | Value +---------------+-------+ | log_bin OFF +---------------+-------+ 1 row in set (0.00 sec)
值為OFF,需開啟,開啟binlog方式如下:
#vim /etc/my.cnf
在[mysqld]中加入
log-bin = mysql-bin log-bin = /usr/local/mysql/log/mysql-bin.log
重啟mysql服務
#service mysqld stop #service mysqld start
二、模擬數(shù)據(jù)寫入
建庫
create database backup;
建表
CREATE TABLE `number` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '編號', `updatetime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
寫入數(shù)據(jù)
程序2-1
#coding:utf8 #python2.7 import MySQLdb import time def connect_mysql(db_host="192.168.11.169",user="martin",passwd="martin",db="backup",charset="utf8"): conn = MySQLdb.connect(host=db_host,user=user,passwd=passwd,db=db,charset=charset) conn.autocommit(True) return conn.cursor() #數(shù)據(jù)插入 for i in range(0,10): #time=time.strftime("%Y-%m-%d %H:%M:%S") sql = 'insert into number(updatetime) values(%s)' values = [(time.strftime("%Y-%m-%d %H:%M:%S"))] db1 = connect_mysql() print db1.executemany(sql,values)
查詢數(shù)據(jù)
mysql> select * from number; +-------+------------------------+ | id | updatetime +--------------------------------+ | 1 | 2016-06-29 23:27:15 | | 2 | 2016-06-29 23:27:15 | | 3 | 2016-06-29 23:27:15 | | 4 | 2016-06-29 23:27:15 | | 5 | 2016-06-29 23:27:15 | | 6 | 2016-06-29 23:27:15 | | 7 | 2016-06-29 23:27:15 | | 8 | 2016-06-29 23:27:15 | | 9 | 2016-06-29 23:27:15 | | 10 | 2016-06-29 23:27:15 | +-------+------------------------+ 10 rows in set (0.00 sec)
三、全量備份
mysqldump -uroot -p -F --master-data=2 backup |gzip> /martin/data/backup_$(date +%F).sql.gz
注:加-F能刷新binlog,方便恢復時操作。
四、模擬寫入增量數(shù)據(jù)
繼續(xù)執(zhí)行程序2-1。
查詢數(shù)據(jù)
mysql> select * from number; +----+---------------------------+ | id | updatetime | +----+---------------------------+ | 1 | 2016-06-29 23:27:15 | | 2 | 2016-06-29 23:27:15 | | 3 | 2016-06-29 23:27:15 | | 4 | 2016-06-29 23:27:15 | | 5 | 2016-06-29 23:27:15 | | 6 | 2016-06-29 23:27:15 | | 7 | 2016-06-29 23:27:15 | | 8 | 2016-06-29 23:27:15 | | 9 | 2016-06-29 23:27:15 | | 10 | 2016-06-29 23:27:15 | | 11 | 2016-06-29 23:31:03 | | 12 | 2016-06-29 23:31:03 | | 13 | 2016-06-29 23:31:03 | | 14 | 2016-06-29 23:31:03 | | 15 | 2016-06-29 23:31:03 | | 16 | 2016-06-29 23:31:03 | | 17 | 2016-06-29 23:31:03 | | 18 | 2016-06-29 23:31:03 | | 19 | 2016-06-29 23:31:03 | | 20 | 2016-06-29 23:31:03 | +-------+---------------------+ 20 rows in set (0.00 sec)
五、增量備份
保留mysql-bin.000002及之后的binlog即可。
六、模擬誤操作
delete from number;
七、再次寫入增量數(shù)據(jù)
執(zhí)行程序2-1
select * from bumber;
+------+------------------------+ | id | updatetime | +------+------------------------+ | 21 | 2016-06-29 23:41:06 | | 22 | 2016-06-29 23:41:06 | | 23 | 2016-06-29 23:41:06 | | 24 | 2016-06-29 23:41:06 | | 25 | 2016-06-29 23:41:06 | | 26 | 2016-06-29 23:41:06 | | 27 | 2016-06-29 23:41:06 | | 28 | 2016-06-29 23:41:06 | | 29 | 2016-06-29 23:41:06 | | 30 | 2016-06-29 23:41:06 | +------+------------------------+ 10 rows in set (0.00 sec)
八、恢復
此時發(fā)現(xiàn)之前的delete操作為誤操作,急需恢復,恢復過程如下
給該表加上讀鎖
lock table number read;
將全量備份的數(shù)據(jù)導入
#cd /martin/data/ #gzip -d number_2016-06-29.sql.gz #grep -i "change" *.sql -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=107;
刷新日志
#mysqladmin -uroot -p'martin' flush-logs #cd /usr/local/mysql/log #ls|grep mysql-bin|grep -v index mysql-bin.000001 mysql-bin.000002 mysql-bin.000003
可確定mysql-bin.000002為增量數(shù)據(jù)binlog
導入全量備份
#cd /martin/data/ #mysql -uroot -p backup < number_2016-06-29.sql #cp /usr/local/mysql/log/mysql-bin.000002 /martin/data/ #mysqlbinlog mysql-bin.000002 >bin.sql #vim bin.sql
在bin.sql找到之前的delete語句,刪除
mysql -uroot -p <bin.sql
九、確認已恢復數(shù)據(jù)
登錄mysql
#mysql -uroot -p'martin' backup select * from number;
+----+---------------------+ | id | updatetime | +----+---------------------+ | 1 | 2016-06-29 23:27:15 | | 2 | 2016-06-29 23:27:15 | | 3 | 2016-06-29 23:27:15 | | 4 | 2016-06-29 23:27:15 | | 5 | 2016-06-29 23:27:15 | | 6 | 2016-06-29 23:27:15 | | 7 | 2016-06-29 23:27:15 | | 8 | 2016-06-29 23:27:15 | | 9 | 2016-06-29 23:27:15 | | 10 | 2016-06-29 23:27:15 | | 11 | 2016-06-29 23:31:03 | | 12 | 2016-06-29 23:31:03 | | 13 | 2016-06-29 23:31:03 | | 14 | 2016-06-29 23:31:03 | | 15 | 2016-06-29 23:31:03 | | 16 | 2016-06-29 23:31:03 | | 17 | 2016-06-29 23:31:03 | | 18 | 2016-06-29 23:31:03 | | 19 | 2016-06-29 23:31:03 | | 20 | 2016-06-29 23:31:03 | | 21 | 2016-06-29 23:41:06 | | 22 | 2016-06-29 23:41:06 | | 23 | 2016-06-29 23:41:06 | | 24 | 2016-06-29 23:41:06 | | 25 | 2016-06-29 23:41:06 | | 26 | 2016-06-29 23:41:06 | | 27 | 2016-06-29 23:41:06 | | 28 | 2016-06-29 23:41:06 | | 29 | 2016-06-29 23:41:06 | | 30 | 2016-06-29 23:41:06 | +----+---------------------+ 30 rows in set (0.00 sec)
恢復完成!以上就是本文的全部內(nèi)容,在操作數(shù)據(jù)庫時候要多加小心盡量避免誤操作,如果萬一遇到了,希望本文能夠幫助大家。
相關(guān)文章
Express連接MySQL及數(shù)據(jù)庫連接池技術(shù)實例
數(shù)據(jù)庫連接池是程序啟動時建立足夠數(shù)量的數(shù)據(jù)庫連接對象,并將這些連接對象組成一個池,由程序動態(tài)地對池中的連接對象進行申請、使用和釋放,本文重點給大家介紹Express連接MySQL及數(shù)據(jù)庫連接池技術(shù),感興趣的朋友一起看看吧2022-02-02mysql函數(shù)拼接查詢concat函數(shù)的使用方法
下面小編就為大家?guī)硪黄猰ysql函數(shù)拼接查詢concat函數(shù)的使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-083種高效的Tags標簽系統(tǒng)數(shù)據(jù)庫設(shè)計方案分享
這篇文章主要介紹了3種高效的Tags標簽系統(tǒng)數(shù)據(jù)庫設(shè)計方案分享,現(xiàn)在主流的博客、CMS系統(tǒng)都有一個標簽系統(tǒng),本文就探討它的數(shù)據(jù)庫設(shè)計方式,需要的朋友可以參考下2014-07-07MySQL group by和left join并用解決方式
這篇文章主要介紹了MySQL group by和left join并用解決方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12選擇MySQL數(shù)據(jù)庫的命令以及PHP腳本下的操作方法
這篇文章主要介紹了選擇MySQL數(shù)據(jù)庫的命令以及PHP腳本下的操作方法,此外文中還對MySQL的基本數(shù)據(jù)類型作了介紹,需要的朋友可以參考下2015-11-11mysql 數(shù)據(jù)庫中my.ini的優(yōu)化 2G內(nèi)存針對站多 抗壓型的設(shè)置
mysql數(shù)據(jù)庫中my.ini的優(yōu)化,2G內(nèi)存,針對站多,抗壓型的設(shè)置.大家可以借鑒下。2009-08-08