- 尽可能在从库解析,避免对主库造成影响
- 先粗略定位涉及相关的库表操作的 binlog,再单独解析对应的 binlog 中的数据
- 在解析 DDL 时无需加 -v 输出详细信息(加快解析速度)
- 如果开启了 binlog_rows_query_log_events 参数,需要用 -vv 参数才可显示具体的 SQL 语句
常用命令
1. 解析 binlog 排查问题
如果只是解析出来查看,可以加 --base64-output=decode-rows 不显示行格式的内容:
mysqlbinlog --no-defaults -vv --base64-output=decode-rows mysql-bin.000008
2. 解析指定 GTID 的事务
用来分析某个事务做了什么:
mysqlbinlog --no-defaults -vv --base64-output=decode-rows --include-gtids='00008027-0000-0000-0000-000000008027:234932' mysql-bin.000008
3. 解析指定范围的 binlog
a. 时间范围
–start-datetime、–stop-datetime 解析出指定时间范围内的 binlog,这个只适合粗略的解析,不精准,因此不要用来回放 binlog。有个小技巧:如果只能确定大概的时间范围,而且不确定在哪个 binlog 中,可以直接解析多个 binlog。比如大概在 15:20-16:20 内做了个表删除操作,但这个时间内有多个 binlog,可以这样:
mysqlbinlog --no-defaults -vv --base64-output=decode-rows --start-datetime='2022-04-11 15:20:00' --stop-datetime='2022-04-11 16:20:00' mysql-bin.000007 mysql-bin.000008
b. 偏移范围
–start-position、–stop-position 解析 binlog 指定偏移量范围内的 binlog。如果同时指定了 --start-position 和 --stop-position,并且是解析多个 binlog,则 --start-position 只对第一个 binlog 生效,–stop-position 只对最后一个 binlog 生效。
这个常用场景是:已经解析过一次 binlog 并取得目标事务的 起始 position 后,精确的解析这一段 binlog:
mysqlbinlog --no-defaults -vv --base64-output=decode-rows --start-position='1700' --stop-position='2049' mysql-bin.000008
# at 1700 --"start-position是 GTID event 前的这个 position"
...
...
SET @@SESSION.GTID_NEXT= '00008027-0000-0000-0000-000000008027:234933'/*!*/;
# at 1779
#220411 16:34:34 server id 8027 end_log_pos 1854 CRC32 0x8045a89d Query thread_id=10 exec_time=0 error_code=0
SET TIMESTAMP=1649666074/*!*/;
BEGIN
/*!*/;
# at 1854
#220411 16:34:34 server id 8027 end_log_pos 1909 CRC32 0x95943ed8 Rows_query
# delete from zhouwb where id = 6
# at 1909
#220411 16:34:34 server id 8027 end_log_pos 1969 CRC32 0xbc91d326 Table_map: `test`.`zhouwb` mapped to numbe