1. 程式人生 > >利用binlog恢復mysql資料庫資料

利用binlog恢復mysql資料庫資料

前提:mysql資料庫開啟了binlog日誌 

    通過  show variables like 'log_%'; 檢視是否開啟binlog日誌。

    

操作步驟:

    在資料庫中執行

    show master status;

    檢視當前資料庫對應的binlog檔案

    

    再執行

    show binlog events in 'binlog.000010';

    檢視binlog日誌記錄,查詢到相應的Pos節點

   執行 /usr/local/mysql/bin/mysqlbinlog --start-position=7171 --stop-position=7546 --database=mydatabase --skip-gtids /var/lib/mysql/binlog.000010 | mysql -uroot -p資料庫密碼 mydatabase

指定結束點之前的資料恢復   只有--stop-position=7546 無--start-position=7171

指定區間恢復 --start-position=7171 --stop-position=7546

指定時間區間恢復 --start-datatime="2018-11-01 11:11:11"  --stop-datatime="2018-11-11 11:11:11"

 

 

PS:通過binlog恢復的根本原理就是按照指定區間的sql操作重新執行一遍,因此其間有建立表的語句的話,可能回合當前資料庫表衝突,需要略過

另外: 

重新整理binlog日誌,產生新的binlog日誌檔案    flush logs; 每次mysqld服務重啟時,會自動執行此命令,重新整理binlog日誌。