MySQL binlog之log event
binlog是mysql以二进制形式打印的日志，它默认不加密，不压缩。每个正常的binlog文件头部，有4个字节的标记，值为0xfe 0x62 0x69 0x6e。LOG_EVENT是binlog里的单位。除去头部的标记，binlog就是一个LOG_EVENT的序列。每个LOG_EVENT都独立单元，没有互相引用的关系，它也有自己的二进制头部，主要是记录了时间戳、类型标记等描述信息。
log event 结构
This section describes the general properties of events as byte sequences as they are written to binary or relay log files.
All events have a common general structure consisting of an event header followed by event data:
The details about what goes in the header and data parts have changed over time, which gives rise to different versions of the binary log format:
- v1: Used in MySQL 3.23
- v3: Used in MySQL 4.0.2 though 4.1
- v4: Used in MySQL 5.0 and up
下面主要看v4 的结构，v4 event structure:
header length = x bytes
data length = (event_length - x) bytes
fixed data length = y bytes variable data length = (event_length - (x + y)) bytes
x is given by the header_length field in the format description event (FDE). Currently, x is 19, so the extra_headers field is empty.
y is specific to the event type, and is given by the FDE. The fixed-part length is the same for all events of a given type, but may vary for different event types.
The fixed part of the event data is sometimes referred to as the "post-header" part. The variable part is sometimes referred to as the "payload" or "body."
log event type
常见的log event type
QUERY_EVENT Written when an updating statement is done.
STOP_EVENT Written when mysqld stops.
ROTATE_EVENT Written when mysqld switches to a new binary log file. This occurs when someone issues a FLUSH LOGS statement or the current binary log file becomes too large. The maximum size is determined by max_binlog_size.
RAND_EVENT Written every time a statement uses the RAND() function; precedes other events for the statement. Indicates the seed values to use for generating a random number with RAND() in the next statement. This is written only before a QUERY_EVENT and is not used with row-based logging.
XID_EVENT Generated for a commit of a transaction that modifies one or more tables of an XA-capable storage engine. Normal transactions are implemented by sending a QUERY_EVENT containing a BEGIN statement and a QUERY_EVENT containing a COMMIT statement (or a ROLLBACK statement if the transaction is rolled back).
WRITE_ROWS_EVENT Used for row-based binary logging. This event logs inserts of rows in a single table.