共計 6116 個字符,預計需要花費 16 分鐘才能閱讀完成。
這篇文章主要介紹“怎么理解 MySQL 的 GTID 復制”,在日常操作中,相信很多人在怎么理解 MySQL 的 GTID 復制問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么理解 MySQL 的 GTID 復制”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
什么是 GTID
什么是 GTID 呢,簡而言之,就是全局事務 ID(global transaction identifier),最初由 google 實現,官方 MySQL 在 5.6 才加入該功能。
GTID 是事務提交時創建分配的唯一標識符,所有事務均與 GTID 一一映射。
GTID 的格式類似于:
5882bfb0-c936-11e4-a843-000c292dc103:1
這個字符串,用“:”分開,前面表示這個服務器的 server_uuid,這是一個 128 位的隨機字符串,在第一次啟動時生成(函數 generate_server_uuid),對應的 variables 是只讀變量 server_uuid。它能以極高的概率保證全局唯一性,并存到文件
DATADIR/auto.cnf 中。因此要注意保護這個文件不要被刪除或修改。
第二部分是一個自增的事務 ID 號,事務 id 號 +server_uuid 來唯一標示一個事務。
mysql show global variables like %gtid%
+--------------------------+------------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------------+
| enforce_gtid_consistency | ON |
| gtid_executed | 5882bfb0-c936-11e4-a843-000c292dc103:1-6 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
+--------------------------+------------------------------------------+
5 rows in set (0.00 sec)
mysql show global variables like %uuid%
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | 5882bfb0-c936-11e4-a843-000c292dc103 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)
shell cat auto.cnf
[auto]
server-uuid=5882bfb0-c936-11e4-a843-000c292dc103
設置 GTID 復制
同步主從數據
mysql SET @@global.read_only = ON;
Query OK, 0 rows affected (0.01 sec)
停止所有數據庫
shell mysqladmin -u root -p shutdown
設置開發 GTID 模式并啟動所有數據庫
shell vi my.cnf 添加如下內容
================================================================
[mysqld]
gtid_mode=ON
log-slave-updates=ON
enforce-gtid-consistency=ON # 強制 GTID 的一致性
================================================================
從庫指定主庫
mysql CHANGE MASTER TO
- MASTER_HOST = host,
- MASTER_PORT = port,
- MASTER_USER = user,
- MASTER_PASSWORD = password,
- MASTER_AUTO_POSITION = 1;
mysql START SLAVE;
Query OK, 0 rows affected (0.04 sec)
禁止 read-only 模式
mysql SET @@global.read_only = OFF;
Query OK, 0 rows affected (0.00 sec)
GTID 復制的限制
GTID 模式實例和非 GTID 模式實例是不能進行復制的,要求非常嚴格,要么都是 GTID,要么都不是
gtid_mode 是只讀的,要改變狀態必須 1) 關閉實例、2)修改配置文件、3) 重啟實例
更新非事務引擎表
在同一事務中更新事務表與非事務表將導致多個 GTIDs 分配給同一事務
mysql cretea table tt (id int) engine=myisam;
mysql insert into tt values(1),(2);
mysql cretea table t (id int) engine=innodb;
mysql insert into t values(1),(2);
mysql set autocommit = 0;mysql begin;mysql update t set id = 3 where id =2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql update tt 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 statements
不安全的基于語句復制,實際是兩個獨立的事件,一個用于建表,一個用于向新表插入源表數據。
mysql create table t engine=innodb as select * from tt;
ERROR 1786 (HY000): CREATE TABLE ... SELECT is forbidden when @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1.
臨時表
事務內部不能執行創建刪除臨時表語句,但可以在事務外執行,但必須設置 set autocommit = 1
mysql create temporary table tttt(id int);
ERROR 1787 (HY000): When @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1, the statements CREATE TEMPORARY TABLE and
DROP TEMPORARY TABLE can be executed in a non-transactional context only, and require that AUTOCOMMIT = 1.
mysql set autocommit = 1;
Query OK, 0 rows affected (0.00 sec)
mysql create temporary table tttt(id int);
Query OK, 0 rows affected (0.04 sec)
不執行不支持的語句
啟用 –enforce-gtid-consistency 選項啟動 GTID 模式,上述不支持的語句將會返回錯誤。
運維操作
a. 忽略復制錯誤
當備庫復制出錯時,傳統的跳過錯誤的方法是設置 sql_slave_skip_counter, 然后再 START SLAVE。
但如果打開了 GTID,就會設置失敗:
mysql stop slave;
Query OK, 0 rows affected (0.03 sec)
mysql set global sql_slave_skip_counter = 1;
ERROR 1858 (HY000): sql_slave_skip_counter can not be set when the server is running with @@GLOBAL.GTID_MODE = ON.
Instead, for each transaction that you want to skip, generate an empty transaction with the same GTID as the transaction
提示的錯誤信息告訴我們,可以通過生成一個空事務來跳過錯誤的事務。
我們手動產生一個備庫復制錯誤:
[slave]
mysql alter table t add primary key pk_id(id);
Query OK, 2 rows affected (0.12 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql insert into t values(1);
mysql insert into t values(4);
mysql insert into t values(5);
mysql show master status ;
+-------------------+----------+--------------+------------------+-------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-info.000004 | 914 | | | 5882bfb0-c936-11e4-a843-000c292dc103:1-17 |
+-------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)
mysql show slave status \G
*************************** 1. row ***************************
...
Slave_IO_Running: Yes
lave_SQL_Running: No
Last_Errno: 1062
Last_Error: Could not execute Write_rows event on table db_test.t; Duplicate entry 1 for key PRIMARY ,
Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event s master log mysql-info.000004, end_log_pos 401
Retrieved_Gtid_Set: 5882bfb0-c936-11e4-a843-000c292dc103:1-15
Executed_Gtid_Set: 5882bfb0-c936-11e4-a843-000c292dc103:1-14,
f1e6584a-c935-11e4-a840-000c29348dbe:1
Auto_Position: 1
1 row in set (0.00 sec)
mysql SET @@SESSION.GTID_NEXT= 5882bfb0-c936-11e4-a843-000c292dc103:15
Query OK, 0 rows affected (0.00 sec)
mysql begin;
Query OK, 0 rows affected (0.00 sec)
mysql commit;
Query OK, 0 rows affected (0.00 sec)
mysql SET SESSION GTID_NEXT = AUTOMATIC;
mysql start slave;
mysql show slave status\G
*************************** 1. row ***************************
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Last_Errno: 0
Retrieved_Gtid_Set: 5882bfb0-c936-11e4-a843-000c292dc103:1-17
Executed_Gtid_Set: 5882bfb0-c936-11e4-a843-000c292dc103:1-17,
f1e6584a-c935-11e4-a840-000c29348dbe:1
Auto_Position: 1
1 row in set (0.00 sec)
再查看 show slave status,就會發現錯誤事務已經被跳過了。這種方法的原理很簡單,空事務產生的 GTID 加入到 GTID_EXECUTED 中,
這相當于告訴備庫,這個 GTID 對應的事務已經執行了,此時主從數據不一致。
到此,關于“怎么理解 MySQL 的 GTID 復制”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!