MySQL主從復(fù)制之GTID模式詳細介紹?
一、GTID概述
MySQL5.6 在原有主從復(fù)制的基礎(chǔ)上增加了一個新的復(fù)制方式,即基于GTID的復(fù)制方式,它由UUID和事務(wù)ID兩個部分組成,具有如下特點。
- GTID事務(wù)是全局唯一性的,并且一個事務(wù)對應(yīng)一個GTID值。
- 一個GTID值在同一個MySQL實例上只會執(zhí)行一次。
二、GTID相較與傳統(tǒng)復(fù)制的優(yōu)勢
- 主從搭建更加簡便,不用手動特地指定
position位置。 - 復(fù)制集群內(nèi)有一個統(tǒng)一的標識,識別、管理上更方便。
- 故障轉(zhuǎn)移更容易,不用像傳統(tǒng)復(fù)制那樣需要找
log_file和log_Pos的位置。 - 通常情況下GTID是連續(xù)沒有空洞的,更能保證數(shù)據(jù)的一致性,零丟失。
- 相對于ROW復(fù)制模式,數(shù)據(jù)安全性更高,切換更簡單。
- 比傳統(tǒng)的復(fù)制更加安全,一個GTID在一個MySQL實例上只會執(zhí)行一次,避免重復(fù)執(zhí)行導(dǎo)致數(shù)據(jù)混亂或者主從不一致。
三、GTID自身存在哪些限制
- 在一個復(fù)制組中,必須都要開啟GTID。
- MySQL5.6開啟GTID需要重啟。
- 不支持
sql_slave_skip_counte操作,傳統(tǒng)復(fù)制可以使用這個命令跳過事務(wù)。 - 不允許在一個SQL同時更新一個事務(wù)引擎和非事務(wù)引擎的表,如
InnoDB和MyISAM。 - 對于
create temporary table 和drop temporary table語句不支持。 - 不支持create table … select 語句復(fù)制。
四、GTID工作原理簡單介紹
master節(jié)點在更新數(shù)據(jù)的時候,會在事務(wù)前產(chǎn)生GTID信息,一同記錄到binlog日志中。- slave節(jié)點的io線程將
binlog寫入到本地relay log中。 - 然后SQL線程從
relay log中讀取GTID,設(shè)置gtid_next的值為該gtid,然后對比slave端的binlog是否有記錄。 - 如果有記錄的話,說明該GTID的事務(wù)已經(jīng)運行,slave會忽略。
- 如果沒有記錄的話,slave就會執(zhí)行該GTID對應(yīng)的事務(wù),并記錄到binlog中。
五、如何開啟GTID復(fù)制
除傳統(tǒng)復(fù)制需要開啟的binlog相關(guān)參數(shù)之外,GTID同步需額外開啟如下參數(shù)設(shè)置,注意主從節(jié)點需要同步開啟。
gtid_mode=on ? ?# 開啟GTID enforce-gtid-consistency=on ?# 需要同步設(shè)置該參數(shù) log-slave-updates=1 ? ? ?# 5.6 版本需要開啟該參數(shù)
六、查看GTID相關(guān)參數(shù)
[root@GreatSQL][(none)]>show variables like '%gtid%'; +----------------------------------+-------------------------------------------------------------------------------------+ | Variable_name ? ? ? ? ? ? ? ? ? ?| Value ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | +----------------------------------+-------------------------------------------------------------------------------------+ | binlog_gtid_simple_recovery ? ? ?| ON ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| | enforce_gtid_consistency ? ? ? ? | ON ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| | gtid_executed ? ? ? ? ? ? ? ? ? ?| 613743f5-8b1c-11ec-9922-00155dcff911:1-14 | | gtid_executed_compression_period | 0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | | gtid_mode ? ? ? ? ? ? ? ? ? ? ? ?| ON ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| | gtid_next ? ? ? ? ? ? ? ? ? ? ? ?| AUTOMATIC ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | | gtid_owned ? ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | | gtid_purged ? ? ? ? ? ? ? ? ? ? ?| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | | session_track_gtids ? ? ? ? ? ? ?| OFF ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | +----------------------------------+-------------------------------------------------------------------------------------+ 9 rows in set (0.00 sec)
參數(shù)簡要說明:

七、GTID與傳統(tǒng)模式建立復(fù)制時候語句的不同點
# 傳統(tǒng)復(fù)制 change master to master_host="127.0.0.1",master_port=3310,MASTER_USER='sync',MASTER_PASSWORD='GreatSQL',MASTER_LOG_FILE='log-bin.000005', MASTER_LOG_POS=4111; # GTID復(fù)制 change master to master_host="127.0.0.1",master_port=3310,MASTER_USER='sync',MASTER_PASSWORD='GreatSQL',MASTER_AUTO_POSITION=1
GTID同步在建立復(fù)制的時候,將傳統(tǒng)復(fù)制由人為指定binlog的pos位點改為了MASTER_AUTO_POSITION=1自動獲取binlog的pos位點。
八、GTID同步狀態(tài)簡單解析
除了傳統(tǒng)的查看binlog和pos值之外,GTID模式可以更直觀的查看某個事務(wù)執(zhí)行的情況。
[root@GreatSQL][(none)]>show slave status\G; *************************** 1. row *************************** ? ? ? ? ? ? ? ?Slave_IO_State: Waiting for master to send event ? ? ? ? ? ? ? ? ? Master_Host: 192.168.6.215 ? ? ? ? ? ? ? ? ? Master_User: sync ? ? ? ? ? ? ? ? ? Master_Port: 3306 ? ? ? ? ? ? ? ? Connect_Retry: 60 ? ? ? ? ? ? ? Master_Log_File: binlog.000001 ? ? ? ? ? Read_Master_Log_Pos: 2425 ? ? ? ? ? ? ? ?Relay_Log_File: mgr2-relay-bin.000002 ? ? ? ? ? ? ? ? Relay_Log_Pos: 2634 ? ? ? ? Relay_Master_Log_File: binlog.000001 ? ? ? ? ? ? ?Slave_IO_Running: Yes ? ? ? ? ? ? Slave_SQL_Running: Yes ? ? ? ? ? ? ? Replicate_Do_DB: ? ? ? ? ? Replicate_Ignore_DB: ? ? ? ? ? ?Replicate_Do_Table: ? ? ? ?Replicate_Ignore_Table: ? ? ? Replicate_Wild_Do_Table: ? Replicate_Wild_Ignore_Table: ? ? ? ? ? ? ? ? ? ?Last_Errno: 0 ? ? ? ? ? ? ? ? ? ?Last_Error: ? ? ? ? ? ? ? ? ?Skip_Counter: 0 ? ? ? ? ? Exec_Master_Log_Pos: 2425 ? ? ? ? ? ? ? Relay_Log_Space: 2842 ? ? ? ? ? ? ? Until_Condition: None ? ? ? ? ? ? ? ?Until_Log_File: ? ? ? ? ? ? ? ? Until_Log_Pos: 0 ? ? ? ? ? ?Master_SSL_Allowed: No ? ? ? ? ? ?Master_SSL_CA_File: ? ? ? ? ? ?Master_SSL_CA_Path: ? ? ? ? ? ? ? Master_SSL_Cert: ? ? ? ? ? ? Master_SSL_Cipher: ? ? ? ? ? ? ? ?Master_SSL_Key: ? ? ? ? Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No ? ? ? ? ? ? ? ? Last_IO_Errno: 0 ? ? ? ? ? ? ? ? Last_IO_Error: ? ? ? ? ? ? ? ?Last_SQL_Errno: 0 ? ? ? ? ? ? ? ?Last_SQL_Error: ? Replicate_Ignore_Server_Ids: ? ? ? ? ? ? ?Master_Server_Id: 2153306 ? ? ? ? ? ? ? ? ? Master_UUID: 613743f5-8b1c-11ec-9922-00155dcff911 ? ? ? ? ? ? ?Master_Info_File: mysql.slave_master_info ? ? ? ? ? ? ? ? ? ? SQL_Delay: 0 ? ? ? ? ? SQL_Remaining_Delay: NULL ? ? ? Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates ? ? ? ? ? ?Master_Retry_Count: 86400 ? ? ? ? ? ? ? ? ? Master_Bind: ? ? ? Last_IO_Error_Timestamp: ? ? ?Last_SQL_Error_Timestamp: ? ? ? ? ? ? ? ?Master_SSL_Crl: ? ? ? ? ? ?Master_SSL_Crlpath: ? ? ? ? ? ?Retrieved_Gtid_Set: 613743f5-8b1c-11ec-9922-00155dcff911:1-10 ? ? ? ? ? ? Executed_Gtid_Set: 613743f5-8b1c-11ec-9922-00155dcff911:1-10, 652ade08-8b1c-11ec-9f62-00155dcff90a:1-2 ? ? ? ? ? ? ? ? Auto_Position: 1 ? ? ? ? ?Replicate_Rewrite_DB: ? ? ? ? ? ? ? ? ?Channel_Name: ? ? ? ? ? ?Master_TLS_Version: ? ? ? ?Master_public_key_path: ? ? ? ? Get_master_public_key: 0 ? ? ? ? ? ? Network_Namespace: 1 row in set, 1 warning (0.01 sec) ERROR: No query specified
GTID相關(guān)鍵參數(shù)說明:

到此這篇關(guān)于MySQL主從復(fù)制之GTID模式詳細介紹 的文章就介紹到這了,更多相關(guān)MySQL主從復(fù)制之GTID模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL中的RIGHT?JOIN和CROSS?JOIN操作示例
本文詳細介紹了MySQL中的RIGHT?JOIN和CROSS?JOIN操作,RIGHT?JOIN返回右表中的所有記錄及與左表中的記錄相匹配的記錄,而CROSS?JOIN返回兩個表中所有可能的組合,通過實際示例和輸出結(jié)果,我們展示了如何使用RIGHT?JOIN和CROSS?JOIN進行數(shù)據(jù)庫查詢,一起看看吧2023-07-07
mysql數(shù)據(jù)庫亂碼之保存越南文亂碼解決方法
做一個包含越南文的網(wǎng)站,用戶說在保存包含越南文的文章時,MSYQL亂碼了,看下面的解決方法2013-12-12
MySQL高并發(fā)生成唯一訂單號的方法實現(xiàn)
這篇文章主要介紹了MySQL高并發(fā)生成唯一訂單號的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02

