解析MySQL binlog --大致结构及event type

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解析MySQL binlog --大致结构及event type相关的知识,希望对你有一定的参考价值。

1、简介

binlog以事件的形式记录数据库变更情况。通过执行show binlog events in "binlog file"命令可以查看事件

mysql> show binlog events in "mysql-bin.000002";  
+------------------+-----+-------------+-----------+-------------+---------------------------------------------+  
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                        |  
+------------------+-----+-------------+-----------+-------------+---------------------------------------------+  
| mysql-bin.000002 |   4 | Format_desc |        11 |         120 | Server ver: 5.6.26-debug-log, Binlog ver: 4 |  
| mysql-bin.000002 | 120 | Query       |        11 |         191 | BEGIN                                       |  
| mysql-bin.000002 | 191 | Table_map   |        11 |         236 | table_id: 70 (yzs.t1)                       |  
| mysql-bin.000002 | 236 | Write_rows  |        11 |         280 | table_id: 70 flags: STMT_END_F              |  
| mysql-bin.000002 | 280 | Xid         |        11 |         311 | COMMIT /* xid=9 */                          |  
+------------------+-----+-------------+-----------+-------------+---------------------------------------------+  
5 rows in set (0.00 sec)  
mysql> show binlog events in "mysql-bin.000001";  
+------------------+-----+-------------+-----------+-------------+---------------------------------------------+  
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                        |  
+------------------+-----+-------------+-----------+-------------+---------------------------------------------+  
| mysql-bin.000001 |   4 | Format_desc |        11 |         120 | Server ver: 5.6.26-debug-log, Binlog ver: 4 |  
| mysql-bin.000001 | 120 | Query       |        11 |         197 | BEGIN                                       |  
| mysql-bin.000001 | 197 | Query       |        11 |         294 | use `yzs`; insert into t1 select 2,2        |  
| mysql-bin.000001 | 294 | Xid         |        11 |         325 | COMMIT /* xid=9 */                          |  
| mysql-bin.000001 | 325 | Stop        |        11 |         348 |                                             |  
+------------------+-----+-------------+-----------+-------------+---------------------------------------------+  
5 rows in set (0.00 sec)

2、binlog事件格式及类型
技术分享图片
分为2部分,事件头和事件体。事件头包括:

timestamp:事件开始的执行时间,固定4字节展示是新纪元(epoch time)以来的秒数。

event type:指明该事件的类型

server-id:服务器的server ID

event size:该事件的长度

next-log pos:固定4字节下一个event的开始位置

flag:固定2字节 event flags
#define LOG_EVENT_BINLOG_IN_USE_F 0x1 这个flags表示是否binlog正确的关闭了
..其他标签可参看源码log_event.h

事件体:根据事件类型的不同,包含了不同的信息。

binlog事件类型:
技术分享图片
只挑了比较重要的事件类型进行解析。下章节针对每个event类型进行详细解析。

以上是关于解析MySQL binlog --大致结构及event type的主要内容,如果未能解决你的问题,请参考以下文章

故障分析 MySQL 数据”丢失”事件之 binlog 解析应用一则

mysql binlog解析概要

mysql开启binlog日志

spring boot使用mysql-binlog-connector-java解析mysql binlog日志(实时+离线)

解析MySQL binlog --FORMAT_DESCRIPTION_EVENT

mysql之 binlog维护详细解析(开启binlog相关参数作用mysqlbinlog解读binlog删除)