1. 程式人生 > >解析MySQL binlog --(5)ROWS_EVENT:WRITE_ROWS_EVENT、UP

解析MySQL binlog --(5)ROWS_EVENT:WRITE_ROWS_EVENT、UP

row_event binlog

1、介紹

對於statement格式的binlog,所有增刪改的SQL語句都記錄在QUERY_EVENT中,而row格式的binlog則以ROWS_EVENT格式記錄對數據庫的修改。ROWS_EVENT分為3種:WRITE_ROWS_EVENT、UPDATE_ROWS_EVENT、DELETE_ROWS_EVENT,分別對應insert、update、delete。WRITE_ROWS_EVENT包含了要插入的數據;UPDATE_ROWS_EVENT不僅包含行修改後的值,也包括修改前的值;DELETE_ROWS_EVENT僅僅記錄刪除行的主鍵值。
2、定義格式
技術分享圖片
3、案例講解

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 */                                             |
| mysql-bin.000002 |  502 | Query       |        11 |         573 | BEGIN                                                           |
| mysql-bin.000002 |  573 | Table_map   |        11 |         618 | table_id: 70 (yzs.t1)                                           |
| mysql-bin.000002 |  618 | Write_rows  |        11 |         662 | table_id: 70 flags: STMT_END_F                                  |
| mysql-bin.000002 |  662 | Xid         |        11 |         693 | COMMIT /* xid=46 */                                             |
| mysql-bin.000002 |  693 | Query       |        11 |         764 | BEGIN                                                           |
| mysql-bin.000002 |  764 | Table_map   |        11 |         809 | table_id: 70 (yzs.t1)                                           |
| mysql-bin.000002 |  809 | Write_rows  |        11 |         853 | table_id: 70 flags: STMT_END_F                                  |
| mysql-bin.000002 |  853 | Xid         |        11 |         884 | COMMIT /* xid=47 */                                             |
| mysql-bin.000002 |  884 | Query       |        11 |         955 | BEGIN                                                           |
| mysql-bin.000002 |  955 | Table_map   |        11 |        1000 | table_id: 70 (yzs.t1)                                           |
| mysql-bin.000002 | 1000 | Write_rows  |        11 |        1044 | table_id: 70 flags: STMT_END_F                                  |
| mysql-bin.000002 | 1044 | Xid         |        11 |        1075 | COMMIT /* xid=56 */                                             |
| mysql-bin.000002 | 1075 | Query       |        11 |        1199 | use `yzs`; create table t2(id1 int primary key,id2 int,id3 int) |
| mysql-bin.000002 | 1199 | Query       |        11 |        1270 | BEGIN                                                           |
| mysql-bin.000002 | 1270 | Table_map   |        11 |        1316 | table_id: 71 (yzs.t2)                                           |
| mysql-bin.000002 | 1316 | Write_rows  |        11 |        1364 | table_id: 71 flags: STMT_END_F                                  |
| mysql-bin.000002 | 1364 | Xid         |        11 |        1395 | COMMIT /* xid=59 */                                             |
| mysql-bin.000002 | 1395 | Query       |        11 |        1466 | BEGIN                                                           |
| mysql-bin.000002 | 1466 | Table_map   |        11 |        1512 | table_id: 71 (yzs.t2)                                           |
| mysql-bin.000002 | 1512 | Delete_rows |        11 |        1560 | table_id: 71 flags: STMT_END_F                                  |
| mysql-bin.000002 | 1560 | Xid         |        11 |        1591 | COMMIT /* xid=60 */                                             |
| mysql-bin.000002 | 1591 | Query       |        11 |        1662 | BEGIN                                                           |
| mysql-bin.000002 | 1662 | Table_map   |        11 |        1708 | table_id: 71 (yzs.t2)                                           |
| mysql-bin.000002 | 1708 | Write_rows  |        11 |        1756 | table_id: 71 flags: STMT_END_F                                  |
| mysql-bin.000002 | 1756 | Xid         |        11 |        1787 | COMMIT /* xid=63 */                                             |
+------------------+------+-------------+-----------+-------------+-----------------------------------------------------------------+
34 rows in set (0.00 sec)

3.1 insert
mysqlbinlog工具看到的日誌:

# at 1316  
#180324 23:03:59 server id 11  end_log_pos 1364 CRC32 0xb23b2d4f    Write_rows: table id 71 flags: STMT_END_F  

BINLOG ‘  
zzu3WhMLAAAALgAAACQFAAAAAEcAAAAAAAEAA3l6cwACdDIAAwMDAwAGU9yKKQ==  
zzu3Wh4LAAAAMAAAAFQFAAAAAEcAAAAAAAEAAgAD//gBAAAAAQAAAAEAAABPLTuy  
‘/*!*/;  
### INSERT INTO `yzs`.`t2`  
### SET  
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */  
###   @2=1 /* INT meta=0 nullable=1 is_null=0 */  
###   @3=1 /* INT meta=0 nullable=1 is_null=0 */  
# at 1364  

技術分享圖片
------公有事件頭-----

1)timestamp:cf 3b b7 5a,4個字節

2)event_type:1e,1個字節,WRITE_ROWS_EVENT = 30

3)server-id:0b 00 00 00,4個字節即11

4)event_size:30 00 00 00,4個字節,即該event總大小是48字節

5)next-log pos:54 05 00 00,4個字節,即下一個log的位置是1364字節位置

6)flag:00 00

-------私有事件頭------

7)table ID:47 00 00 00 00 00,6個字節,即71

8)flag:01 00,2個字節,可以包含以下信息。該事件是否是語句的最後一個事件,是否需要進行外鍵約束檢查,針對innodb的二級索引是否需要進行唯一性檢查,該事件是否包含了完整一行數據也就是說覆蓋了所有列。

9)Var-size header len:02 00

10)RW_V_EXTRAINFO_TAG、m_extra_row_data:沒有

---------事件體---------

11)列個數:03,即3個列

12)columns-present-bitmap:ff,包含了所有列的數據

13)null-bitmap:f8,即1111 1000,即3個列都不是NULL

14)列值:01 00 00 00 01 00 00 00 01 00 00 00,即3個列的值是1,1,1

15)checksum:4f 2d 3b b2

2.2 delete

# at 1512  
#180325  0:19:45 server id 11  end_log_pos 1560 CRC32 0xc6b58a5e    Delete_rows: table id 71 flags: STMT_END_F  

BINLOG ‘  
kU23WhMLAAAALgAAAOgFAAAAAEcAAAAAAAEAA3l6cwACdDIAAwMDAwAG0hGsLw==  
kU23WiALAAAAMAAAABgGAAAAAEcAAAAAAAEAAgAD//gBAAAAAQAAAAEAAABeirXG  
‘/*!*/;  
### DELETE FROM `yzs`.`t2`  
### WHERE  
###   @1=1 /* INT meta=0 nullable=0 is_null=0 */  
###   @2=1 /* INT meta=0 nullable=1 is_null=0 */  
###   @3=1 /* INT meta=0 nullable=1 is_null=0 */  
# at 1560  

技術分享圖片
和insert的類型差不多,這裏只看最後存的值:

f8 01 00 00 00 01 00 00 00 01 00 00 00

即存儲的是delete的行值。

update類似,就不再使用案例講解,感興趣的可以按照格式解析。

解析MySQL binlog --(5)ROWS_EVENT:WRITE_ROWS_EVENT、UP