深入了解復(fù)制-全局事務(wù)標(biāo)識(shí)符(GTID)

1)什么是GTID

GTID(Global Transaction ID)是對(duì)于一個(gè)已提交事務(wù)的編號(hào),并且是一個(gè)全局唯一的編號(hào)。GTID實(shí)際上是由UUID+TID組成的。其中UUID是一個(gè)MySQL實(shí)例的唯一標(biāo)識(shí),保存在mysql數(shù)據(jù)目錄下的auto.cnf文件里。TID代表了該實(shí)例上已經(jīng)提交的事務(wù)數(shù)量,并且隨著事務(wù)提交單調(diào)遞增。下面是一個(gè)GTID的具體形式:3E11FA47-71CA-11E1-9E33-C80AA9429562:23。

2)GTID的作用

根據(jù)GTID可以知道事務(wù)最初是在哪個(gè)實(shí)例上提交的

GTID的存在方便了Replication的Failover

3)GTID比傳統(tǒng)復(fù)制的優(yōu)勢(shì)

更簡(jiǎn)單的實(shí)現(xiàn)failover,不用以前那樣在需要找log_file和log_Pos。

更簡(jiǎn)單的搭建主從復(fù)制。

比傳統(tǒng)復(fù)制更加安全。

GTID是連續(xù)沒(méi)有空洞的,因此主從庫(kù)出現(xiàn)數(shù)據(jù)沖突時(shí),可以用添加空事物的方式進(jìn)行跳過(guò)。

4)GTID的工作原理:

master更新數(shù)據(jù)時(shí),會(huì)在事務(wù)前產(chǎn)生GTID,一同記錄到binlog日志中。

slave端的i/o線(xiàn)程將變更的binlog,寫(xiě)入到本地的relay log中。

sql線(xiàn)程從relay log中獲取GTID,然后對(duì)比slave端的binlog是否有記錄。

如果有記錄,說(shuō)明該GTID的事務(wù)已經(jīng)執(zhí)行,slave會(huì)忽略。

如果沒(méi)有記錄,slave就會(huì)從relay log中執(zhí)行該GTID的事務(wù),并記錄到binlog。

在解析過(guò)程中會(huì)判斷是否有主鍵,如果沒(méi)有就用二級(jí)索引,如果沒(méi)有就用全部掃描。

5)GTID常用參數(shù)注釋?zhuān)?/h1>

GTID的參數(shù)注釋?zhuān)?/p>

[master]>showglobal variables like '%gtid%';

enforce_gtid_consistency:開(kāi)啟gtid的一些安全限制(介意開(kāi)啟)。

gtid_executed:全局和seeeion級(jí)別都可以用。用來(lái)保存已經(jīng)執(zhí)行過(guò)的GTIDs。

注:showmaster status\G;輸出結(jié)果中的Executed_Gtid_Set和gitd_executed一致。reset

master時(shí),此值會(huì)被清空。

gtid_owned:全局和session級(jí)別都可用,全局表示所有服務(wù)器擁有GTIDs,session級(jí)別表示當(dāng)前client擁有所有GTIDs。(此功能用的少)

gtid_mode:是否開(kāi)啟GTID功能。

gtid_purged:全局參數(shù),設(shè)置在binlog中,已經(jīng)purged的GTIDs,并且purged掉的GTIDs會(huì)包含到gtid_executed中。

注:從而導(dǎo)致slave不會(huì)再去master請(qǐng)求這些GTIDs,并且Executed_Gtid_Set為空時(shí),才可以設(shè)置此值。

gtid_next:這個(gè)時(shí)session級(jí)別的參數(shù):

6)使用GTID配置主從復(fù)制

實(shí)際工作主要會(huì)在兩種情況下配置:一是新搭建的服務(wù)器,直接配置啟動(dòng)就可以,二是已經(jīng)在運(yùn)行的服務(wù)器,這時(shí)候需要閉關(guān)master的寫(xiě),保證所有slave端都已經(jīng)和master端數(shù)據(jù)保持同步。然后即可按如下方法配置

主服務(wù)器配置:

停止mysql服務(wù),修改/etc/my.cnf配置文件,主要配置以下幾項(xiàng):

log-bin =mysql-bin

log_bin_index =mysql-bin.index

expire_logs_days = 30

binlog_format = ROW

log-slave-updates = true

sync-binlog = 1:

gtid-mode = on

enforce-gtid-consistency = true

啟動(dòng)數(shù)據(jù)庫(kù)服務(wù),查看相關(guān)信息

使用如下命令查看GTID的相關(guān)參數(shù):showglobal variables like '%gtid%';


下圖顯示GTID是否正常使用:


再次使用show global variables like '%gtid%';查看參數(shù)設(shè)置,出現(xiàn)如下結(jié)果。


配置從服務(wù)器:

停止mysql服務(wù),修改/etc/my.cnf配置文件,主要配置以下幾項(xiàng):

gtid-mode = on

enforce-gtid-consistency = true

server-id = 1

log-bin =mysql-bin

log_bin_index =mysql-bin.index

expire_logs_days = 30

binlog_format = ROW

sync-binlog = 1:

relay-log = relay-log

relay-log-index = relay-log.index

log-slave-updates = true

master-info-repository = table

relay-log-info-repository = table

slave-parallel-workers = 1

relay_log_purge = 1

relay_log_recovery = 1

report-port = 3306

report-host = 192.168.10.72

skip-slave-start

啟動(dòng)數(shù)據(jù)庫(kù)服務(wù)

連接master:

CHANGE MASTER TOMASTER_HOST='192.168.10.71',MASTER_PORT=3306,MASTER_USER='repl_user',MASTER_PASSWORD='123456', MASTER_AUTO_POSITION=1;

啟動(dòng)復(fù)制線(xiàn)程:

start slave;

相看相關(guān)狀態(tài):

slave上顯示:

mysql> show slave status\G;

*************************** 1. row***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.10.71

Master_User: repl_user

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: master-bin.000008

Read_Master_Log_Pos: 191

Relay_Log_File: relay-log.000005

Relay_Log_Pos: 363

Relay_Master_Log_File: master-bin.000008

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: 191

Relay_Log_Space: 530

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: 171

Master_UUID:0e9896a7-14f7-11e7-a0e6-000c2900551e

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 the slaveI/O thread to update it

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set: 0e9896a7-14f7-11e7-a0e6-000c2900551e:1

Auto_Position: 1

1 row in set (0.00 sec)

在master顯示:


7)、修復(fù)GTID復(fù)制錯(cuò)誤

在基于GTID的復(fù)制拓?fù)渲?,要想修?fù)Slave的SQL線(xiàn)程錯(cuò)誤,過(guò)去的SQL_SLAVE_SKIP_COUNTER方式不再適用。需要通過(guò)設(shè)置gtid_next或gtid_purged完成,當(dāng)然前提是已經(jīng)確保主從數(shù)據(jù)一致,僅僅需要跳過(guò)復(fù)制錯(cuò)誤讓復(fù)制繼續(xù)下去。其中g(shù)tid_next就是跳過(guò)某個(gè)執(zhí)行事務(wù),設(shè)置gtid_next的方法一次只能跳過(guò)一個(gè)事務(wù),要批量的跳過(guò)事務(wù)可以通過(guò)設(shè)置gtid_purged完成。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容