事務中混合多個存儲引擎,會產生多個GTID。
- 當使用GTID,在同一個事務中,更新包括了非事務引擎(MyISAM)和事務引擎(InnoDB)表的操作,就會導致多個GTID分配給同一個事務。
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.
- 主從表的存儲引擎不一致,會導致數據不一致。
- 基於GTID複製,不支持CREATE TABLE…SELECT 語句
該語句實際上被記錄爲兩個單獨的事件,一個是創建表,另一個插入數據。當事務執行該語句時,在一些情況下,這兩個事件可能接收到相同的事務ID,導致插入的事件被從庫跳過。
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
- 不支持事務裏包含 CREAT TEMPORARY TABLE和DROP TEMPORARY TABLE
在 autocommit=1的情況下,可以創建臨時表,MASTER創建臨時表不產生GTID信息,所以不會同步到SLAVE。
#事務裏
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來緩存事務的binlog
#存放非事務表和臨時表binlog
binlog_cache_data stmt_cache;
#存放事務表binlog
binlog_cache_data trx_cache;
GTID中,會檢查這兩個cache,如有衝突,則拋出錯誤
- 不推薦在GTID模式的實例上進行 mysql_upgrade
歸根結底,還是因爲mysql_upgrade的過程中要創建或修改系統表(非事務引擎)