首页 文章详情

mysqlbinlog日志恢复数据

php学习交流吧 | 267 2020-12-20 11:22 0 0 0
UniSMS (合一短信)

嗨!有段时间没有更新了,最近有好多人加我说要进群的,由于当时建群没人进,再加上最近一段时间比较忙,就无奈先解散了哈哈哈,抱歉了各位!


ok,回归正文

恢复数据的重要命令如下

MySQL> flush logs;

默认的日志是mysql-bin.000001

现在刷新了重新开启一个就多了一个mysql-bin.000002


./mysqlbinlog --no-defaults binlog日志名,来查看日志

[root@localhost bin]# ./mysqlbinlog --no-defaults ../var/mysql-bin.000001 | more    //查看bin-log日志的内容[root@localhost bin]# ./mysqlbinlog --no-defaults ../var/mysql-bin.000001 | ./mysql -uroot -p //恢复mysql-bin.000001日志的内容


如果需要从某个点恢复到某个点,用以下操作
定位:--start-position 开始点

--stop-position 结束点--start-date 开始时间--stop-date  结束时间


现在恢复mysql-bin.000002恢复,从134点开始到386结束 

[root@localhost bin]# ./mysqlbinlog --no-defaults --start-position 134 --stop-position=386 ../var/mysql-bin.000002 | ./mysql -uroot -p


mysqlbinlog日志恢复数据实验
//查看一下var下面的内容,现在是没有mysql-log.000001类似的binlog日志的

[root@localhost var]# lsbrocms      ibdata1      ib_logfile1    localhost.pid  mysql-bin.indexbrotherblog  ib_logfile0  localhost.err  mysql          test
[root@localhost var]# ../bin/mysql -uroot -p //登录数据库mysql> use test; //使用test数据库mysql> flush logs; //刷新binlog日志,新开一个,现在会在var目录下面生成一个mysql-bin.000001的文件,以下的操作都会记录其中


//创建一个表

mysql> create table user(    -> id int auto_increment primary key,    -> username char(30),    -> password char(32))    -> engine=myisam default charset=utf8;


//插入几条测试数据

mysql> insert into user(username,password) values(1,2);mysql> insert into user(username,password) values(1,2);mysql> insert into user(username,password) values(1,2);


新开一个binlog日志,现在会生成一个名为mysql-bin.000002的文件,下面的操作会记录在mysql-bin.000002的文件中

mysql> flush logs;


查询一下内容

mysql> select * from user;+----+----------+----------+| id | username | password |+----+----------+----------+|  1 | 1        | 2        ||  2 | 1        | 2        ||  3 | 1        | 2        |+----+----------+----------+


现在将数据删除

mysql> delete from user;


将表删除

mysql> drop table user;


查看表里面的内容

mysql> select * from user;mysql> \q
[root@localhost var]# lsbrocms      ibdata1      ib_logfile1    localhost.pid  mysql-bin.000001  mysql-bin.indexbrotherblog  ib_logfile0  localhost.err  mysql          mysql-bin.000002  test[root@localhost var]# ../bin/mysqlbinlog --no-defaults mysql-bin.000001 | more //查看mysql-bin.000001里面的内容[root@localhost var]# ../bin/mysqlbinlog --no-defaults mysql-bin.000002 | more //查看mysql-bin.000002里面的内容[root@localhost var]# ../bin/mysqlbinlog --no-defaults mysql-bin.000001 | ../bin/mysql -uroot -p //用mysql-bin.000001来恢复数据Enter password:[root@localhost var]# ../bin/mysql -uroot -p //进数据库查看


数据库操作

mysql> use test;mysql> show tables;+----------------+| Tables_in_test |+----------------+| user          |+----------------+1 row in set (0.00 sec)  mysql> select * from user; //查看数据,数据回来了+----+----------+----------+| id | username | password |+----+----------+----------+|  1 | 1        | 2        ||  2 | 1        | 2        ||  3 | 1        | 2        |+----+----------+----------+3 rows in set (0.00 sec)  mysql> \q


如果需要从某个点恢复到某个点,用以下操作
定位: 

--start-position 开始点
--stop-position 结束点
--start-date 开始时间
--stop-date  结束时间
 
现在恢复mysql-bin.000002恢复,从134点开始到386结束 

[root@localhost bin]# ./mysqlbinlog --no-defaults --start-position 134 --stop-position=386 ../var/mysql-bin.000002 | ./mysql -uroot -p



good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter