gtid
gtid全称:Global transaction identifiers,翻译成中文:全局事务标记,由server的uuid和transation id拼接而成。00015630-1111-1111-1111-111111111111:2
00015630-1111-1111-1111-111111111111:4-7
00015630-1111-1111-1111-111111111111:2:4-7:19
00015630-1111-1111-1111-111111111111:2:4-7:19,00015631-2222-2222-2222-222222222222:3-5
00015630-1111-1111-1111-111111111111是执行此事务的server的uuid,1-7是此server执行了从1到7这7个事务,transation id是连续的,随着事务的执行,gtid也是在一直变化的。
同一个服务器的多个gtid可以合并为一个表达式,如果是连续的gtid可以用中划线“-”拼接成一个表达式,多个不连续的gtid可以使用冒号":"拼接,不同服务器的gtid可以使用逗号“,”拼接。
gtid_purged
丢弃的所有gtid。show global variables like 'gtid_purged';
数据是gtid集合。已经执行过的,在binlog里不存在的gtid。它是gtid_executed 的子集。
binlog不存在的情况:没有开启binlog
手工删除了binlog
binlog过期,系统自动删除
Previous-GTIDs
Previous-GTIDs是在每一个binlog文件的开头,用于描述在此binlog生成之前,本服务器执行过的所有的GTID集合,不包括手动设置gtid_purged的值。
第一个binlog文件,Previous-GTIDs为空#200512 16:55:07 server id 101 end_log_pos 154 CRC32 0xa2339881 Previous-GTIDs
# [empty]
# at 154
此后的binlog文件#200513 10:36:12 server id 103 end_log_pos 194 CRC32 0x65733250 Previous-GTIDs
# 00015632-3333-3333-3333-333333333333:1-3
# at 194
gtid_executed
当前服务器已经执行的gtid集合。
系统执行事务的时候,会查询当前事务的gtid是否在gtid_excuted集合中,如果此事务gtid已经存在,则此事务会被系统忽略,不执行。
mysql5.7在mysql库里,有一个gtid_executed表,存的就是已经执行过的gtid值。CREATE TABLE `gtid_executed` (
`source_uuid` char(36) NOT NULL COMMENT 'uuid of the source where the transaction was originally executed.',
`interval_start` bigint(20) NOT NULL COMMENT 'First number of interval.',
`interval_end` bigint(20) NOT NULL COMMENT 'Last number of interval.',
PRIMARY KEY (`source_uuid`,`interval_start`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1mysql> select * from gtid_executed limit 5;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 00015630-1111-1111-1111-111111111111 | 1 | 1 |
| 00015630-1111-1111-1111-111111111111 | 2 | 2 |
| 00015630-1111-1111-1111-111111111111 | 3 | 3 |
| 00015630-1111-1111-1111-111111111111 | 4 | 4 |
| 00015630-1111-1111-1111-111111111111 | 5 | 5 |
+--------------------------------------+----------------+--------------+
5 rows in set (0.00 sec)
服务器会根据系统变量gtid_executed_compressionperiod的值对gtid_executed表进行压缩,gtid_executed_compressionperiod的值为200,表示执行200个事务后对gtid_executed表进行压缩,设置为0,表示不压缩。
压缩线程mysql> select * from performance_schema.threads where name like '%gtid%'\G
*************************** 1. row ***************************
THREAD_ID: 27
NAME: thread/sql/compress_gtid_table
TYPE: FOREGROUND
PROCESSLIST_ID: 1
PROCESSLIST_USER: NULL
PROCESSLIST_HOST: NULL
PROCESSLIST_DB: NULL
PROCESSLIST_COMMAND: Daemon
PROCESSLIST_TIME: 255913
PROCESSLIST_STATE: Suspending
PROCESSLIST_INFO: NULL
PARENT_THREAD_ID: 1
ROLE: NULL
INSTRUMENTED: YES
HISTORY: YES
CONNECTION_TYPE: NULL
THREAD_OS_ID: 6221
1 row in set (0.00 sec)
手工触发压缩flush logs;mysql> select * from mysql.gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 00015630-1111-1111-1111-111111111111 | 1 | 1 |
| 00015630-1111-1111-1111-111111111111 | 2 | 2 |
| 00015630-1111-1111-1111-111111111111 | 3 | 3 |
| 00015630-1111-1111-1111-111111111111 | 4 | 4 |
| 00015630-1111-1111-1111-111111111111 | 5 | 5 |
| 00015630-1111-1111-1111-111111111111 | 6 | 6 |
| 00015630-1111-1111-1111-111111111111 | 7 | 7 |
| 00015630-1111-1111-1111-111111111111 | 8 | 8 |
| 00015631-2222-2222-2222-222222222222 | 1 | 1 |
| 00015631-2222-2222-2222-222222222222 | 2 | 2 |
| 00015631-2222-2222-2222-222222222222 | 3 | 3 |
+--------------------------------------+----------------+--------------+
11 rows in set (0.00 sec)
mysql> flush logs;
Query OK, 0 rows affected (0.19 sec)
mysql> select * from mysql.gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 00015630-1111-1111-1111-111111111111 | 1 | 8 |
| 00015631-2222-2222-2222-222222222222 | 1 | 3 |
| 00015632-3333-3333-3333-333333333333 | 1 | 3 |
+--------------------------------------+----------------+--------------+
3 rows in set (0.00 sec)
gtid_next
下一个事务的gtid值。作用域是会话级别@SESSION
AUTOMATIC 自动生成gtid值,分配给下一个事务。它是gtid_next的默认值。
具体的GTID值,系统会把此GTID分配给下一个事务。
如果gtid_next值为空,系统无法分配gtid,则事务会失败。SET @@SESSION.GTID_NEXT='AUTOMATIC';
当gtid_next是AUTOMATIC时,系统会自动追回不连续的gtid。
binlog日志文件里的gtid_next是当前事务所用的gtid值#200513 14:49:19 server id 103 end_log_pos 259 CRC32 0x95726ab3 GTID last_committed=0 sequence_number=1 rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= '00015632-3333-3333-3333-333333333333:4'/*!*/;
# at 259
gtid_modeOFF: Both new and replicated transactions must be anonymous.(生成的是匿名事务,slave也只能应用匿名事务)
OFF_PERMISSIVE:(1) New transactions are anonymous. Replicated transactions can be either
anonymous or GTID transactions.(生成的是匿名事务,slave可以应用匿名和GTID事务)
ON_PERMISSIVE(2): New transactions are GTID transactions. Replicated transactions can be either
anonymous or GTID transactions.(生成的是GTID事务,slave可以应用匿名和GTID事务)
ON(3): Both new and replicated transactions must be GTID transactions(生成的是GTID事务,slave也只能应用GTID事务)