GTID的概念
- 全局事務標識:
global transaction identifiers
- GTID是一個事務一一對應,並且全局唯一ID
- GTID在一個服務器上只執行一次,避免重複執行導致數據混亂或主從不一致
- GTID用來代替傳統複製方法,不再使用MASTER_LOG_FILE+MASTER_LOG_POS開啓複製。而是使用MASTER_AUTO_POSTION=1的方式開始複製。
- MySQL-5.6.5開始支持的,MySQL-5.6.10後開始完善。
- 在傳統的slave端,binlog是不用開啓的,但是在GTID中slave端的binlog是必須開啓的,目的是記錄執行過的GTID(強制)。
GTID的組成
GTID = source_id:transaction_id
source_id,用於鑑別原服務器,即mysql服務器唯一的的server_uuid,由於GTID會傳遞到slave,所以也可以理解爲源ID。transaction_id,爲當前服務器上已提交事務的一個序列號,通常從1開始自增長的序列,一個數值對應一個事務。
示例:
3E11FA47-71CA-11E1-9E33-C80AA9429562:23
前面的一串爲服務器的server_uuid,即3E11FA47-71CA-11E1-9E33-C80AA9429562,後面的23爲transaction_id
GTID的優勢
1、更簡單的實現failover,不用以前那樣在需要找log_file和log_pos。
2、更簡單的搭建主從複製。
3、比傳統的複製更加安全。
4、GTID是連續的沒有空洞的,保證數據的一致性,零丟失。
GTID的工作原理
1、當一個事務在主庫端執行並提交時,產生GTID,一同記錄到binlog日誌中。
2、binlog傳輸到slave,並存儲到slave的relaylog後,讀取這個GTID的這個值設置gtid_next變量,即告訴Slave,下一個要執行的GTID值。
3、sql線程從relay log中獲取GTID,然後對比slave端的binlog是否有該GTID。
4、如果有記錄,說明該GTID的事務已經執行,slave會忽略。
5、如果沒有記錄,slave就會執行該GTID事務,並記錄該GTID到自身的binlog,
在讀取執行事務前會先檢查其他session持有該GTID,確保不被重複執行。
6、在解析過程中會判斷是否有主鍵,如果沒有就用二級索引,如果沒有就用全部掃描。
配置基於GTID的複製
環境說明:
數據庫角色 | IP | 應用與系統 |
---|---|---|
主數據庫 | 192.168.24.128 | centos7 myslq-5.7 |
從數據庫 | 192.168.24.130 | centos7 myslq-5.7 |
配置安裝二臺myslq服務
具體配置查看《mysql進階簡單解析》
在主數據庫服務器端操作如下
編輯配置文件
[root@linfan ~]# vim /etc/my.cnf
[root@linfan ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
//添加以下內容
# GTID
server-id=128 //主服務器id
gtid-mode=on //開啓gtid模式
enforce-gtid-consistency=on //強制gtid一致性,開啓後對於特定create table不被支持
# binlog
log_bin=master-binlog
log-slave-updates=1
binlog_format=row //強烈建議,其他格式可能導致數據不一致
# relay log
skip_slave_start=1
重啓myslq服務
[root@linfan ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
在主數據庫上創建一個同步賬戶授權給從數據庫使用
[root@linfan ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create user 'repl'@'192.168.24.130' identified by '123456';
Query OK, 0 rows affected (0.06 sec)
mysql> grant replication slave on *.* to 'repl'@'192.168.24.130';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)
給主數據庫上讀鎖
另開一個終端,給數據庫上讀鎖,避免在備份期間有其他人在寫入導致數據同步的不一致
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.17 sec) ////此鎖表的終端必須在備份完成以後才能退出(退出鎖表失效)
在從數據庫服務器操作如下
編輯配置文件
[root@linfan ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
# GTID
gtid_mode=on
enforce_gtid_consistency=on
server_id=130
# binlog
log-bin=slave-binlog
log-slave-updates=1
binlog-format=row //強烈建議,其他格式可能導致數據不一致
# relay log
skip-slave-start=1
重啓mysql服務器
[root@linfan ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!
配置並啓動主從複製
mysql> change master to
-> master_host='192.168.24.128',
-> master_user='repl',
-> master_password='123456',
-> master_port=3306,
-> master_auto_position= 1 ;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
查看從服務器的狀態:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.24.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-binlog.000001
Read_Master_Log_Pos: 154
Relay_Log_File: linfan-relay-bin.000002
Relay_Log_Pos: 375
Relay_Master_Log_File: master-binlog.000001
Slave_IO_Running: Yes //此處必須爲yes
Slave_SQL_Running: Yes //此處必須爲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: 583
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: 128
Master_UUID: 3396fe35-b1e1-11e8-81bb-000c292340f6
Master_Info_File: /opt/data/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)
解除主庫的鎖表狀態,直接退出交互式界面即可
mysql> quit
Bye
驗證:
在主數據庫創建一個庫doudou
mysql> create database doudou;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| doudou |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.03 sec)
在從數據庫服務器上查看是否同步存在doudou
庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| doudou |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)