當使用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的過程當中要建立或修改系統表(非事務引擎)事務