1. Introduction
Binlog records database changes in the form of events. You can view events by executing the show Binlog events in "Binlog file" command
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 event format and type
Divided into 2 parts, event headers and event bodies. Event headers include:
Timestamp: The execution time of the start of the event, the fixed 4-byte display is the number of seconds since the epoch time.
Event Type: Indicates the type of the event
Server-id: Server ID for servers
Event Size: The length of the event
Next-log pos: Fixed 4 bytes Start position of next event
Flag: Fixed 2 bytes Event flags
#define LOG_EVENT_BINLOG_IN_USE_F 0x1 This flags indicates whether BINLOG is properly closed.
.. Other tags can be see source log_event.h
Event Body: Different information is included depending on the type of event.
Binlog Event Type:
Only the more important event types are selected for parsing. The following section parses each event type in detail.
Parsing MySQL Binlog-(1) Approximate structure and event type