前言
通過這邊文章你能收獲:
沒有接觸過mysql使用binLog恢復(fù)數(shù)據(jù)的朋友們可以對(duì)如何使用binLog恢復(fù)數(shù)據(jù)
有個(gè)入門級(jí)的認(rèn)識(shí)
一、實(shí)際case
目前存在entity_storage_0庫以及庫中的表entity_detail,
1、張三執(zhí)行插入指令插入了一條數(shù)據(jù)
2蹦渣、李四以為是測(cè)試庫内狸,把entity_detail表中的數(shù)據(jù)清空了
3夷恍、現(xiàn)在需要恢復(fù)張三插入的數(shù)據(jù),如何恢復(fù)?
二蚓峦、確定思路
step1卵迂、確定要恢復(fù)的數(shù)據(jù)范圍
step2察蹲、確定恢復(fù)數(shù)據(jù)的binLog文件名
step3请垛、找出binLog中對(duì)應(yīng)數(shù)據(jù)的記錄信息
step4、執(zhí)行恢復(fù)指令
三洽议、相關(guān)指令
show MASTER status;
flush log
show BINLOG EVENTS in '你的binLog文件名';
./mysqlbinlog --start-position 起始position --stop-position 結(jié)束position -d 數(shù)據(jù)庫名稱 /var/lib/mysql/目標(biāo)binLog文件 | mysql -u數(shù)據(jù)用戶名-p數(shù)據(jù)庫密碼 目標(biāo)數(shù)據(jù)庫名稱
四宗收、開始應(yīng)用
套用基本思路中的模型(通常我們會(huì)在回復(fù)數(shù)據(jù)前在客戶端使用flush log指令刷一下日志文件
)
1、確認(rèn)需恢復(fù)的數(shù)據(jù)范圍
張三插入的那條記錄
2亚兄、確定恢復(fù)數(shù)據(jù)的binLog文件名
在mysql客戶端使用show master status
指令混稽,查看當(dāng)前的binLog
文件名(由于是測(cè)試使用,且沒有flush log
,所以當(dāng)前的file就是我們的目標(biāo)binLog
文件,如果是mysql宕機(jī)或者其他場(chǎng)景,那么需要根據(jù)實(shí)際情況確認(rèn)文件)
3、找出binLog中對(duì)應(yīng)數(shù)據(jù)的記錄信息
在mysql客戶端使用show BINLOG EVENTS in 'binlog.000006';
指令,分析出需要執(zhí)行的end_log_pos
4审胚、執(zhí)行恢復(fù)指令
在mysql的bin目錄下,執(zhí)行指令./mysqlbinlog --start-position 330 --stop-position 606 -d entity_storage_0 /var/lib/mysql/binlog.000006 | mysql -uroot -p111111 entity_storage_0
然后去數(shù)據(jù)庫查看數(shù)據(jù),可以發(fā)現(xiàn)數(shù)據(jù)正承傺恢復(fù).
至此,入門小實(shí)例done!
參考資料
mysql官網(wǎng) - https://dev.mysql.com/doc/refman/8.0/en/point-in-time-recovery-binlog.html