MYSQL如何识别一个binlog中的一个事物
短信预约 -IT技能 免费直播动态提醒
原创水平有限
测试版本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 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; 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 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; 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 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; 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
### @2=2
# 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';
所以我们认为一个事物的binlog是504到764
如果写为733会出现这种情况
mysqlbinlog testsla.000003 --start-position=504 --stop-position=733 -vv --base64-output=decode-rows
........
# at 689
#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
### @2=2
DELIMITER ;
# End of log file
ROLLBACK ; --很明显没有xid event 没有commit而mysqlbinlog自己家了一个rollback而回滚掉了
Gno:5
-->Query Event:Pos:338(0X152) N_Pos:428(0X1ac) Time:1496998879 Event_size:90(bytes)
Exe_time:0 Use_db:test Statment(35b-trun):delete from test Gno:5
>Xid Event:Pos:428(0X1ac) N_Pos:459(0X1cb) Time:1496998879 Event_size:31(bytes)
COMMIT; Gno:5
及
>GTID EVENT :事物开始
-->Query Event :begin
-->Query Event :正真的语句
>Xid Event: commit
没有Map Event和Insert Event,当然记录的是语句就节约空间了。
下面是mysqlbinlog输出
# at 194 --GTID EVENT开始
#170609 17:01:19 server id 933310 end_log_pos 259 CRC32 0x6a408c33 GTID [commit=yes]
SET @@SESSION.GTID_NEXT= '89dfa8a4-cb13-11e6-b504-000c29a879a3:5';
# at 259 --Query Event BEGIN
#170609 17:01:19 server id 933310 end_log_pos 338 CRC32 0x9b25b2af Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1496998879;
SET @@session.pseudo_thread_id=2;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1;
SET @@session.sql_mode=1075838976;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1;
;
SET @@session.character_set_client=83,@@session.collation_connection=83,@@session.collation_server=83;
SET @@session.lc_time_names=0;
SET @@session.collation_database=DEFAULT;
BEGIN
;
# at 338 --Query Event 正真的语句
#170609 17:01:19 server id 933310 end_log_pos 428 CRC32 0x4e4230f8 Query thread_id=2 exec_time=0 error_code=0
use `test`;
SET TIMESTAMP=1496998879;
delete from test
;
# at 428 -- XID EVENT结束
#170609 17:01:19 server id 933310 end_log_pos 459 CRC32 0x38079d60 Xid = 159
COMMIT;
作者微信:
测试版本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 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; 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 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; 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 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; 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
### @2=2
# 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';
所以我们认为一个事物的binlog是504到764
如果写为733会出现这种情况
mysqlbinlog testsla.000003 --start-position=504 --stop-position=733 -vv --base64-output=decode-rows
........
# at 689
#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
### @2=2
DELIMITER ;
# End of log file
ROLLBACK ; --很明显没有xid event 没有commit而mysqlbinlog自己家了一个rollback而回滚掉了
Gno:5
-->Query Event:Pos:338(0X152) N_Pos:428(0X1ac) Time:1496998879 Event_size:90(bytes)
Exe_time:0 Use_db:test Statment(35b-trun):delete from test Gno:5
>Xid Event:Pos:428(0X1ac) N_Pos:459(0X1cb) Time:1496998879 Event_size:31(bytes)
COMMIT; Gno:5
及
>GTID EVENT :事物开始
-->Query Event :begin
-->Query Event :正真的语句
>Xid Event: commit
没有Map Event和Insert Event,当然记录的是语句就节约空间了。
下面是mysqlbinlog输出
# at 194 --GTID EVENT开始
#170609 17:01:19 server id 933310 end_log_pos 259 CRC32 0x6a408c33 GTID [commit=yes]
SET @@SESSION.GTID_NEXT= '89dfa8a4-cb13-11e6-b504-000c29a879a3:5';
# at 259 --Query Event BEGIN
#170609 17:01:19 server id 933310 end_log_pos 338 CRC32 0x9b25b2af Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1496998879;
SET @@session.pseudo_thread_id=2;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1;
SET @@session.sql_mode=1075838976;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1;
;
SET @@session.character_set_client=83,@@session.collation_connection=83,@@session.collation_server=83;
SET @@session.lc_time_names=0;
SET @@session.collation_database=DEFAULT;
BEGIN
;
# at 338 --Query Event 正真的语句
#170609 17:01:19 server id 933310 end_log_pos 428 CRC32 0x4e4230f8 Query thread_id=2 exec_time=0 error_code=0
use `test`;
SET TIMESTAMP=1496998879;
delete from test
;
# at 428 -- XID EVENT结束
#170609 17:01:19 server id 933310 end_log_pos 459 CRC32 0x38079d60 Xid = 159
COMMIT;
作者微信:
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341