mysqlbinlog查看与恢复与修改

mysql的binlog日志作用是什么:
用来记录mysql内部增删改等对Mysql数据库有更新的内容记录。(注意:不会记录select日志)
如何打开binlog功能:
[root@localhost 3306]# grep log-bin my.cnf
log-bin=mysql-bin

查看binlog日志:
mysqlbinlog mysql-bin.000005
指定库(分库)查看,例如指定test库:
mysqlbinlog -d test mysql-bin.000005
导出为sql语句:
mysqlbinlog -d test mysql-bin.000005 >test.sql
可以对比导出所有与只导出test库的sql文件,查看:
vimdiff test.sql all.sql

指定位置恢复:
例如我们要恢复插入大米米这个语句,我们从binlog日志中找到这段话:

# at 199
#171220  8:59:03 server id 1  end_log_pos 310 CRC32 0x9f7c1c05  Query   thread_id=1     exec_time=0     error_code=0
use `chao`/*!*/;
SET TIMESTAMP=1513731543/*!*/;
insert into user values ("大米米")
/*!*/;
# at 310
#171220  8:59:03 server id 1  end_log_pos 341 CRC32 0x8c4a3509  Xid = 9
COMMIT/*!*/;
# at 341
#171220  9:05:16 server id 1  end_log_pos 450 CRC32 0x1c582ca9  Query   thread_id=1     exec_time=0     error_code=0

其中at 199代表起始位置,at 310 代表结束位置:

mysqlbinlog  mysql-bin.000005 --start-position=199 --stop-position=310 -r pos.sql

less pos.sql 可以发现只导出了我们指定的insert语句

指定时间恢复:
还是恢复插入大米米这条语句,
可以观察起始时间是171220 8:59:03,结束时间是171220 9:05:16

mysqlbinlog mysql-bin.000005 --start-datetime='2017-12-20 8:59:03' --stop-datetime='2017-12-20 9:05:16' -r time.sql

less time.sql 查看可以发现导出了我们指定的sql语句。
指定时间不够精确,因为一秒钟的时间可能有很多条语句执行
注意:如果不给结束时间就是匹配到结尾,反之,不给开始时间就是从最开始匹配

binlog 模式:

mysql> show variables like "%binlog_format";
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+

在线修改模式
mysql> SET GLOBAL binlog_format=”ROW”; //行级模式
或者: SET GLOBAL binlog_format=”MIXED”; //综合模式
然后退出重进:
刷新生成新开始记录的binlog日志:
mysqladmin -uroot -pqb123 -S /data/3306/mysql.sock flush-logs
去数据库更新一条记录:
mysql> update test set name=”改变” where name =”ff”;
查看修改后的binlog
mysqlbinlog –base64-output=decode-rows -v mysql-bin.000012

猜你喜欢

转载自blog.csdn.net/qq_25611295/article/details/78891108
今日推荐