1. 程式人生 > >解析MySQL binlog --(4)TABLE_MAP_EVENT

解析MySQL binlog --(4)TABLE_MAP_EVENT

table_map_event

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格式定義
技術分享圖片
3、結合案例說明
技術分享圖片
--------公有事件頭-------

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文件裏解析出來的一致。

解析MySQL binlog --(4)TABLE_MAP_EVENT