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 |
+------------------+-----+-------------+-----------+-------------+---------------------------------------------+
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 |
| 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类型进行详细解析。