MySQL主從複製(3)——Change Master參數詳解

MySQL主從複製(3)——Change Master參數詳解

在進行MySQL主從複製配置時,change master 用於配置和改變 slave 服務器用於連接 master 服務器的參數,以便 slave服務器讀取 master 服務器的 binlog 及 slave 服務器的 relay log。同時更新 master.info 及 relay-log.info信息。執行該語句之前從服務器上如果 IO 線程和 SQL 線程已經啓動,需要先停止(執行 stop slave 命令)。

一、change master 命令概述

change master 命令的格式如下:

CHANGE MASTER TO option [, option] ...  
option:  
   MASTER_BIND = 'interface_name'  
   MASTER_HOST = 'host_name'  
   MASTER_USER = 'user_name'  
   MASTER_PASSWORD = 'password'  
   MASTER_PORT = port_num  
   MASTER_CONNECT_RETRY = interval  
   MASTER_RETRY_COUNT = count  
   MASTER_DELAY = interval  
   MASTER_HEARTBEAT_PERIOD = interval  
   MASTER_LOG_FILE = 'master_log_name'  
   MASTER_LOG_POS = master_log_pos  
   MASTER_AUTO_POSITION = {01}  
   RELAY_LOG_FILE = 'relay_log_name'  
   RELAY_LOG_POS = relay_log_pos  
   MASTER_SSL = {01}  
   MASTER_SSL_CA = 'ca_file_name'  
   MASTER_SSL_CAPATH = 'ca_directory_name'  
   MASTER_SSL_CERT = 'cert_file_name'  
   MASTER_SSL_CRL = 'crl_file_name'  
   MASTER_SSL_CRLPATH = 'crl_directory_name'  
   MASTER_SSL_KEY = 'key_file_name'  
   MASTER_SSL_CIPHER = 'cipher_list'  
   MASTER_SSL_VERIFY_SERVER_CERT = {01}  
   IGNORE_SERVER_IDS = (server_id_list)  
 server_id_list:  
    [server_id [, server_id] ... ]

執行 change master 命令後的信息保存在 master.info 和 relay-log.info 兩個文件中。

[root@localhost ~]# ll /var/lib/mysql/*info
-rw-r-----. 1 mysql mysql 126 7月   5 11:52 /var/lib/mysql/master.info
-rw-r-----. 1 mysql mysql  61 7月   5 11:28 /var/lib/mysql/relay-log.info

查看 master.info 文件的內容:

[root@localhost ~]# cat /var/lib/mysql/master.info
25
mysql-bin.000014
1818
192.168.1.11
repl
123456
3306
60
0





0
30.000

0
95cfc8eb-2d58-11ea-840b-000c296166d5
86400


0


[root@localhost ~]# 

查看 relay-log.info 文件的內容:

[root@localhost ~]# cat /var/lib/mysql/relay-log.info
7
./mysql-relay-log.000002
1539
mysql-bin.000014
1818
0
0
1

[root@localhost ~]# 

change master 命令涉及到的參數很多,在執行 change master 命令時,如果不指定某個參數的話,該參數保留原值或默認值。因此,如果某些參數不需要更改,change master to 命令可以不帶該參數。

例如,改變了用於複製的用戶密碼,則 change master to 只需要更改 MASTER_PASSWORD 選項即可,代碼如下:

mysql> stop slave;
Query OK, 0 rows affected (0.05 sec)

mysql> change master to master_password='123456';
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

二、change master 常用參數的詳細說明

1、MASTER_HOST 與 MASTER_PORT

MASTER_HOST用於設置 master 服務器的主機名(或IP地址);MASTER_PORT 用於設置 master 服務器中的MySQL實例端口號。
如果指定 MASTER_HOST 與 MASTER_PORT參數,slave會認爲與之前指定的不是同一個(即便MASTER_HOST 與 MASTER_PORT所帶的參數與之前相同),之前指定的 master 的 binlog 文件名及位置將不再適用。

例如:

change master to
....
master_host='192.168.1.11',
master_port=3306,
....;

2、MASTER_USER 與 MASTER_PASSWORD

連接到 master 主機複製賬戶所對應的用戶名和密碼。 MASTER_HOST、MASTER_PORT、MASTER_USER、MASTER_PASSWORD 四個選項提供了 slave 服務器連接到 master 主機的信息。

例如:

change master to
....
master_host='192.168.1.11',
master_port=3306,
master_user='repl',
master_password='123456',
....;

3、MASTER_LOG_FILE 與 MASTER_LOG_POS

MASTER_LOG_FILE 與 MASTER_LOG_POS 兩個選項確定 slave 的 IO 線程下次開始執行時從 master 開始讀取的位置座標。
RELAY_LOG_FILE 與 RELAY_LOG_POS 兩個選項確定 slave 的 SQL 線程下次開始執行時從relay log 開始讀取的位置座標。
如果指定了 MASTER_LOG_FILE 或 MASTER_LOG_POS 中的任意一項,就不能再指定RELAY_LOG_FILE 或 RELAY_LOG_POS,也不能指定MASTER_AUTO_POSITION = 1。

例如:

change master to
....
master_host='192.168.1.11',
master_port=3306,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000012',
master_log_pos=559,
....;

4、MASTER_AUTO_POSITION

該參數在 mysql5.6.5 版本引入,如果運行 change master to 時使用 MASTER_AUTO_POSITION = 1,slave 服務器連接 master 服務器時將使用基於 GTID 的複製協議。
使用基於 GTID 的複製時(MASTER_AUTO_POSITION = 1),首先要開啓 gtid_mode(在 my.cnf 中設置 gtid-mode= ON)。
使用 GTID 後如果要恢復到基於文件的複製協議,在執行 change master to 時需要指定 MASTER_AUTO_POSITION = 0以及 MASTER_LOG_FILE 和 MASTER_LOG_POSITION。

5、MASTER_CONNECT_RETRY

設置重新連接到 master 時的超時等待時間,默認爲60秒。

change master to
....
master_host='192.168.1.11',
master_port=3306,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000012',
master_log_pos=559,
master_connect_retry=120,
....;

6、MASTER_RETRY_COUNT

限制重新連接的次數,更新 show slave status 輸出的 Master_Retry_Count列。默認值是24 * 3600 = 86400。MASTER_RETRY_COUNT = 0 表示重新連接次數無限制。

例如:

change master to
....
master_host='192.168.1.11',
master_port=3306,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000012',
master_log_pos=559,
master_connect_retry=120,
master_retry_count=0,
....;

7、MASTER_DELAY

MASTER_DELAY 的默認值爲0,取值範圍爲0至2^31–1,表示 slave 至少落後 master 的複製時間。來自 master 的事件不直接執行,而是至少等到 master 執行完該該事件 MASTER_DELAY 所指定的時間間隔後才執行。

例如:

change master to
....
master_host='192.168.1.11',
master_port=3306,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000012',
master_log_pos=559,
master_connect_retry=120,
master_retry_count=0,
master_delay=86400,  --slave 延遲 24 小時
....;

8、MASTER_BIND

在 slave 複製時,從服務器有多個IP的情況下使用,以確定使用哪一個 IP 連接到 master。

change master to
....
master_host='192.168.1.11',
master_bin='192.168.12',
master_port=3306,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000012',
master_log_pos=559,
master_connect_retry=120,
master_retry_count=0,
master_delay=86400,  --slave 延遲 24 小時
....;

9、IGNORE_SERVER_IDS = (server_id_list)

server_id_list: [server_id [, server_id] … ],後面接以逗號分隔的0個或多個server-id,主要用於多主複製或環形複製的情況,處於複製鏈條中間的服務器異常,可以跳過出問題的MySQL實例。

mysql> CHANGE MASTER TO MASTER_HOST=xxx IGNORE_SERVER_IDS= [server_id [, server_id] ... ]

清除忽略的主機列表使用如下命令:

mysql> CHANGE MASTER TO IGNORE_SERVER_IDS = ();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章