MySQL之GTID主從配置
阿新 • • 發佈:2018-12-16
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 | 系統版本與應用 | 有無資料 |
---|---|---|---|
主資料庫 | 192.168.225.128 | redhat7 mysql-5.7 | 無資料 |
從資料庫 | 192.168.225.129 | redhat7 mysql-5.7 | 無資料 |
修改主資料庫配置檔案
[[email protected] ~]# vim /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
修改從資料庫配置檔案
[[email protected] ~]# vim /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=129 #binglog log-bin=slave-binglog log-slave-updates=1 binlog-format=row #relay log skip-slave-start=1
- 在主資料庫裡建立一個同步賬號授權給從庫使用
mysql> create user 'repl'@'192.168.225.129' identified by 'repl123.';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'repl'@'192.168.225.129';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
192.168.225.129(從庫)
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.225.128',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl123.',
-> MASTER_PORT=3306,
-> MASTER_AUTO_POSITION = 1;
Query OK, 0 rows affected, 2 warnings (0.06 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.225.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-binlog.000002
Read_Master_Log_Pos: 1019
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 1240
Relay_Master_Log_File: master-binlog.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: 1019
Relay_Log_Space: 1451
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: 85620474-e669-11e8-bdf5-000c294b7cb8
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: 85620474-e669-11e8-bdf5-000c294b7cb8:1-4
Executed_Gtid_Set: 85620474-e669-11e8-bdf5-000c294b7cb8:1-4
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
驗證:
在主庫上建立一個庫
mysql> create database dubai;
Query OK, 1 row affected (0.00 sec)
在從庫上檢視
[[email protected] ~]# mysql -uroot -phxd123456. -e'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| dubai |
| mysql |
| performance_schema |
| sys |
+--------------------+
[[email protected] ~]#
同步成功