什麼是GTID? GTID (Global Transaction ID) 是對於一個已提交事務的編號,並且是一個全局唯一的編號。 GTID 實際上 是由UUID+TID 組成的。其中 UUID 是一個 MySQL 實例的唯一標識。TID代表了該實例上已經提交的事務數量,並且隨著事務提交單調遞增。 ...
什麼是GTID?
GTID (Global Transaction ID) 是對於一個已提交事務的編號,並且是一個全局唯一的編號。 GTID 實際上 是由UUID+TID 組成的。
其中 UUID 是一個 MySQL 實例的唯一標識。TID代表了該實例上已經提交的事務數量,並且隨著事務提交單調遞增。
下麵是一個GTID的具體形式:03a1eb63-c21a-11ec-b07f-000c2987bea6:1-25,冒號分割前邊為UUID,後邊為TID。
GTID 集合可以包含來自多個 MySQL 實例的事務,它們之間用逗號分隔。
GTID的工作原理
①當一個事務在主庫端執行並提交時,產生GTID,一同記錄到binlog日誌中。
②binlog傳輸到slave,並存儲到slave的relaylog後,讀取這個GTID的這個值設置GTID_next變數,即告訴Slave,下一個要執行的GTID值。
③sql線程從relay log中獲取GTID,然後對比slave端的binlog是否有該GTID。
④如果有記錄,說明該GTID的事務已經執行,slave會忽略。
⑤如果沒有記錄,slave就會執行該GTID事務,並記錄該GTID到自身的binlog,在讀取執行事務前會先檢查其他session持有該GTID,確保不被重覆執行。
⑥在解析過程中會判斷是否有主鍵,如果沒有就用二級索引,如果沒有就用全部掃描。
資料庫角色 IP 系統與MySQL版本 有無數據
主資料庫 192.168.121.164 RHEL7 MySQL5.7 無數據
從資料庫 192.168.121.133 RHEL7 MySQL5.7 無數據
mysql> grant replication slave on \*.* to 'slave'@'192.168.121.%' identified by 'MySql@123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
配置主庫文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-name-resolve
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-name-resolve
log-bin=mysql-bin #開啟二進位日誌
server-id=1 #伺服器ID,必須唯一
gtid-mode=on #開啟gtid模式
enforce-gtid-consistency=on #強制gtid一致性,開啟後對特定的create table不支持
binlog-format=row #預設為mixed混合模式,更改成row複製,為了數據一致性
log-slave-updates=1 #從庫binlog記錄主庫同步的操作日誌
skip-slave-start=1 #跳過slave複製線程
binlog-ignore-db=mysql
然後重啟一下資料庫
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# systemctl status mysqld
查看GTID模式狀態
[root@localhost ~]# mysql -uroot -pMySql@123 -e 'show variables like "%GTID%";'
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-----------+
配置從庫文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-name-resolve
log-bin=mysql-bin
server-id=2
gtid-mode=on
enforce-gtid-consistency=on
binlog-format=row
log-slave-updates=1
skip-slave-start=1
然後重啟一下資料庫
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# systemctl status mysqld
查看GTID模式狀態
[root@localhost ~]# mysql -uroot -pMySql@123 -e 'show variables like "%GTID%";'
+----------------------------------+-----------+
| Variable_name | Value |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_next | AUTOMATIC |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-----------+
開啟主從同步
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to
master_host='192.168.121.164',
master_user='slave',
master_password='MySql@123',
master_auto_position=1;
使用GTID方式同步的時候,必須指定master_auto_position=1
GTID使用master_auto_position=1代替了基於binlog和position號的主從同步方式,更便於主從同步的搭建
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.11
Master_User: Jaking
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: server12-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
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: 154
Relay_Log_Space: 577
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: 11
Master_UUID: 03a1eb63-c21a-11ec-b07f-000c2987bea6
Master_Info_File: /var/lib/mysql/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:
Executed_Gtid_Set:
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
然後進行數據測試
create table aa(nid int,stuname char(15),sex int);
insert into aa value (2001,'tom',1),(2002,'mary',0),(2003,'cak',1);
select * from aa;
然後去從庫查看數據
發現庫已經有了,到這裡已經可以高興了
當然還是要看下表了,結果還是令人滿意的
這時候我們可以看下兩個庫的狀態
查看主庫狀態
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000002 | 1620 | | | 03a1eb63-c21a-11ec-b07f-000c2987bea6:1-6 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
查看從庫狀態
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000002 | 1584 | | | 03a1eb63-c21a-11ec-b07f-000c2987bea6:1-6 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
到這裡就完成辣
還有可以測試一下從庫停機後,主庫仍在寫入數據,從庫恢復後能否自動同步數據
從庫停掉主從同步
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.10.11
Master_User: Jaking
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1620
Relay_Log_File: server12-relay-bin.000002
Relay_Log_Pos: 1833
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: No
Slave_SQL_Running: No
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: 1620
Relay_Log_Space: 2043
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: NULL
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: 11
Master_UUID: 03a1eb63-c21a-11ec-b07f-000c2987bea6
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 03a1eb63-c21a-11ec-b07f-000c2987bea6:1-6
Executed_Gtid_Set: 03a1eb63-c21a-11ec-b07f-000c2987bea6:1-6
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
然後去主庫添加數據
查看從庫
是沒有同步信息過來的,因為我們沒有開啟同步
然後我們開啟同步
所有測試就完成了。
祝大家都能成功。