2021-07-22 gtid 詳解

gtid 詳解

Retrieved_Gtid_Set Executed_Gtid_Set

Retrieved_Gtid_Set : 從庫已經(jīng)接收到主庫的事務(wù)編號

Executed_Gtid_Set : 從庫自身已經(jīng)執(zhí)行的事務(wù)編號

執(zhí)行三個(gè)事務(wù)之后

[root@localhost][db1]> create table t2 ( id int);
Query OK, 0 rows affected (0.07 sec)

[root@localhost][db1]> insert into t2 select 1;
Query OK, 1 row affected (0.07 sec)
Records: 1  Duplicates: 0  Warnings: 0

[root@localhost][db1]> insert into t2 select 2;
Query OK, 1 row affected (0.02 sec)
其中主庫的 Executed_Gtid_Set為:2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-3

看到從庫的Retrieved_Gtid_Set為:   2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-3

         Executed_Gtid_Set為: 2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-3

也就是說主庫產(chǎn)生了3個(gè)事務(wù),從庫接收到了主庫的3個(gè)事務(wù),且都已全部執(zhí)行。

多個(gè) set

Executed_Gtid_Set: 2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-33,
8ce853fc-6f8a-11e7-8940-000c29e3f5ab:1

原因:

這里的2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-33好理解,就是已經(jīng)執(zhí)行主庫的1-33的事務(wù),那么8ce853fc-6f8a-11e7-8940-000c29e3f5ab:1呢?其實(shí)也簡單,有兩種情況

一種:NO.1 從庫有數(shù)據(jù)寫入(即從庫插入數(shù)據(jù))


[root@localhost][db1]> insert into t2 select 1;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0
show slave status\G;

Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
                  Master_UUID: 2a09ee6e-645d-11e7-a96c-000c2953a1cb
             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: 2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-3
            Executed_Gtid_Set: 2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-3,
8ce853fc-6f8a-11e7-8940-000c29e3f5ab:1
                Auto_Position: 1
         Replicate_Rewrite_DB:
可以看到已經(jīng)執(zhí)行的事務(wù)有來自主庫的2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-3,也有從庫自己寫入的數(shù)據(jù):8ce853fc-6f8a-11e7-8940-000c29e3f5ab:1。

另一種:NO.2 主從切換(這里使用的是MHA切換主從)


             Master_Server_Id: 20
                  Master_UUID: 8ce853fc-6f8a-11e7-8940-000c29e3f5ab
             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: 8ce853fc-6f8a-11e7-8940-000c29e3f5ab:1
            Executed_Gtid_Set: 2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-3,
8ce853fc-6f8a-11e7-8940-000c29e3f5ab:1
                Auto_Position: 1
可以看到主從切換以后主庫的server-id是20。這里的意思是接收到主庫8ce853fc-6f8a-11e7-8940-000c29e3f5ab:1,并已經(jīng)執(zhí)行了這個(gè)事物,這個(gè)事物其實(shí)就是之前從庫寫入的那條數(shù)據(jù)。對于2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-3就是之前主庫執(zhí)行的3個(gè)事務(wù),如果此時(shí)在主庫再插入一條數(shù)據(jù),那么變化如下:

 Retrieved_Gtid_Set: 8ce853fc-6f8a-11e7-8940-000c29e3f5ab:1-2
  Executed_Gtid_Set: 2a09ee6e-645d-11e7-a96c-000c2953a1cb:1-3,
8ce853fc-6f8a-11e7-8940-000c29e3f5ab:1-2

下面說說GTID不連續(xù)問題

這個(gè)是由于binlog被清理后導(dǎo)致的:

binlog不可能永遠(yuǎn)駐留在服務(wù)上,需要定期進(jìn)行清理(通過expire_logs_days可以控制定期清理間隔),否則遲早它會(huì)把磁盤用盡。gtid_purged用于記錄已經(jīng)被清除了的binlog事務(wù)集合,它是gtid_executed的子集。只有g(shù)tid_executed為空時(shí)才能手動(dòng)設(shè)置該變量,此時(shí)會(huì)同時(shí)更新gtid_executed為和gtid_purged相同的值。gtid_executed為空意味著要么之前沒有啟動(dòng)過基于GTID的復(fù)制,要么執(zhí)行過RESET MASTER。執(zhí)行RESET MASTER時(shí)同樣也會(huì)把gtid_purged置空,即始終保持gtid_purged是gtid_executed的子集。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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