mysql利用mysqlbinlog命令恢復(fù)誤刪除數(shù)據(jù)的實(shí)現(xiàn)
MYSQL 5.7.22
開啟二進(jìn)志日志
日志格式MIXED
實(shí)驗(yàn)過程:1、執(zhí)行:FLUSH LOGS;
master-bin.000014 文件就是新生成的文件
刷新日志是為了實(shí)驗(yàn)內(nèi)容更直觀,更容易觀察到整個(gè)實(shí)驗(yàn)過程的內(nèi)容。
我看到網(wǎng)上許多文章有在用REST MASTER;而未說明此命令的嚴(yán)重性
這條命令會(huì)刪除所有日志文件,并將文件名和記錄點(diǎn)進(jìn)行重置歸零,99%的情況下是用不到這條命令的
刪除日志可以用PURGE MASTER LOGS...這樣保險(xiǎn)一點(diǎn)
2、新日志文件已經(jīng)生成,先觀察一下內(nèi)容,有幾個(gè)點(diǎn)需要了解
查看二進(jìn)日日志文件命令:mysqlbinlog master-bin.000014
# at 4#180903 16:19:12 server id 1 end_log_pos 123 CRC32 0xe03659b3 Start: binlog v 4, server v 5.7.22-log created 180903 16:19:12
先看上邊兩個(gè)箭頭:
# at 4(事件開始點(diǎn)) #180903 16:19:12 (代表的是時(shí)間) server id 1(主備復(fù)制時(shí)需要為每個(gè)MYSQL數(shù)據(jù)庫指定唯一的SERVER ID,我的未配置,默認(rèn)是1) end_log_pos 123(事件結(jié)束點(diǎn))再看下邊兩個(gè)箭頭:
# at 123(事件開始點(diǎn),和上邊的事件結(jié)束點(diǎn)是對應(yīng)的) end_log_pos 154(事件結(jié)束點(diǎn)) at 4 和 at 123之間的內(nèi)容就是事件內(nèi)容3、模擬業(yè)務(wù)場景,建表,插入數(shù)據(jù),最后將某個(gè)表刪除;為了真實(shí),我建了兩個(gè)庫,同時(shí)向不同的庫寫入內(nèi)容,最后將其中一個(gè)庫中的某個(gè)表刪除。
mysql> FLUSH LOGS;Query OK, 0 rows affected (0.01 sec)mysql> create database t1;Query OK, 1 row affected (0.03 sec)mysql> create database t2;Query OK, 1 row affected (0.00 sec)mysql> use t1;Database changedmysql> create table t1 (id int);Query OK, 0 rows affected (0.03 sec)mysql> use t2;Database changedmysql> create table t2 (id int);Query OK, 0 rows affected (0.03 sec)mysql> insert into t2 values (3);Query OK, 1 row affected (0.01 sec)mysql> insert into t2 values (4);Query OK, 1 row affected (0.01 sec)mysql> use t1;Database changedmysql> insert into t1 values (1);Query OK, 1 row affected (0.01 sec)mysql> insert into t1 values (2);Query OK, 1 row affected (0.01 sec)mysql> use t2;Database changedmysql> insert into t2 values(20);Query OK, 1 row affected (0.01 sec)mysql> use t1;Database changedmysql> insert into t1 values(10);Query OK, 1 row affected (0.01 sec)mysql> drop table t1;Query OK, 0 rows affected (0.02 sec)mysql> use t2;Database changedmysql> insert into t2 values(222);Query OK, 1 row affected (0.01 sec)mysql>
建立T1、T2庫,建立T1、T2表。
向T1插入數(shù)據(jù):1、2、10
向T2插入數(shù)據(jù):3、4、20、222
模擬場景,刪除T1表,T2庫T2表業(yè)務(wù)還在繼續(xù)運(yùn)行
現(xiàn)在將要通過日志將T1表進(jìn)行恢復(fù)。
首先要先找到那個(gè)刪除命令的日志點(diǎn):
mysqlbinlog master-bin.000014|grep -5a 'DROP TABLE'
看到#AT 2439 (記下這個(gè)數(shù)字)
在這個(gè)事件點(diǎn)執(zhí)行的DROP TABLE操作。
由于日志文件內(nèi)不只有T1庫的日志,還有T2庫的日志,一會(huì)只取T1數(shù)據(jù)庫的日志
而且還只取2439日志點(diǎn)之前的日志,再進(jìn)行重新應(yīng)用
如果把2439的日志取的話,再應(yīng)用時(shí)數(shù)據(jù)庫會(huì)重新建庫建表,插數(shù)據(jù), 還會(huì)執(zhí)行這條刪表語句。
mysqlbinlog -d t1 --stop-position=2439 master-bin.000014>test.sql(執(zhí)行這條語句竟然報(bào)錯(cuò)了)
WARNING: The option --database has been used. It may filter parts of transactions, but will include the GTIDs in any case. If you want to exclude or include transactions, you should use the options --exclude-gtids or --include-gtids, respectively, instead.暫時(shí)弄不清楚原因,百度了下修改成:
mysqlbinlog master-bin.000014 -d t1 --skip-gtids --stop-position=2439>test.sql
-d:參數(shù)是指定某個(gè)數(shù)據(jù)庫日志
命令意思是將master-bin.000014日志文件內(nèi)的T1數(shù)據(jù)庫日志,事件點(diǎn)2439之前的日志,輸出到test.sql
# tail test.sql
看看文件最后幾行
登錄數(shù)據(jù)庫:
mysql> use t1;Database changedmysql> source test.sql
中間報(bào)錯(cuò)了一次,因?yàn)槔镞叞◣霻1語句。
再查看表內(nèi)容
這樣數(shù)據(jù)就回來了。
到此這篇關(guān)于mysql利用mysqlbinlog命令恢復(fù)誤刪除數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)mysql mysqlbinlog恢復(fù)誤刪除內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Oracle 10G First Release的新特性簡介(上)2. 詳解mysql的備份與恢復(fù)3. 使用DB2look 重新創(chuàng)建優(yōu)化器訪問計(jì)劃(3)(1)4. 有時(shí)Oracle不用索引來查找數(shù)據(jù)的原因5. MySQL之高可用集群部署及故障切換實(shí)現(xiàn)6. sql server 2005中的表分區(qū)7. 利用sql server 2005數(shù)據(jù)庫郵件發(fā)送電子郵件8. MariaDB的安裝與配置教程9. Win2k Server下Oracle 9iAS安裝報(bào)告(二)10. Oracle 10g存儲(chǔ)過程遠(yuǎn)程SQL注入漏洞
