MySQL master-slave replication (3)-Detailed explanation of Change Master parameters

MySQL master-slave replication (3)-Detailed explanation of Change Master parameters

When performing MySQL master-slave replication configuration, change master is used to configure and change the parameters used by the slave server to connect to the master server, so that the slave server can read the binlog of the master server and the relay log of the slave server. At the same time update master.info and relay-log.info information. Before executing the statement, if the IO thread and SQL thread have been started from the server, they need to be stopped (execute the stop slave command).

1. Overview of change master command

The format of the change master command is as follows:

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] ... ]

The information after executing the change master command is saved in two files, master.info and 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

View the contents of the master.info file:

[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 ~]# 

View the contents of the relay-log.info file:

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

[root@localhost ~]# 

The change master command involves many parameters. When the change master command is executed, if a parameter is not specified, the parameter retains the original value or default value. Therefore, if some parameters do not need to be changed, the change master to command can be omitted.

For example, if the user password used for replication is changed, change master to only needs to change the MASTER_PASSWORD option, the code is as follows:

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)

2. Detailed description of common parameters of change master

1、MASTER_HOST 与 MASTER_PORT

MASTER_HOST is used to set the host name (or IP address) of the master server; MASTER_PORT is used to set the port number of the MySQL instance in the master server.
If you specify the MASTER_HOST and MASTER_PORT parameters, the slave will think that it is not the same as the previously specified (even if the MASTER_HOST and MASTER_PORT parameters are the same as before), the previously specified master's binlog file name and location will no longer apply.

E.g:

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

2、MASTER_USER 与 MASTER_PASSWORD

Connect to the user name and password corresponding to the replication account of the master host. The four options MASTER_HOST, MASTER_PORT, MASTER_USER, and MASTER_PASSWORD provide the information of connecting the slave server to the master host.

E.g:

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

The two options MASTER_LOG_FILE and MASTER_LOG_POS determine the position coordinates of the slave's IO thread that will be read from the master when it starts executing next time.
The two options RELAY_LOG_FILE and RELAY_LOG_POS determine the position coordinates of the slave's SQL thread that will be read from the relay log when it starts executing next time.
If either of MASTER_LOG_FILE or MASTER_LOG_POS is specified, RELAY_LOG_FILE or RELAY_LOG_POS cannot be specified, nor MASTER_AUTO_POSITION = 1.

E.g:

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

This parameter was introduced in version 5.6.5 of mysql. If MASTER_AUTO_POSITION = 1 is used when running change master to, the slave server will use the GTID-based replication protocol when connecting to the master server.
When using GTID-based replication (MASTER_AUTO_POSITION = 1), first enable gtid_mode (set gtid-mode = ON in my.cnf).
If you want to restore to a file-based replication protocol after using GTID, you need to specify MASTER_AUTO_POSITION = 0 and MASTER_LOG_FILE and MASTER_LOG_POSITION when executing change master to.

5、MASTER_CONNECT_RETRY

Set the timeout waiting time when reconnecting to the master, the default is 60 seconds.

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

Limit the number of reconnections and update the Master_Retry_Count column in the show slave status output. The default value is 24 * 3600 = 86400. MASTER_RETRY_COUNT = 0 means unlimited reconnection times.

E.g:

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

The default value of MASTER_DELAY is 0, and the value range is 0 to 2^31–1, indicating that the slave is at least behind the replication time of the master. The event from the master is not executed directly, but at least until the master executes the event after the time interval specified by MASTER_DELAY.

E.g:

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

In slave replication, it is used when the slave server has multiple IPs to determine which IP to use to connect to the 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]… ], followed by 0 or more server-id separated by commas, mainly used for multi-master replication or ring replication, the server in the middle of the replication chain is abnormal, you can skip out The MySQL instance in question.

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

To clear the list of ignored hosts, use the following command:

mysql> CHANGE MASTER TO IGNORE_SERVER_IDS = ();

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/107137550