当使用GTID,在同一个事务中,更新包括了非事务引擎(MyISAM)和事务引擎(InnoDB)表的操做,就会致使多个GTID分配给同一个事务。mysql
mysql> CREATE TABLE `t_test_myisam` ( -> `id` int(11) NOT NULL, -> `name` varchar(10) DEFAULT NULL, -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.01 sec) mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> update t_test set name ='aa1' where id =1; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> insert into t_test_myisam values(1,'aa2'); ERROR 1785 (HY000): Statement violates GTID consistency: 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.
该语句实际上被记录为两个单独的事件,一个是建立表,另外一个插入数据。当事务执行该语句时,在一些状况下,这两个事件可能接收到相同的事务ID,致使插入的事件被从库跳过。sql
mysql> create table t_test_new as select * from t_test; ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.
解决方案:缓存
mysql> create table t_test_new like t_test; Query OK, 0 rows affected (0.06 sec) mysql> insert into t_test_new select * from t_test; Query OK, 5 rows affected (0.03 sec) Records: 5 Duplicates: 0 Warnings: 0
在 autocommit=1的状况下,能够建立临时表,MASTER建立临时表不产生GTID信息,因此不会同步到SLAVE。ide
#事务里 mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> CREATE TEMPORARY TABLE TMP_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> CREATE TEMPORARY TABLE TMP_TEST(ID int); Query OK, 0 rows affected (0.00 sec)
MySQL5.7中cache里面的机制,大致来讲,binlog有两个cache来缓存事务的binlogcode
#存放非事务表和临时表binlog binlog_cache_data stmt_cache; #存放事务表binlog binlog_cache_data trx_cache;
GTID中,会检查这两个cache,若有冲突,则抛出错误事件
归根结底,仍是由于mysql_upgrade的过程当中要建立或修改系统表(非事务引擎)事务