MySQL识别一个binlog中的一个事物

MySQL测试版本5.7.14
设置GTID_MODE=ON
ON(3): Both new and replicated transactions must be GTID transactions(生成的是GTID事物,slave也只能应用GTID事物)
设置binlog格式为row模式
做如下操作
mysql> insert into test values(1,2);
Query OK, 1 row affected (0.01 sec)
mysql> insert into test values(2,3);
Query OK, 1 row affected (0.01 sec)
mysql> delete from test;
Query OK, 2 rows affected (0.01 sec)
mysql> select * from test;
Empty set (0.00 sec)

首先我通过自己的工具infobin找到了这段操作的binlog,如果想获得这个工具学习可以参考文章最后
简单解释一下这里  Pos:是当前位置对应mysqlbinlog的 # at 504这里的N_pos是结束位置对应
mysqlbinlog的 end_log_pos
>Gtid Event:Pos:504(0X1f8) N_pos:569(0X239) Time:1496993578 Event_size:65(bytes)
Gtid:89dfa8a4-cb13-11e6-b54-0c29a879a3:2
-->Query Event:Pos:569(0X239) N_Pos:641(0X281) Time:1496993578 Event_size:72(bytes)
Exe_time:0  Use_db:test Statment(35b-trun):BEGIN /*!Trx begin!*/ Gno:2
---->Map Event:Pos641(0X281) N_pos:689(0X2b1) Time:1496993578 Event_size:48(bytes)
TABLE_ID:142 DB_NAME:test TABLE_NAME:test Gno:2
------>Insert Event:Pos:689(0X2b1) N_pos:733(0X2dd) Time:1496993578 Event_size:44(bytes)
Dml on table: test.test  table_id:142 Gno:2
>Xid Event:Pos:733(0X2dd) N_Pos:764(0X2fc) Time:1496993578 Event_size:31(bytes)
COMMIT; /*!Trx end*/ Gno:2 --注意这里以N_Pos为结尾及下一个event的开始位置
>Gtid Event:Pos:764(0X2fc) N_pos:829(0X33d) Time:1496993581 Event_size:65(bytes)
Gtid:89dfa8a4-cb13-11e6-b54-0c29a879a3:3
-->Query Event:Pos:829(0X33d) N_Pos:901(0X385) Time:1496993581 Event_size:72(bytes)
Exe_time:0  Use_db:test Statment(35b-trun):BEGIN /*!Trx begin!*/ Gno:3
---->Map Event:Pos901(0X385) N_pos:949(0X3b5) Time:1496993581 Event_size:48(bytes)
TABLE_ID:142 DB_NAME:test TABLE_NAME:test Gno:3
------>Insert Event:Pos:949(0X3b5) N_pos:993(0X3e1) Time:1496993581 Event_size:44(bytes)
Dml on table: test.test  table_id:142 Gno:3
>Xid Event:Pos:993(0X3e1) N_Pos:1024(0X400) Time:1496993581 Event_size:31(bytes)
COMMIT; /*!Trx end*/ Gno:3  --注意这里以N_Pos为结尾及下一个event的开始位置
>Gtid Event:Pos:1024(0X400) N_pos:1089(0X441) Time:1496993584 Event_size:65(bytes)
Gtid:89dfa8a4-cb13-11e6-b54-0c29a879a3:4
-->Query Event:Pos:1089(0X441) N_Pos:1161(0X489) Time:1496993584 Event_size:72(bytes)
Exe_time:0  Use_db:test Statment(35b-trun):BEGIN /*!Trx begin!*/ Gno:4
---->Map Event:Pos1161(0X489) N_pos:1209(0X4b9) Time:1496993584 Event_size:48(bytes)
TABLE_ID:142 DB_NAME:test TABLE_NAME:test Gno:4
------>Delete Event:Pos:1209(0X4b9) N_pos:1262(0X4ee) Time:1496993584 Event_size:53(bytes)
Dml on table: test.test  table_id:142 Gno:4
>Xid Event:Pos:1262(0X4ee) N_Pos:1293(0X50d) Time:1496993584 Event_size:31(bytes)
COMMIT; /*!Trx end*/ Gno:4 --注意这里以N_Pos为结尾及下一个event的开始位置

显然这里包含了3个事物,
1、504到764为一个事物,工具显示这个event为Insert Event,在表test.test
2、764到1024为一个事物,工具显示这个event为Insert Event,在表test.test
3、1024到1293为一个事物,工具显示这个event为Delete Event,在表test.test

这就是我做的操作,这个工具主要是通过分析binlog event方便寻找事物,当然mysqlbinlog也可以只是输出有点不直观。
在通过mysqlbinlog分析的时候一定要注意一个事物的开始和结束。
(mysqlbinlog testsla.000003  -vv --start-postions=504 --stop-postions=1024 --base64-output=decode-rows 查看不通过base64算法显示二进制内容)
(mysqlbinlog testsla.000003  -vv --start-postions=504 --stop-postions=1024 查看通过base64算法显示二进制内容)

下面我们通过mysqlbinlog来分析上面的事物1 504到764为
# at 473
#170609 15:20:45 server id 933310  end_log_pos 504 CRC32 0x609296d7    Xid = 161
COMMIT/*!*/; ---注意这里上一个事物的结束叫做xid event
# at 504    ---这里是事物1 的起点没叫做gtid event
#170609 15:32:58 server id 933310  end_log_pos 569 CRC32 0xf7eebfc7    GTID [commit=yes]
SET @@SESSION.GTID_NEXT= '89dfa8a4-cb13-11e6-b504-000c29a879a3:2'/*!*/;
# at 569    ---这段event是query event
#170609 15:32:58 server id 933310  end_log_pos 641 CRC32 0xb4caa78c    Query  thread_id=4    exec_time=0    error_code=0
SET TIMESTAMP=1496993578/*!*/;
BEGIN
/*!*/;
# at 641    ---这段event是map event
#170609 15:32:58 server id 933310  end_log_pos 689 CRC32 0xb055655f    Table_map: `test`.`test` mapped to number 142
# at 689    ---这段event是insert event
#170609 15:32:58 server id 933310  end_log_pos 733 CRC32 0xd907a353    Write_rows: table id 142 flags: STMT_END_F
### INSERT INTO `test`.`test`
### SET
###  @1=1 /* INT meta=0 nullable=1 is_null=0 */
###  @2=2 /* INT meta=0 nullable=1 is_null=0 */
# at 733    --这段event是xid event
#170609 15:32:58 server id 933310  end_log_pos 764 CRC32 0x9dbe0a6b    Xid = 323
COMMIT/*!*/;  ---这里是一个事物的结尾叫做xid event,但是注意不是733而是下一个event开始的位置764 或者是 xid event 的end_log_pos ,否则将会被回滚掉
# at 764
#170609 15:33:01 server id 933310  end_log_pos 829 CRC32 0x82aac64c    GTID [commit=yes]
SET @@SESSION.GTID_NEXT= '89dfa8a4-cb13-11e6-b504-000c29a879a3:3'/*!*/;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e950d1b7b298abb2a803bc2ebd6b4385.html