binlog 相关命令 event事件
录下当前日志的文件名和偏移位置,在后续查看日志过程中可以准确定位
show master status;
show variables like '%log_bin%';
-- 确认日志已经正确启用,这里也可以查看日志文件的具体路径
show binlog events in 'mysql-bin.000101';
show BINARY logs;
查看当前所有保存的binlog文件名和大小
一、show binlog events命令的格式
show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];
说明:
(1)IN ‘log_name’:指定要查询的binlog文件名(如果省略此参数,则默认指定第一个binlog文件);
(2)FROM pos:指定从哪个pos起始点开始查起(如果省略此参数,则从整个文件的第一个pos点开始算);
(3)LIMIT【offset】:偏移量(默认为0);
(4)row_count:查询总条数(如果省略,则显示所有行)。
show binlog events;
会查看到所有binlog文件
show binlog events in 'mysql-bin.000002';
show binlog events in 'mysql-bin.000002' from 417;
show binlog events in 'mysql-bin.000002' from 219 limit 5;
show binlog events in 'mysql-bin.000002' from 219 limit 2,5;
binlog文件中存储的内容称之为二进制事件,简称事件。我们的每一个数据库更新操作(Insert、Update、Delete等),都会对应的一个事件。使用show binlog events命令可以以列表的形式显示日志中的事件信息。
一、show binlog events命令的格式
show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];
1
说明:
(1)IN ‘log_name’:指定要查询的binlog文件名(如果省略此参数,则默认指定第一个binlog文件);
(2)FROM pos:指定从哪个pos起始点开始查起(如果省略此参数,则从整个文件的第一个pos点开始算);
(3)LIMIT【offset】:偏移量(默认为0);
(4)row_count:查询总条数(如果省略,则显示所有行)。
1、查看binlog文件信息
mysql> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 923 |
| mysql-bin.000002 | 154 |
+------------------+-----------+
2 rows in set (0.00 sec)
1
2、查询第一个binlog日志
mysql> show binlog events;
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.27-log, Binlog ver: 4 |
| mysql-bin.000001 | 123 | Previous_gtids | 1 | 154 | |
| mysql-bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 219 | Query | 1 | 291 | BEGIN |
| mysql-bin.000001 | 291 | Table_map | 1 | 339 | table_id: 113 (hist.t2) |
| mysql-bin.000001 | 339 | Write_rows | 1 | 413 | table_id: 113 flags: STMT_END_F |
| mysql-bin.000001 | 413 | Xid | 1 | 444 | COMMIT /* xid=16 / |
| mysql-bin.000001 | 444 | Anonymous_Gtid | 1 | 509 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 509 | Query | 1 | 581 | BEGIN |
| mysql-bin.000001 | 581 | Table_map | 1 | 629 | table_id: 113 (hist.t2) |
| mysql-bin.000001 | 629 | Update_rows | 1 | 845 | table_id: 113 flags: STMT_END_F |
| mysql-bin.000001 | 845 | Xid | 1 | 876 | COMMIT / xid=17 */ |
| mysql-bin.000001 | 876 | Rotate | 1 | 923 | mysql-bin.000002;pos=4 |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
13 rows in set (0.00 sec)
1
3、查询第二个binlog日志
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 | 1 | 123 | Server ver: 5.7.27-log, Binlog ver: 4 |
| mysql-bin.000002 | 123 | Previous_gtids | 1 | 154 | |
| mysql-bin.000002 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000002 | 219 | Query | 1 | 291 | BEGIN |
| mysql-bin.000002 | 291 | Table_map | 1 | 336 | table_id: 112 (hist.t1) |
| mysql-bin.000002 | 336 | Write_rows | 1 | 386 | table_id: 112 flags: STMT_END_F |
| mysql-bin.000002 | 386 | Xid | 1 | 417 | COMMIT /* xid=39 / |
| mysql-bin.000002 | 417 | Anonymous_Gtid | 1 | 482 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000002 | 482 | Query | 1 | 554 | BEGIN |
| mysql-bin.000002 | 554 | Table_map | 1 | 599 | table_id: 112 (hist.t1) |
| mysql-bin.000002 | 599 | Write_rows | 1 | 649 | table_id: 112 flags: STMT_END_F |
| mysql-bin.000002 | 649 | Xid | 1 | 680 | COMMIT / xid=40 */ |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
12 rows in set (0.00 sec)
4、查询mysql-bin.000002文件,从pos点417开始查询
mysql> show binlog events in 'mysql-bin.000002' from 417;
+------------------+-----+----------------+-----------+-------------+--------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+--------------------------------------+
| mysql-bin.000002 | 417 | Anonymous_Gtid | 1 | 482 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000002 | 482 | Query | 1 | 554 | BEGIN |
| mysql-bin.000002 | 554 | Table_map | 1 | 599 | table_id: 112 (hist.t1) |
| mysql-bin.000002 | 599 | Write_rows | 1 | 649 | table_id: 112 flags: STMT_END_F |
| mysql-bin.000002 | 649 | Xid | 1 | 680 | COMMIT /* xid=40 */ |
+------------------+-----+----------------+-----------+-------------+--------------------------------------+
5 rows in set (0.00 sec)
1
5、查询mysql-bin.000002文件,从pos点219开始查询,查询5条记录
mysql> show binlog events in 'mysql-bin.000002' from 219 limit 5;
+------------------+-----+----------------+-----------+-------------+--------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+--------------------------------------+
| mysql-bin.000002 | 219 | Query | 1 | 291 | BEGIN |
| mysql-bin.000002 | 291 | Table_map | 1 | 336 | table_id: 112 (hist.t1) |
| mysql-bin.000002 | 336 | Write_rows | 1 | 386 | table_id: 112 flags: STMT_END_F |
| mysql-bin.000002 | 386 | Xid | 1 | 417 | COMMIT /* xid=39 */ |
| mysql-bin.000002 | 417 | Anonymous_Gtid | 1 | 482 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
+------------------+-----+----------------+-----------+-------------+--------------------------------------+
5 rows in set (0.00 sec)
1
6、查询mysql-bin.000002文件,从pos点219开始查询,查询5条记录,偏移2行
mysql> show binlog events in 'mysql-bin.000002' from 219 limit 2,5;
+------------------+-----+----------------+-----------+-------------+--------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+--------------------------------------+
| mysql-bin.000002 | 336 | Write_rows | 1 | 386 | table_id: 112 flags: STMT_END_F |
| mysql-bin.000002 | 386 | Xid | 1 | 417 | COMMIT /* xid=39 */ |
| mysql-bin.000002 | 417 | Anonymous_Gtid | 1 | 482 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000002 | 482 | Query | 1 | 554 | BEGIN |
| mysql-bin.000002 | 554 | Table_map | 1 | 599 | table_id: 112 (hist.t1) |
+------------------+-----+----------------+-----------+-------------+--------------------------------------+
5 rows in set (0.00 sec)
1
2
二、binlog管理事件
使用show binlog events命令查询到的每一行数据就是一个binlog管理事件。当使用mysqlbinlog查看日志信息时,每两个【at 数字】之间的信息就对应一个管理事件。
mysql> show binlog events in '/var/lib/mysql/mysql-bin.000002';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000002 | 4 | Format_desc | 2 | 123 | Server ver: 5.7.27-log, Binlog ver: 4 |
| mysql-bin.000002 | 123 | Previous_gtids | 2 | 154 | |
| mysql-bin.000002 | 154 | Anonymous_Gtid | 2 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000002 | 219 | Query | 2 | 298 | BEGIN |
| mysql-bin.000002 | 298 | Query | 2 | 397 | use hist
; update t2 set id=id+10000 |
| mysql-bin.000002 | 397 | Xid | 2 | 428 | COMMIT /* xid=11 */ |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
6 rows in set (0.00 sec)
1
1、每一个管理事件对应的信息说明
(1)Log_name:当前事件所在的binlog文件名称;
(2)Pos:当前事件的开始位置,每个事件都占用固定的字节大小,结束位置(End_log_position)减去Pos,就是这个事件占用的字节数。第一个事件位置并不是从0开始,而是从4。Mysql通过文件中的前4个字节,来判断这是不是一个binlog文件。这种方式很常见,很多格式的文件,如pdf、doc、jpg等,都会通常前几个特定字符判断是否是合法文件。
(3)Event_type:表示事件的类型;
(4)Server_id:表示产生这个事件的mysql server_id,通过设置my.cnf中的server-id选项进行配置;
(5)End_log_position:下一个事件的开始位置;
(6)Info:当前事件的描述信息。
2、Statement模式下的事件说明
以下是一个Statement日志格式的管理事件信息:
mysql> show binlog events in '/var/lib/mysql/mysql-bin.000001';
+------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 2 | 123 | Server ver: 5.7.27-log, Binlog ver: 4 |
| mysql-bin.000001 | 123 | Previous_gtids | 2 | 154 | |
| mysql-bin.000001 | 154 | Anonymous_Gtid | 2 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 219 | Query | 2 | 298 | BEGIN |
| mysql-bin.000001 | 298 | Intvar | 2 | 330 | INSERT_ID=2 |
| mysql-bin.000001 | 330 | Query | 2 | 456 | use hist
; insert into t2(name) values('zhang'),('wang'),('li') |
| mysql-bin.000001 | 456 | Xid | 2 | 487 | COMMIT /* xid=31 / |
| mysql-bin.000001 | 487 | Anonymous_Gtid | 2 | 552 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 552 | Query | 2 | 631 | BEGIN |
| mysql-bin.000001 | 631 | Intvar | 2 | 663 | INSERT_ID=5 |
| mysql-bin.000001 | 663 | Query | 2 | 789 | use hist
; insert into t2(name) values('jack'),('tom'),('rose') |
| mysql-bin.000001 | 789 | Xid | 2 | 820 | COMMIT / xid=32 / |
| mysql-bin.000001 | 820 | Anonymous_Gtid | 2 | 885 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 885 | Query | 2 | 964 | BEGIN |
| mysql-bin.000001 | 964 | Query | 2 | 1063 | use hist
; update t2 set id=id+10000 |
| mysql-bin.000001 | 1063 | Xid | 2 | 1094 | COMMIT / xid=33 / |
| mysql-bin.000001 | 1094 | Anonymous_Gtid | 2 | 1159 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 1159 | Query | 2 | 1250 | create database wgx |
| mysql-bin.000001 | 1250 | Anonymous_Gtid | 2 | 1315 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 1315 | Query | 2 | 1451 | use wgx
; create table t1(id int primary key auto_increment,name char(20)) |
| mysql-bin.000001 | 1451 | Anonymous_Gtid | 2 | 1516 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 1516 | Query | 2 | 1593 | BEGIN |
| mysql-bin.000001 | 1593 | Intvar | 2 | 1625 | INSERT_ID=1 |
| mysql-bin.000001 | 1625 | Query | 2 | 1741 | use wgx
; insert into t1(name) values('zhang'),('tom') |
| mysql-bin.000001 | 1741 | Xid | 2 | 1772 | COMMIT / xid=44 */ |
| mysql-bin.000001 | 1772 | Stop | 2 | 1795 | |
+------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------+
26 rows in set (0.01 sec)
说明:
(1)每个binlog文件总是以Format Description Event作为开始,以Rotate Event(Stop Event)作为结束。在开始和结束之间,穿插着其他各种事件。
(2)每个事务都是以Query Event作为开始,其INFO列内容为BEGIN,以Xid Event表示结束,其INFO列内容为COMMIT。即使对于单条更新SQL我们没有开启事务,Mysql也会默认的帮我们开启事务。
(3)Intvar Event事件:如果主键是自增(AUTO_INCREMENT)列,MySQL首先会自增一个值,这就是Intvar Event的作用。需要注意的是,该事件,只会在Statement模式下出现。
(4)Query Event:记录的就是插入的SQL。这也体现了Statement模式的作用,就是记录执行的SQL。
(5)Stop Event:表示日志文件的结束。
2、Row模式下的事件说明
以下是一个Row日志格式的管理事件信息:
mysql> show binlog events;
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.27-log, Binlog ver: 4 |
| mysql-bin.000001 | 123 | Previous_gtids | 1 | 154 | |
| mysql-bin.000001 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 219 | Query | 1 | 291 | BEGIN |
| mysql-bin.000001 | 291 | Table_map | 1 | 339 | table_id: 113 (hist.t2) |
| mysql-bin.000001 | 339 | Write_rows | 1 | 413 | table_id: 113 flags: STMT_END_F |
| mysql-bin.000001 | 413 | Xid | 1 | 444 | COMMIT /* xid=16 / |
| mysql-bin.000001 | 444 | Anonymous_Gtid | 1 | 509 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000001 | 509 | Query | 1 | 581 | BEGIN |
| mysql-bin.000001 | 581 | Table_map | 1 | 629 | table_id: 113 (hist.t2) |
| mysql-bin.000001 | 629 | Update_rows | 1 | 845 | table_id: 113 flags: STMT_END_F |
| mysql-bin.000001 | 845 | Xid | 1 | 876 | COMMIT / xid=17 */ |
| mysql-bin.000001 | 876 | Rotate | 1 | 923 | mysql-bin.000002;pos=4 |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
13 rows in set (0.00 sec)
说明:
(1)每个binlog文件总是以Format Description Event作为开始,以Rotate Event(Stop Event)作为结束。在开始和结束之间,穿插着其他各种事件。
(2)TABLE_MAP EVENT:其作用是记录INSERT、DELETE、UPDATE操作的表结构。
(3)Write_rows:插入记录。
(4)Update_rows:更新记录。
(5)Rotate Event:表示日志文件的结束。
ddl event的例子
-- full image
是个query事件。 具体的,官网有写。
一个事务内多次更新:
会分成多个事件。
然后, 如果有default的值,binlog里也会有, binlog存的是所有的, 变更前后的字段(好像是full 模式下才行)
删除的时候, 存的是原行。 时间肯定不更新,用的是原值。
同一个事务, 不同的update , 更新时间是不一样的
ddl:
关闭bi-frost的一点记录
-- 如果断了, 找这个有gtid的, 对应的setsessionid的 , 才是一段事务日志的开始
关于binlog cdc 相关的事务, 顺序性问题。
目前测试cdc工具都可以保证吐出来的顺序, 是按照事务commit的顺序放出来的。 而且, rollback 消息不会发送给client端。 所以client端省事很多。
再就是xid的问题 。xid是事务的开始顺序, 而不是提交顺序, 所以client不能按照这个做顺序依据。
gtid是SERVERID(看着像uuid)加单调递增的整数,在事务提交时产生,反应了事务的提交顺序。
GTID = source_id:transaction_id
参考
https://blog.51cto.com/u_13874232/5457530
https://juejin.cn/post/7201800584310833207
https://dev.mysql.com/doc/refman/5.7/en/replication-gtids-concepts.html ----看官网更有感觉
参考
https://blog.csdn.net/weixin_44377973/article/details/107122020