GTID 格式和存储
全局事务标识符 (GTID) 是在源服务器(源)上创建并与提交的每个事务相关联的唯一标识符。这个标识符不仅对于它起源的服务器是唯一的,而且在给定的复制拓扑中的所有服务器中都是唯一的。
GTID 分配区分在源上提交的客户端事务和在副本上复制的复制事务。当客户端事务在源上提交时,它会被分配一个新的 GTID,前提是该事务已写入二进制日志。保证客户端事务具有单调递增的 GTID,生成的数字之间没有间隙。如果客户端事务没有写入二进制日志(例如,因为事务被过滤掉,或者事务是只读的),则不会在源服务器上为其分配 GTID。
复制的事务保留分配给源服务器上事务的相同 GTID。GTID 在复制事务开始执行之前就存在,即使复制事务没有写入副本上的二进制日志,或者在副本上被过滤掉,GTID 也会保留下来。MySQL 系统表mysql.gtid_executed
用于保存在 MySQL 服务器上应用的所有事务的分配 GTID,但存储在当前活动的二进制日志文件中的事务除外。
GTID 的自动跳过功能意味着在源上提交的事务只能在副本上应用一次,这有助于保证一致性。一旦在给定服务器上提交了具有给定 GTID 的事务,该服务器将忽略任何尝试执行具有相同 GTID 的后续事务的尝试。不会引发错误,也不会执行事务中的任何语句。
如果具有给定 GTID 的事务已开始在服务器上执行,但尚未提交或回滚,则任何尝试在具有相同 GTID 的服务器上启动并发事务的尝试都会阻塞。服务器既不开始执行并发事务也不将控制权返回给客户端。一旦事务的第一次尝试提交或回滚,在同一 GTID 上阻塞的并发会话可能会继续。如果第一次尝试回滚,则一个并发会话继续尝试该事务,并且阻塞在同一 GTID 上的任何其他并发会话仍然被阻塞。如果第一次尝试提交,所有并发会话停止被阻塞,并自动跳过事务的所有语句。
GTID 表示为一对坐标,由冒号字符 ( :
) 分隔,如下所示:
<span style="background-color:#ffffff"><span style="color:#555555"><span style="background-color:#f8f8f8"><span style="color:#000000"><code class="language-ini"><span style="color:#990055">GTID</span> <span style="color:#0077aa"><span style="color:#999999">=</span> <em>source_id</em>:<em>transaction_id</em></span></code></span></span></span></span>
source_id
标识始发服务器 。通常,源 server_uuid用于此目的。这transaction_id
是一个序列号,由在源上提交事务的顺序确定。例如,要提交的第一个事务具有1
作为其的 transaction_id
,而要在同一原始服务器上提交的第十个事务被分配transaction_id
为 10
。交易不可能0
在 GTID 中作为序列号。例如,最初在具有 UUID 的服务器上提交的第 23 个事务 3E11FA47-71CA-11E1-9E33-C80AA9429562
具有此 GTID:
<span style="background-color:#ffffff"><span style="color:#555555"><span style="background-color:#f8f8f8"><span style="color:#000000"><code class="language-none">3E11FA47-71CA-11E1-9E33-C80AA9429562:23</code></span></span></span></span>
服务器实例上 GTID 的序列号的上限是有符号 64 位整数(2 的 63 次幂减 1,或 9,223,372,036,854,775,807)的非负值的数量。如果服务器用完 GTID,它会采取 binlog_error_action. 从 MySQL 8.0.23 开始,当服务器实例接近限制时会发出警告消息。
事务的 GTID 显示在 mysqlbinlog的输出中,它用于标识性能模式复制状态表中的单个事务,例如 replication_applier_status_by_worker. gtid_next 系统变量 ( )存储的值@@GLOBAL.gtid_next
是单个 GTID。
GTID 集
GTID 集是包含一个或多个单个 GTID 或 GTID 范围的集合。GTID 集以多种方式在 MySQL 服务器中使用。例如,由 gtid_executed和 gtid_purged系统变量存储的值是 GTID 集。( START REPLICA或在 MySQL 8.0.22 之前, START SLAVE)子句UNTIL SQL_BEFORE_GTIDS
可UNTIL SQL_AFTER_GTIDS
用于使副本处理事务仅到 GTID 集中的第一个 GTID,或在 GTID 集中的最后一个 GTID 之后停止。内置函数 GTID_SUBSET()并 GTID_SUBTRACT()需要 GTID 集作为输入。
来自同一服务器的一系列 GTID 可以折叠成一个表达式,如下所示:
<span style="background-color:#ffffff"><span style="color:#555555"><span style="background-color:#f8f8f8"><span style="color:#000000"><code class="language-none">3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5</code></span></span></span></span>
上面的示例表示源自 MySQL 服务器的第一到第五个事务,其 server_uuid为 3E11FA47-71CA-11E1-9E33-C80AA9429562
. 来自同一服务器的多个单个 GTID 或 GTID 范围也可以包含在单个表达式中,GTID 或范围用冒号分隔,如下例所示:
<span style="background-color:#ffffff"><span style="color:#555555"><span style="background-color:#f8f8f8"><span style="color:#000000"><code class="language-none">3E11FA47-71CA-11E1-9E33-C80AA9429562:1-3:11:47-49</code></span></span></span></span>
GTID 集可以包括单个 GTID 和 GTID 范围的任意组合,并且它可以包括源自不同服务器的 GTID。此示例显示了存储在 gtid_executed系统变量 ( @@GLOBAL.gtid_executed
) 中的 GTID 集,该副本应用了来自多个来源的事务:
<span style="background-color:#ffffff"><span style="color:#555555"><span style="background-color:#f8f8f8"><span style="color:#000000"><code class="language-none">2174B383-5441-11E8-B90A-C80AA9429562:1-3, 24DA167-0C0C-11E8-8442-00059A3C7B00:1-19</code></span></span></span></span>
从服务器变量返回 GTID 集时,UUID 按字母顺序排列,数字区间按升序合并。
GTID 集的语法如下:
<span style="background-color:#ffffff"><span style="color:#555555"><span style="background-color:#f8f8f8"><span style="color:#000000"><code class="language-sql"><em>gtid_set</em>:
<em>uuid_set</em> <span style="color:#999999">[</span><span style="color:#999999">,</span> <em>uuid_set</em><span style="color:#999999">]</span> <span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span>
<span style="color:#a67f59">|</span> <span style="color:#669900">''</span>
<em>uuid_set</em>:
<em>uuid</em>:<span style="color:#0077aa"><em>interval</em></span><span style="color:#999999">[</span>:<span style="color:#0077aa"><em>interval</em></span><span style="color:#999999">]</span><span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span>
<em>uuid</em>:
<em>hhhhhhhh</em><span style="color:#a67f59">-</span><em>hhhh</em><span style="color:#a67f59">-</span><em>hhhh</em><span style="color:#a67f59">-</span><em>hhhh</em><span style="color:#a67f59">-</span><em>hhhhhhhhhhhh</em>
<em>h</em>:
<span style="color:#999999">[</span><span style="color:#990055">0</span><span style="color:#a67f59">-</span><span style="color:#990055">9</span><span style="color:#a67f59">|</span>A<span style="color:#a67f59">-</span>F<span style="color:#999999">]</span>
<span style="color:#0077aa"><em>interval</em></span>:
<em>n</em><span style="color:#999999">[</span><span style="color:#a67f59">-</span><em>n</em><span style="color:#999999">]</span>
<span style="color:#999999">(</span><em>n</em> <span style="color:#a67f59">>=</span> <span style="color:#990055">1</span><span style="color:#999999">)</span></code></span></span></span></span>
mysql.gtid_executed 表
GTID 存储在数据库中名为 的表 gtid_executed
中 mysql
。此表中的一行包含,对于它所代表的每个 GTID 或 GTID 集,原始服务器的 UUID,以及该集的开始和结束事务 ID;对于仅引用单个 GTID 的行,最后两个值是相同的。
该mysql.gtid_executed
表在安装或升级 MySQL 服务器时创建(如果它不存在),使用CREATE TABLE 类似于此处显示的语句:
<span style="background-color:#ffffff"><span style="color:#555555"><span style="background-color:#f8f8f8"><span style="color:#000000"><code class="language-sql"><span style="color:#0077aa">CREATE</span> <span style="color:#0077aa">TABLE</span> gtid_executed <span style="color:#999999">(</span>
source_uuid <span style="color:#834689">CHAR</span><span style="color:#999999">(</span><span style="color:#990055">36</span><span style="color:#999999">)</span> <span style="color:#a67f59">NOT</span> <span style="color:#990055">NULL</span><span style="color:#999999">,</span>
interval_start <span style="color:#834689">BIGINT</span><span style="color:#999999">(</span><span style="color:#990055">20</span><span style="color:#999999">)</span> <span style="color:#a67f59">NOT</span> <span style="color:#990055">NULL</span><span style="color:#999999">,</span>
interval_end <span style="color:#834689">BIGINT</span><span style="color:#999999">(</span><span style="color:#990055">20</span><span style="color:#999999">)</span> <span style="color:#a67f59">NOT</span> <span style="color:#990055">NULL</span><span style="color:#999999">,</span>
<span style="color:#0077aa">PRIMARY</span> <span style="color:#0077aa">KEY</span> <span style="color:#999999">(</span>source_uuid<span style="color:#999999">,</span> interval_start<span style="color:#999999">)</span>
<span style="color:#999999">)</span></code></span></span></span></span>
警告
与其他 MySQL 系统表一样,不要尝试自己创建或修改此表。
该mysql.gtid_executed
表供 MySQL 服务器内部使用。它使副本能够在副本上禁用二进制日志记录时使用 GTID,并在二进制日志丢失时启用 GTID 状态的保留。请注意,mysql.gtid_executed
如果您发出RESET MASTER.
GTID 仅在is 或时 存储在mysql.gtid_executed
表中。如果二进制日志被禁用 ( is ),或者如果 or 被禁用,则服务器在事务提交时将属于每个事务的 GTID 与事务一起存储在 表中。此外,该表以用户可配置的速率定期压缩,如 mysql.gtid_executed 表压缩中所述。 gtid_modeON
ON_PERMISSIVE
log_bin
OFF
log_replica_updateslog_slave_updatesmysql.gtid_executed
如果启用了二进制日志(log_bin
is ON
),从 MySQL 8.0.17 开始, InnoDB
仅存储引擎,服务器更新mysql.gtid_executed
表的方式与禁用二进制日志或副本更新日志时相同,在事务提交时存储每个事务的 GTID . 但是,在 MySQL 8.0.17 之前的版本中,对于其他存储引擎,服务器仅 mysql.gtid_executed
在二进制日志轮换或服务器关闭时才更新表。在这些时候,服务器将写入先前二进制日志的所有事务的 GTID 写入 mysql.gtid_executed
桌子。这种情况适用于 MySQL 8.0.17 之前的源,或 MySQL 8.0.17 之前启用了二进制日志记录的副本,或者使用除 之外的存储引擎InnoDB
,它具有以下后果:
- 在服务器意外停止的情况下,当前二进制日志文件中的 GTID 集不会保存在
mysql.gtid_executed
表中。这些 GTID 在恢复期间从二进制日志文件添加到表中,以便可以继续复制。例外情况是,如果您在服务器重新启动时禁用二进制日志记录(使用 --skip-log-bin 或 --disable-log-bin)。在这种情况下,服务器无法访问二进制日志文件来恢复 GTID,因此无法启动复制。 - 该
mysql.gtid_executed
表不包含所有已执行事务的 GTID 的完整记录。该信息由gtid_executed 系统变量的全局值提供。在 MySQL 8.0.17 之前的版本和除 之外的存储引擎InnoDB
中,始终使用@@GLOBAL.gtid_executed
(在每次提交后更新)来表示 MySQL 服务器的 GTID 状态,而不是查询mysql.gtid_executed
表。
mysql.gtid_executed
即使服务器处于只读或超级只读模式 ,MySQL 服务器也可以写入 表。在 MySQL 8.0.17 之前的版本中,这确保了二进制日志文件仍然可以在这些模式下轮换。如果 mysql.gtid_executed
无法访问表进行写入,并且二进制日志文件由于达到最大文件大小 ( max_binlog_size) 以外的任何原因轮换,则继续使用当前二进制日志文件。向请求轮换的客户端返回一条错误消息,并在服务器上记录一条警告。如果 mysql.gtid_executed
无法访问表以进行写入并且max_binlog_size 已到达,则服务器根据其 binlog_error_action设置进行响应。如果 IGNORE_ERROR
设置,服务器上记录错误并停止二进制日志记录,或者如果 ABORT_SERVER
设置,服务器关闭。
mysql.gtid_executed 表压缩
随着时间的推移,该 mysql.gtid_executed
表可能会充满许多行,这些行引用源自同一服务器的各个 GTID,并且其事务 ID 构成一个范围,类似于此处显示的内容:
<span style="background-color:#ffffff"><span style="color:#555555"><span style="background-color:#f8f8f8"><span style="color:#000000"><code class="language-sql"><span style="color:#555555"><span style="color:#999999">+</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">+</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">+</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">+</span></span>
<span style="color:#555555"><span style="color:#999999">|</span> source_uuid <span style="color:#999999">|</span> interval_start <span style="color:#999999">|</span> interval_end <span style="color:#999999">|</span></span>
<span style="color:#555555"><span style="color:#999999">|</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">+</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">+</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">-</span><span style="color:#999999">|</span></span>
<span style="color:#555555"><span style="color:#999999">|</span> 3E11FA47<span style="color:#999999">-</span>71CA<span style="color:#999999">-</span>11E1<span style="color:#999999">-</span>9E33<span style="color:#999999">-</span>C80AA9429562 <span style="color:#999999">|</span> 37 <span style="color:#999999">|</span> 37 <span style="color:#999999">|</span></span>
<span style="color:#555555"><span style="color:#999999">|</span> 3E11FA47<span style="color:#999999">-</span>71CA<span style="color:#999999">-</span>11E1<span style="color:#999999">-</span>9E33<span style="color:#999999">-</span>C80AA9429562 <span style="color:#999999">|</span> 38 <span style="color:#999999">|</span> 38 <span style="color:#999999">|</span></span>
<span style="color:#555555"><span style="color:#999999">|</span> 3E11FA47<span style="color:#999999">-</span>71CA<span style="color:#999999">-</span>11E1<span style="color:#999999">-</span>9E33<span style="color:#999999">-</span>C80AA9429562 <span style="color:#999999">|</span> 39 <span style="color:#999999">|</span> 39 <span style="color:#999999">|</span></span>
<span style="color:#555555"><span style="color:#999999">|</span> 3E11FA47<span style="color:#999999">-</span>71CA<span style="color:#999999">-</span>11E1<span style="color:#999999">-</span>9E33<span style="color:#999999">-</span>C80AA9429562 <span style="color:#999999">|</span> 40 <span style="color:#999999">|</span> 40 <span style="color:#999999">|</span></span>
<span style="color:#555555"><span style="color:#999999">|</span> 3E11FA47<span style="color:#999999">-</span>71CA<span style="color:#999999">-</span>11E1<span style="color:#999999">-</span>9E33<span style="color:#999999">-</span>C80AA9429562 <span style="color:#999999">|</span> 41 <span style="color:#999999">|</span> 41 <span style="color:#999999">|</span></span>
<span style="color:#555555"><span style="color:#999999">|</span> 3E11FA47<span style="color:#999999">-</span>71CA<span style="color:#999999">-</span>11E1<span style="color:#999999">-</span>9E33<span style="color:#999999">-</span>C80AA9429562 <span style="color:#999999">|</span> 42 <span style="color:#999999">|</span> 42 <span style="color:#999999">|</span></span>
<span style="color:#555555"><span style="color:#999999">|</span> 3E11FA47<span style="color:#999999">-</span>71CA<span style="color:#999999">-</span>11E1<span style="color:#999999">-</span>9E33<span style="color:#999999">-</span>C80AA9429562 <span style="color:#999999">|</span> 43 <span style="color:#999999">|</span> 43 <span style="color:#999999">|</span></span>
<span style="color:#999999">.</span><span style="color:#999999">.</span><span style="color:#999999">.</span></code></span></span></span></span>
mysql.gtid_executed
为了节省空间,MySQL 服务器可以通过用跨越整个事务标识符间隔的单行替换每组这样的行来定期 压缩 表,如下所示:
<span style="background-color:#ffffff"><span style="color:#555555"><span style="background-color:#f8f8f8"><span style="color:#000000"><code class="language-none">+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
|--------------------------------------+----------------+--------------|
| 3E11FA47-71CA-11E1-9E33-C80AA9429562 | 37 | 43 |
...</code></span></span></span></span>
服务器可以使用名为 thread/sql/compress_gtid_table
. 该线程没有在 的输出中列出SHOW PROCESSLIST,但可以将其视为 threads表中的一行,如下所示:
<span style="background-color:#ffffff"><span style="color:#555555"><span style="background-color:#f8f8f8"><span style="color:#000000"><code class="language-sql"><span style="color:#a67f59">mysql></span> <span style="color:#0077aa">SELECT</span> <span style="color:#a67f59">*</span> <span style="color:#0077aa">FROM</span> performance_schema<span style="color:#999999">.</span>threads <span style="color:#0077aa">WHERE</span> <span style="color:#0077aa">NAME</span> <span style="color:#a67f59">LIKE</span> <span style="color:#669900">'%gtid%'</span>\G
<span style="color:#555555"><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span> 1. row <span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span><span style="color:#999999">*</span>
THREAD_ID<span style="color:#999999">:</span> 26
NAME<span style="color:#999999">:</span> thread/sql/compress_gtid_table
TYPE<span style="color:#999999">:</span> FOREGROUND
PROCESSLIST_ID<span style="color:#999999">:</span> 1
PROCESSLIST_USER<span style="color:#999999">:</span> NULL
PROCESSLIST_HOST<span style="color:#999999">:</span> NULL
PROCESSLIST_DB<span style="color:#999999">:</span> NULL
PROCESSLIST_COMMAND<span style="color:#999999">:</span> Daemon
PROCESSLIST_TIME<span style="color:#999999">:</span> 1509
PROCESSLIST_STATE<span style="color:#999999">:</span> Suspending
PROCESSLIST_INFO<span style="color:#999999">:</span> NULL
PARENT_THREAD_ID<span style="color:#999999">:</span> 1
ROLE<span style="color:#999999">:</span> NULL
INSTRUMENTED<span style="color:#999999">:</span> YES
HISTORY<span style="color:#999999">:</span> YES
CONNECTION_TYPE<span style="color:#999999">:</span> NULL
THREAD_OS_ID<span style="color:#999999">:</span> 18677</span></code></span></span></span></span>
在服务器上启用二进制日志记录时,不使用此压缩方法,而是 mysql.gtid_executed
在每次二进制日志轮换时压缩表。但是,当服务器上禁用二进制日志记录时, thread/sql/compress_gtid_table
线程会休眠,直到执行了指定数量的事务,然后唤醒以执行 mysql.gtid_executed
表压缩。然后它会休眠,直到发生相同数量的事务,然后唤醒以再次执行压缩,无限期地重复此循环。在压缩表之前经过的事务数以及压缩率由 gtid_executed_compression_period 系统变量。将该值设置为 0 意味着线程永远不会唤醒,这意味着不使用这种显式压缩方法。相反,压缩会根据需要隐式发生。
从 MySQL 8.0.17 开始,事务由与非事务分开的进程InnoDB 写入 表。此过程由不同的线程控制, . 这个 GTID 持久化线程以组的形式收集 GTID,将它们刷新到 表中,然后压缩表。如果服务器混合了 事务和非事务,它们分别写入表,线程执行的压缩 会干扰 GTID 持久线程的工作,并可能显着减慢它。因此,从该版本开始,建议您设置 mysql.gtid_executed
InnoDB
innodb/clone_gtid_thread
mysql.gtid_executed
InnoDBInnoDBmysql.gtid_executed
compress_gtid_table
gtid_executed_compression_period 为 0,因此compress_gtid_table
线程永远不会被激活。
从 MySQL 8.0.23 开始, gtid_executed_compression_period 默认值为 0, GTID 持久化线程将事务InnoDB
和非InnoDB
事务写入 mysql.gtid_executed
表。
对于 MySQL 8.0.17 之前的版本, gtid_executed_compression_period 可以使用默认值 1000,这意味着在每 1000 个事务之后执行表压缩,或者您可以选择其他值。在这些版本中,如果您将值设置为 0 并且禁用二进制日志记录,则不会对mysql.gtid_executed
表执行显式压缩,并且您应该准备好应对表可能需要的磁盘空间量的潜在大幅增加,如果你做这个。
当启动服务器实例时,如果 gtid_executed_compression_period 设置为非零值并启动 thread/sql/compress_gtid_table
线程,在大多数服务器配置中,都会对mysql.gtid_executed
表执行显式压缩。在 MySQL 8.0.17 之前的版本中,启用二进制日志记录时,压缩是由二进制日志在启动时轮换的事实触发的。在 MySQL 8.0.20 的版本中,压缩由线程启动触发。在中间版本中,压缩不会在启动时发生。