1.前言
Mysqlbinlog命令是解析二进制binlog内容的命令,该命令挺重要的!
2.详解
root@localhost 22:34: [(none)]> show binary logs; +------------+-----------+ | Log_name | File_size | +------------+-----------+ | bin.000001 | 5197 | | bin.000002 | 3574 | | bin.000003 | 225 | | bin.000004 | 489 | +------------+-----------+ 4 rows in set (0.00 sec)
root@localhost 22:34: [(none)]> show binlog events in 'bin.000004'; +------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------+ | bin.000004 | 4 | Format_desc | 20107 | 123 | Server ver: 5.7.30-log, Binlog ver: 4 | | bin.000004 | 123 | Previous_gtids | 20107 | 206 | 8c4ba31c-c52b-4d24-ba30-e506b63ac731:1-27:1000005-1000007 | | bin.000004 | 206 | Gtid | 20107 | 267 | SET @@SESSION.GTID_NEXT= '5a412d15-04bc-11ec-95d2-000c29395ab1:1' | | bin.000004 | 267 | Query | 20107 | 489 | GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.31.%' IDENTIFIED WITH 'mysql_native_password' AS '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' | +------------+-----+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec)
注意:以上两个命令需要在Mysql客户端中进行执行
Mysqlbinlog用法:
Usage: mysqlbinlog [options] log-files
- --base64-output=decode-rows –v 选项解析:通常用这个进行解析Mysqlbinlog日志。
- 加-d选项,将只显示对test数据库的操作日志
- 加-o选项, 忽略掉日志中的前n个操作
- 加-r选项,将输出的文本格式日志输出到指定文件,下面将文件结果输出到文件resultfile中
- 加"--start-datetime--stop-datetime"显示9:00 ~12:00之间的日志:
--start-datetime='2021/07/30 09:00:00' --stop-datetime='2021/07/30 12:00:00'
- 加"--start-position=#和--stop-position=#" 后面加的是位点,它和日期范围类似,不过更精确。
- --include-gtids #截取指定的gtid
- --exclude-gtids #排除指定的gtid
- --skip-gtids #跳过gtid幂等性机制的检查,即截取gtid时不带gtid的信息
模拟故障演练:
1.准备数据 create database backup; use backup; create table t1(id int); insert into t1 values(1),(2),(3) commit; 2.周二23:00全备 mysqldump -uroot -p123 -A -R --triggers --set-gtid-purged=OFF --master-data=2 --single-transaction|gzip > /backup/full_$(date+%F).sql.gz 3. 模拟周二23:00到周三10点之间数据变化 use backup; insert into t1(11),(12),(13); commit; create table t2 (id int); insert into t2 values(11),(22),(33); 4.删库(模拟) drop backup; 5.利用gtid 恢复周二23:00到宕机时刻的数据 可以查看命令: show binlog events in '当前正在使用的二进制日志',从中找出需要的数据所对应的GTID号 备份命令: mysqlbinlog --skip-gtids --include-gtids='62b6a13b-19b2-11eb-a0b7-00163e2ce7ef:6-7' --exclude-gtids='9a85ae81-0d17-11eb-9975-00163e1430bc:6' mysql-bin.000005>/bakup/bin.sql 参数说明: --include-gtids 截取指定的gtid --exclude-gtids 排除指定的gtid --skip-gtids 跳过gtid的幂等性机制的检查,即截取日志的时候不带有gtid的信息 6开始恢复 set sql_log_bin=0 source /backup/full_xxxx.sql source /bakup/bin.sql set sql_log_bin=1
参考文档:https://dev.mysql.com/doc/refman/5.6/en/mysqlbinlog.html