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

1)什么是GTID

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

2)GTID的作用

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

GTID的存在方便了Replication的Failover

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

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

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

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

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

4)GTID的工作原理:

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

slave端的i/o線程將變更的binlog,寫入到本地的relay log中。

sql線程從relay log中獲取GTID,然后對比slave端的binlog是否有記錄。

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

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

在解析過程中會判斷是否有主鍵,如果沒有就用二級索引,如果沒有就用全部掃描。

5)GTID常用參數(shù)注釋:

GTID的參數(shù)注釋:

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

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

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

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

master時,此值會被清空。

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

gtid_mode:是否開啟GTID功能。

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

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

gtid_next:這個時session級別的參數(shù):

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

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

主服務(wù)器配置:

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

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

啟動數(shù)據(jù)庫服務(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配置文件,主要配置以下幾項:

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

啟動數(shù)據(jù)庫服務(wù)

連接master:

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

啟動復(fù)制線程:

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ù)制錯誤

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

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

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

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