binlog event解析

学习看一个gtid模式的binlog

配置文件


[mysqld]

####: for binlog
binlog_format                       =row                          #     row
binlog_error_action                 =abort_server          # abort_server
log_bin                             =D:\\tools\mysql_5.7.36\\mysql-5.7.36-winx64\\binlog\\mysql-bin                      #  off
binlog_rows_query_log_events        =on                             #   off
log_slave_updates                   =on                             #   off
expire_logs_days = 2
binlog_cache_size                   =65536                          #   65536(64k)
binlog_checksum                     =CRC32                           #  CRC32
sync_binlog                         =1                              #   1
slave_preserve_commit_order         =ON                             #  OFF 
server_id                           =297      

gtid_executed_compression_period    =1000                          #    1000
gtid_mode                           =on                            #    off
enforce_gtid_consistency            =on                            #    off

binlog_rows_query_log_events        =on         

执行SQL如下

mysql> begin;select now();
Query OK, 0 rows affected (0.00 sec)

+---------------------+
| now()               |
+---------------------+
| 2022-03-10 09:39:29 |
+---------------------+
1 row in set (0.00 sec)

mysql> insert into test values(10,'test');select now();
Query OK, 1 row affected (0.02 sec)

+---------------------+
| now()               |
+---------------------+
| 2022-03-10 09:39:50 |
+---------------------+
1 row in set (0.00 sec)

mysql> commit;select now();
Query OK, 0 rows affected (0.00 sec)

+---------------------+
| now()               |
+---------------------+
| 2022-03-10 09:40:43 |
+---------------------+
1 row in set (0.00 sec)

mysql>

binlog记录信息

mysqlbinlog -vv xxxx-000001 通过这个命令来查询binlog信息
以下解释在binlog里面备注。

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#220310  9:36:30 server id 297  end_log_pos 123 CRC32 0xf295b2a8        Start: binlog v 4, server v 5.7.36-log created 220310  9:36:30 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
HlYpYg8pAQAAdwAAAHsAAAABAAQANS43LjM2LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAeViliEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AaiylfI=
'/*!*/;
# at 123
#220310  9:36:30 server id 297  end_log_pos 194 CRC32 0x28c16622        Previous-GTIDs
# 9bb60b7a-9df4-11ec-944e-b4a9fcb9227b:1-2
# at 194
#220310  9:40:43 server id 297  end_log_pos 259 CRC32 0x5dcce782        GTID    last_committed=0        sequence_number=1       rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= '9bb60b7a-9df4-11ec-944e-b4a9fcb9227b:3'/*!*/;
# at 259
#220310  9:39:50 server id 297  end_log_pos 331 CRC32 0xfb63ce11        Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1646876390/*!*/;
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=1436549152/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C gbk *//*!*/;
SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 331
#220310  9:39:50 server id 297  end_log_pos 389 CRC32 0x4cf98aa5        Rows_query
# insert into test values(10,'test')
# at 389
#220310  9:39:50 server id 297  end_log_pos 439 CRC32 0xd137dbb0        Table_map: `test`.`test` mapped to number 108
# at 439
#220310  9:39:50 server id 297  end_log_pos 484 CRC32 0xf777fdc0        Write_rows: table id 108 flags: STMT_END_F

BINLOG '
5lYpYh0pAQAAOgAAAIUBAACAACJpbnNlcnQgaW50byB0ZXN0IHZhbHVlcygxMCwndGVzdCcppYr5
TA==
5lYpYhMpAQAAMgAAALcBAAAAAGwAAAAAAAEABHRlc3QABHRlc3QAAgMPAhQAA7DbN9E=
5lYpYh4pAQAALQAAAOQBAAAAAGwAAAAAAAEAAgAC//wKAAAABHRlc3TA/Xf3
'/*!*/;
### INSERT INTO `test`.`test`
### SET
###   @1=10 /* INT meta=0 nullable=1 is_null=0 */
###   @2='test' /* VARSTRING(20) meta=20 nullable=1 is_null=0 */
# at 484
#220310  9:40:43 server id 297  end_log_pos 515 CRC32 0xcdb4745f        Xid = 7
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

下面我每针对每一个event来进行解析

  • binlog日志头信息

这里面记录了每一个新的binlog头的信息,暂时不需要关心,可以看到里面有一些数据库版本信息等。

/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#220310  9:36:30 server id 297  end_log_pos 123 CRC32 0xf295b2a8        Start: binlog v 4, server v 5.7.36-log created 220310  9:36:30 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
HlYpYg8pAQAAdwAAAHsAAAABAAQANS43LjM2LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAeViliEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AaiylfI=
'/*!*/;
  • Previous_gtid_log_event

记录当前事务上一个gtid的event

# at 123
#220310  9:36:30 server id 297  end_log_pos 194 CRC32 0x28c16622        Previous-GTIDs
# 9bb60b7a-9df4-11ec-944e-b4a9fcb9227b:1-2
  • gtid_log_event
    记录当前事务gtid信息的event,注意看时间和commit的时间一样,所以可以得出,gtid信息是在事务提交的时候给分配的。

last_committed=0 ,这个值代表当前一个组内可以一起commit的最大sequence_number的值,这个值一致的事务可以在从库并行复制。

commit-parent 模式:last_committed在binlog组提交的时候共同的last_committed值才会并行复制。
commit-order 模式:会通过主键和unique key 哈希为一个哈希值,存在哈希表中,在并行复制的时候,即使不在同一个binlog组,只要新提交的值的哈希不在哈希表,就代表不冲突,就可以一起并行复制。

sequence_number=1 ,这个值代表在每一个binlog开始额度时候每个事务的递增值,初始值是1,每次加1。

rbr_only=yes 假如该值是yes.就是基于row格式,否则就包含statement格式。

# at 194
#220310  9:40:43 server id 297  end_log_pos 259 CRC32 0x5dcce782        GTID    last_committed=0        sequence_number=1       rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= '9bb60b7a-9df4-11ec-944e-b4a9fcb9227b:3'/*!*/;
  • Query_log_event

这里设置了一些session级别的值,并且记录的begin的语句

# at 259
#220310  9:39:50 server id 297  end_log_pos 331 CRC32 0xfb63ce11        Query   thread_id=2     exec_time=0     error_code=0
SET TIMESTAMP=1646876390/*!*/;
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=1436549152/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C gbk *//*!*/;
SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
  • row_query_log_event
    这里记录了事务执行的原始SQL语句,需要在配置文件开启binlog_rows_query_log_event=on 才可以看到。
# at 331
#220310  9:39:50 server id 297  end_log_pos 389 CRC32 0x4cf98aa5        Rows_query
# insert into test values(10,'test')
  • table_map_event

事务操作对应的表id

# at 389
#220310  9:39:50 server id 297  end_log_pos 439 CRC32 0xd137dbb0        Table_map: `test`.`test` mapped to number 108
  • write_rows_event

代表insert的事务操作记录和数据记录

# at 439
#220310  9:39:50 server id 297  end_log_pos 484 CRC32 0xf777fdc0        Write_rows: table id 108 flags: STMT_END_F

BINLOG '
5lYpYh0pAQAAOgAAAIUBAACAACJpbnNlcnQgaW50byB0ZXN0IHZhbHVlcygxMCwndGVzdCcppYr5
TA==
5lYpYhMpAQAAMgAAALcBAAAAAGwAAAAAAAEABHRlc3QABHRlc3QAAgMPAhQAA7DbN9E=
5lYpYh4pAQAALQAAAOQBAAAAAGwAAAAAAAEAAgAC//wKAAAABHRlc3TA/Xf3
'/*!*/;
### INSERT INTO `test`.`test`
### SET
###   @1=10 /* INT meta=0 nullable=1 is_null=0 */
###   @2='test' /* VARSTRING(20) meta=20 nullable=1 is_null=0 */
  • xid_event
    xid_event表示事务提交,就是commit。
    xid在redo和binlog都会有,从而崩溃恢复的时候,判断binlog假如有xix 就提交,没有就回滚。
# at 484
#220310  9:40:43 server id 297  end_log_pos 515 CRC32 0xcdb4745f        Xid = 7
COMMIT/*!*/;

每个event的产生时间

gtid_log_event ,xid_event 是在commit的时候产生的。

gtid_log_event,row_query_log_event,table_map_event,write_rows_enent 是在执行insert语句产生的。

你可能感兴趣的:(binlog event解析)