mysql使用二進位制日誌恢復資料
阿新 • • 發佈:2018-12-04
一、恢復到某個二進位制檔案
1.開啟二進位制日誌
在mysqld的配置節點下新增如下配置
log-bin="E:/Mysql57BinLog/binlog"(windows下的路徑,linux下自行修改路徑)
expire_logs_days=10
max_binlog_size=100M
2.重啟mysql服務
使用命令show VARIABLES like '%log_bin%';檢視
3.建立庫和表
create database mytest; use mytest; create table t(a int PRIMARY key)ENGINE = INNODB DEFAULT CHARSET=utf8; flush logs;
flush logs,重新整理二進位制日誌後會多出來一個二進位制日誌
使用命令檢視二進位制日誌內容
預設會讀取配置檔案,檢測到no--beep會報錯。
推薦使用命令:mysqlbinlog --no-defaults E:\Mysql57BinLog\binlog.000001
4.插入資料
use mytest;
insert into t select 1 union all select 2 union all select 3;
flush logs;
5.刪除資料庫
drop database mytest;
flush logs;
6.恢復資料
mysqlbinlog --no-defaults E:\Mysql57BinLog\binlog.000001 E:\Mysql57BinLog\binlog.000002 E:\Mysql57BinLog\binlog.000003 | mysql -u root -p
資料已還原。
-----------------------------------華麗的分割線--------------------------------------------------------------
二、恢復到某一時間點的資料
create table t2(a int PRIMARY key)ENGINE=INNODB default CHARSET=utf8;
insert into t2 values(1),(2),(3),(4),(5);
>mysqlbinlog --no-defaults E:\Mysql57BinLog\binlog.000006
刪除資料
delete from t2 where a < 4;
恢復資料
drop database mytest;
刪除庫mytest,回到最原始的地方
mysqlbinlog --no-defaults --start-position="4" --stop-position="1285" E:\Mysql57BinLog\binlog.000006 | mysql -u root -p
資料恢復成功。