mysql使用二进制日志恢复数据

一、恢复到某个二进制文件

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

 

数据恢复成功。

猜你喜欢

转载自my.oschina.net/u/3626804/blog/1933712