1、简介

row格式的binlog文件中,每个ROW_EVENT之前都有一个TABLE_MAP_EVENT,用于描述表的内部ID和结构定义。

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 */                          |
| mysql-bin.000002 | 311 | Query       |        11 |         382 | BEGIN                                       |
| mysql-bin.000002 | 382 | Table_map   |        11 |         427 | table_id: 70 (yzs.t1)                       |
| mysql-bin.000002 | 427 | Write_rows  |        11 |         471 | table_id: 70 flags: STMT_END_F              |
| mysql-bin.000002 | 471 | Xid         |        11 |         502 | COMMIT /* xid=37 */                         |
+------------------+-----+-------------+-----------+-------------+---------------------------------------------+
9 rows in set (0.00 sec)

2、TABLE_MAP_EVENT格式定义
解析MySQL binlog --(4)TABLE_MAP_EVENT_第1张图片
3、结合案例说明
解析MySQL binlog --(4)TABLE_MAP_EVENT_第2张图片
--------公有事件头-------

1)timestamp:62 ca a4 5a:4个字节

2)event_type:13:1个字节,TABLE_MAP_EVENT = 19

3)server-id:0b 00 00 00,4个字节,即11

4)event_size:2d 00 00 00,4个字节,即event总大小是45字节

5)next-log ps:ec 00 00 00,4个字节,即236

6)flag:00 00

----------私有事件头-------------

7)table ID:46 00 00 00 00 00,即table ID是70

8)flag:01 00,暂时未使用

----------事件体-------------------

9)schema name length:03,即schema name大小是3个字节

10)schema name:79 7a 73,yzs

11)00

12)table name length:02,即表名大小是2个字节

13)table name:74 31,即表名是t1

14)00

15)col count:02,即列个数是2

16)col type:03 03

enum enum_field_types { MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY,  
            MYSQL_TYPE_SHORT,  MYSQL_TYPE_LONG,  
            MYSQL_TYPE_FLOAT,  MYSQL_TYPE_DOUBLE,  
            MYSQL_TYPE_NULL,   MYSQL_TYPE_TIMESTAMP,  
            MYSQL_TYPE_LONGLONG,MYSQL_TYPE_INT24,  
            MYSQL_TYPE_DATE,   MYSQL_TYPE_TIME,  
            MYSQL_TYPE_DATETIME, MYSQL_TYPE_YEAR,  
            MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR,  
            MYSQL_TYPE_BIT,  
            MYSQL_TYPE_TIMESTAMP2,  
            MYSQL_TYPE_DATETIME2,  
            MYSQL_TYPE_TIME2,  
            MYSQL_TYPE_NEWDECIMAL=246,  
            MYSQL_TYPE_ENUM=247,  
            MYSQL_TYPE_SET=248,  
            MYSQL_TYPE_TINY_BLOB=249,  
            MYSQL_TYPE_MEDIUM_BLOB=250,  
            MYSQL_TYPE_LONG_BLOB=251,  
            MYSQL_TYPE_BLOB=252,  
            MYSQL_TYPE_VAR_STRING=253,  
            MYSQL_TYPE_STRING=254,  
            MYSQL_TYPE_GEOMETRY=255  

}; 

17)col meta length:00,1个字节

18)col meta:无

19)null bitmap:02,列数是2,一个字节就够:00000010,即主键列那列不能为NULL

20)checksum,不在事件体里。d8 b9 92 2b
write_footer函数最后会计算checksum,并放到事件体后。

virtual bool write(IO_CACHE* file)  
{  
  return(write_header(file, get_data_size()) ||  
  write_data_header(file) ||  
  write_data_body(file) ||  
  write_footer(file));  
}  
# at 191  
#180310 21:53:38 server id 11  end_log_pos 236 CRC32 0x2b92b9d8     Table_map: `yzs`.`t1` mapped to number 70 

可以看到和binlog文件里解析出来的一致。