slave_net_timeout, MASTER_HEARTBEAT_PERIOD, MASTER_CONNECT_RETRY,以及 MASTER_RETRY_COUNT设置和查看

在主从复制中,有几个参数是非常重要的,包括slave_net_timeout, MASTER_HEARTBEAT_PERIOD, MASTER_CONNECT_RETRY,以及 MASTER_RETRY_COUNT。

  • slave_net_timeout表示slave在slave_net_timeout时间之内没有收到master的任何数据(包括binlog,heartbeat),slave认为连接断开,需要重连。默认值60s。

  • MASTER_HEARTBEAT_PERIOD表示心跳的周期。当MASTER_HEARTBEAT_PERIOD时间之内,master没有binlog event发送给slave的时候,就会发送心跳数据给slave。

  • MASTER_CONNECT_RETRY表示重连的时间间隔。
    slave_net_timeout超时后,立刻重连,后续重连的时间间隔由 CHANGE MASTER TO 命令的MASTER_CONNECT_RETRY 参数指定。默认值60s。

  • MASTER_CONNECT_RETRY表示重连的最大次数。默认值86400次。

下面就介绍下这些参数的如何查看和设置。

1.slave_net_timeout的查看和设置

查看slave_net_timeout可以使用如下方式:

mysql> select @@global.slave_net_timeout;
+----------------------------+
| @@global.slave_net_timeout |
+----------------------------+
|                         60 |
+----------------------------+
1 row in set (0.00 sec)

或者

mysql> show variables like 'slave_net_timeout';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| slave_net_timeout | 50    |
+-------------------+-------+
1 row in set (0.00 sec)

设置slave_net_timeout:

mysql> set global slave_net_timeout=50;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'slave_net_timeout';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| slave_net_timeout | 50    |
+-------------------+-------+
1 row in set (0.00 sec)

2.MASTER_HEARTBEAT_PERIOD, MASTER_CONNECT_RETRY,以及 MASTER_RETRY_COUNT

2.1设置

MASTER_HEARTBEAT_PERIOD
MASTER_HEARTBEAT_PERIOD通过change master to 命令设置。
change master to 不带heartbeat参数,默认被设置成slave_net_timeoute的一半。当slave_net_timeoute改变时,不会修改heartbeat。
需要注意两点:

  • 如果将MASTER_HEARTBEAT_PERIOD 设置的比slave_net_timeout大,当主库没有数据发送到从库时,从库就会频繁重连主库。
  • 如果MASTER_HEARTBEAT_PERIOD设置为0,表示禁用heartbeat,当主库没有数据发送到从库时,从库也会频繁重连主库。

MASTER_RETRY_COUNT
MASTER_RETRY_COUNT可以通过命令行参数--master-retry-count 设置,也可以通过change master to的方式设置。
不推荐使用前者,鼓励使用change master to。

MASTER_CONNECT_RETRY
MASTER_CONNECT_RETRY 通过change master to 命令设置。

2.2 查看

从库上 执行show slave status,可以查看Master_Retry_Count,Connect_Retry等,以及更多主从复制信息Master_Info_File的存放位置。

>show slave statusG
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: xr-dba-mysql-hazyytest-test01
                  Master_User: XXXX
                  Master_Port: 5002
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 488
               Relay_Log_File: relay.000002
                Relay_Log_Pos: 698
        Relay_Master_Log_File: mysql-bin.000001
             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: 488
              Relay_Log_Space: 892
              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: 48201050
                  Master_UUID: 10035945-ddeb-11e9-89f3-0022ac533a2f
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: 10035945-ddeb-11e9-89f3-0022ac533a2f:1-2
            Executed_Gtid_Set: 10035945-ddeb-11e9-89f3-0022ac533a2f:1-2
                Auto_Position: 1
1 row in set (0.00 sec)

从上面结果可以看到,Master_Info_File存放在数据表中:
Master_Info_File: mysql.slave_master_info

接着通过查询数据表,查看这几个参数:

>select * from mysql.slave_master_infoG
*************************** 1. row ***************************
       Number_of_lines: 23
       Master_log_name: mysql-bin.000001
        Master_log_pos: 488
                  Host: xr-dba-mysql-hazyytest-test01
             User_name: XXX
         User_password: XXXX
                  Port: 5002
         Connect_retry: 10
           Enabled_ssl: 0
                Ssl_ca:
            Ssl_capath:
              Ssl_cert:
            Ssl_cipher:
               Ssl_key:
Ssl_verify_server_cert: 0
             Heartbeat: 8
                  Bind:
    Ignored_server_ids: 0
                  Uuid: 10035945-ddeb-11e9-89f3-0022ac533a2f
           Retry_count: 86400
               Ssl_crl:
           Ssl_crlpath:
 Enabled_auto_position: 1
1 row in set (0.00 sec)

这里,Connect_retry为10,Heartbeat为8,Retry_count 为86400。

>select Connect_retry,Heartbeat, Retry_count from mysql.slave_master_info;
+---------------+-----------+-------------+
| Connect_retry | Heartbeat | Retry_count |
+---------------+-----------+-------------+
|            10 |         8 |       86400 |
+---------------+-----------+-------------+
1 row in set (0.00 sec)

另外,这几个参数是配置在从库上的,主库上执行查询命令,结果是空的。

>select Connect_retry,Heartbeat, Retry_count from mysql.slave_master_info;
Empty set (0.00 sec)

在MySQL5.7版本中,还可以在replication-connection-configuration-table中查看这几个参数。

> select * from performance_schema.replication_connection_configurationG
*************************** 1. row ***************************
                 CHANNEL_NAME:
                         HOST: 10.23.66.88
                         PORT: 5002
                         USER: repl
            NETWORK_INTERFACE:
                AUTO_POSITION: 1
                  SSL_ALLOWED: NO
                  SSL_CA_FILE:
                  SSL_CA_PATH:
              SSL_CERTIFICATE:
                   SSL_CIPHER:
                      SSL_KEY:
SSL_VERIFY_SERVER_CERTIFICATE: NO
                 SSL_CRL_FILE:
                 SSL_CRL_PATH:
    CONNECTION_RETRY_INTERVAL: 60
       CONNECTION_RETRY_COUNT: 86400
           HEARTBEAT_INTERVAL: 60.000
                  TLS_VERSION:
1 row in set (0.01 sec)

3.参考

slave_net_timeout

change master to

replication-connection-configuration-table

原文地址:https://www.cnblogs.com/lanyangsh/p/12375274.html