MySQL启用GTID的限制

开启 GTID 之后,会由部分的限制,内容如下:

更新非事务引擎表GTID

同步复制是基于事务的,所以 MyISAM 存储引擎不支持,这可能导致多个 GTID 分配给同一个事务。 使用GTID时,使用非事务性存储引擎(如MyISAM)对表的更新不能在与使用事务性存储引擎(如InnoDB)的表的更新相同的语句或事务中进行。

mysql> CREATE TABLE error (ID INT) ENGINE=MyISAM;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO error VALUES(1),(2);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> CREATE TABLE hello (ID INT) ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO hello VALUES(1),(2);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> SET AUTOCOMMIT = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)

mysql> UPDATE hello SET id = 3 WHERE id =2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> UPDATE error SET id = 3 WHERE id =2;
ERROR 1785 (HY000): When @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1, updates to non-transactional tables can only be done in either autocommitted statements or single-statement transactions,and never in the same statement as updates to transactional tables.

CREATE TABLE … SELECT语句不支持

因为该语句会被拆分成 CREATE TABLE 和 INSERT 两个事务,并且这个两个事务被分配了同一个 GTID,这会导致 INSERT 被备库忽略掉。

mysql> CREATE TABLE hello ENGINE=InnoDB AS SELECT * FROM hello;
ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.

Temporary tables事务内部不能执行

创建临时表语句,但可以在事务外执行,但必须设置 set autocommit=1 。另外 procedures, functions, and triggers在使用GTID时不能 CREATE TEMPORARY TABLE and DROP TEMPORARY TABLE 语句

mysql> CREATE TEMPORARY TABLE test(id INT);
ERROR 1787 (HY000): Statement violates GTID consistency: CREATE TEMPORARY TABLE and DROP TEMPORARY TABLE can only be executed outside transactional context. These statements are also not allowed in a function or trigger because functions and triggers are also considered to be multi-statement transactions.

mysql> SET AUTOCOMMIT = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TEMPORARY TABLE test(id INT);
Query OK, 0 rows affected (0.04 sec)

与临时表相关的包括了 CREATE/DROP TEMPORARY TABLE 临时表操作。

总结

实际上,一般启动 GTID 时,可以启用 enforce-gtid-consistency 选项,从而在执行上述不支持的语句时,将会返回错误。

上一篇:[CareerCup] 8.9 An In-memory File System 内存文件系统


下一篇:ElasticSearch启动时出错:ElasticSearch用于拥有的内存权限太小,至少需要262144