MySQL 各種超時參數的含義


MySQL [(none)]> show variables like '%timeout%'; +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | +------------------------------+----------+ 13 rows in set (0.00 sec)
  • PS:文檔說明
  • 根據這些參數的global和session級別分別進行闡述
  • 基於MySQL 5.6.30編寫
  • 加載了半同步復制插件,所以才能看到半同步相關的參數
  • 驗證演示過程可能會打開兩個MySQL會話進行驗證,也可能只打開一個MySQL會話進行驗證
  • 只針對大家平時容易高混淆的或者說不好理解的超時參數做步驟演示,容易理解的超時參數只做文字描述,不做步驟演示
  • 大部分參數基於MySQL命令行客戶端做的演示,但wait_timeout和interactive_timeout這兩個比較特殊,為了對比不同客戶端的差異,還使用了python演示

1、連接、網絡類超時

  • 共有如下幾個:
  • connect_timeout:默認為10S
  • wait_timeout:默認是8小時,即28800秒
  • interactive_timeout:默認是8小時,即28800秒
  • net_read_timeout:默認是30S
  • net_write_timeout:默認是60S

1.1. 針對網絡類超時參數,先簡單梳理一下在MySQL建立連接、發送數據包的整個過程中,每一個階段都用到了哪些超時參數

a)、connect_timeout:在獲取連接階段(authenticate)起作用

  • 獲取MySQL連接是多次握手的結果,除了用戶名和密碼的匹配校驗外,還有IP->HOST->DNS->IP驗證,任何一步都可能因為網絡問題導致線程阻塞。為了防止線程浪費在不必要的校驗等待上,超過connect_timeout的連接請求將會被拒絕。
  • 官方描述:connect_timeout(The number of seconds that the mysqld server waits for a connect packet before responding with Bad handshake. The default value is 10 seconds)

b)、interactive_timeout和wait_timeout:在連接空閑階段(sleep)起作用

  • 即使沒有網絡問題,也不能允許客戶端一直占用連接。對於保持sleep狀態超過了wait_timeout(或interactive_timeout,取決於client_interactive標志)的客戶端,MySQL會主動斷開連接。
  • 官方描述:
  • wait_timeout:The number of seconds the server waits for activity on a noninteractive connection before closing it. On thread startup, the session wait_timeout value is initialized from the global wait_timeout value or from the global interactive_timeoutvalue, depending on the type of client (as defined by the CLIENT_INTERACTIVE connect option to mysql_real_connect()).
  • interactive_timeout:The number of seconds the server waits for activity on an interactive connection before closing it. An interactive client is defined as a client that uses the CLIENT_INTERACTIVE option to mysql_real_connect()

c)、net_read_timeout和net_write_timeout:則是在連接繁忙階段(query)起作用。

  • 即使連接沒有處於sleep狀態,即客戶端忙於計算或者存儲數據,MySQL也選擇了有條件的等待。在數據包的分發過程中,客戶端可能來不及響應(發送、接收、或者處理數據包太慢)。
  • 為了保證連接不被浪費在無盡的等待中,MySQL也會選擇有條件(net_read_timeout和net_write_timeout)地主動斷開連接。
  • 這個參數只對TCP/IP鏈接有效,只針對在Activity狀態下的線程有效
  • 官方描述:
  • net_read_timeout:The number of seconds to wait for more data from a connection before aborting the read. When the server is reading from the client,net_read_timeout is the timeout value controlling when to abort. When the server is writing to the client, net_write_timeout is the timeout value controlling when to abort
  • net_write_timeout:The number of seconds to wait for a block to be written to a connection before aborting the write. See also net_read_timeout.

d)、 handshake流程

  • 在TCP三次握手的基礎之上,簡歷MySQL通訊協議的連接,這個連接建立過程受connect_timeout參數控制
    --------------------TCP established--------------------
    MySQL Server(10.10.20.96)------->Client(10.10.20.51)
    Client(10.10.20.51)------->MySQL Server(10.10.20.96)
    MySQL Server(10.10.20.96)------->Client(10.10.20.51)

--------------------established--------------------

  • 在MySQL通訊協議建立連接之后,此時客戶端連接的超時受wait_timeout和interactive_timeout參數控制
    建立連接后無交互:MySQL server ---wait_timeout--- Client
    建立連接交互后:MySQL server ---interactive_timeout--- Client
  • 在如果客戶端有數據包傳輸,那么這個數據包的傳輸超時由net_read_timeout和net_write_timeout參數控制
    -------------------client與server端有數據傳輸時-------------------
    client ----->MySQL Server(net_read_timeout)
    client <-----MySQL Server(net_write_timeout)

1.2. connect_timeout:該參數沒有session級別,是一個global級別變量

## 使用mysql客戶端打開一個會話,並設置全局 connect_timeout=5 MySQL [(none)]> set global connect_timeout=5; Query OK, 0 rows affected (0.00 sec) MySQL [(none)]> ## 由於mysql客戶端不是很好模擬連接階段(authenticate)的超時,所以使用telnet來發包給mysql,因為telnet的包並不遵循mysql的通訊協議 [root@localhost ~]# time telnet 127.0.0.1 3306 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. N 5.6.30-logwA{k)'&)S9#A`?Z&O9pJ`mysql_native_passwordConnection closed by foreign host. real 0m5.022s #這里可以看到5S之后連接斷開 user 0m0.000s sys 0m0.010s ## 回到mysql客戶端:修改全局 connect_timeout為10S MySQL [(none)]> set global connect_timeout=10; Query OK, 0 rows affected (0.00 sec) MySQL [(none)]> ## 使用telnet再試一次 [root@localhost ~]# time telnet 127.0.0.1 3306 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. N 5.6.30-loggZoA3{6:S\D}iu3;n:uafmysql_native_passwordConnection closed by foreign host. real 0m10.012s user 0m0.000s sys 0m0.002s
  • 從上面的結果中可以看到,MySQL客戶端與服務端的連接階段(authenticate)的超時由參數connect_timeout控制。

1.3. interactive_tineout和wait_timeout參數

1.3.1. interactive_timeout:(MySQL命令行客戶端)
1.3.1.1. session級別修改interactive_timeout
## 打開第一個會話,設置session級別的interactive_timeout=2 MySQL [(none)]> set session interactive_timeout=2; Query OK, 0 rows affected (0.00 sec) MySQL [(none)]> select sleep(3);show session variables like '%timeout%';show global variables like '%timeout%'; +----------+ | sleep(3) | +----------+ | 0 | +----------+ 1 row in set (3.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 2 | #session級別的interactive_timeout改變了 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #session級別的wait_timeout沒有影響 +------------------------------+----------+ 13 rows in set (0.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #global級別的interactive_timeout沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #global級別的wait_timeout沒有影響 +------------------------------+----------+ 13 rows in set (0.00 sec) ## 打開第二個會話,執行show語句 MySQL [(none)]> show session variables like '%timeout%';show global variables like '%timeout%'; +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #session級別的interactive_timeout沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #session級別的wait_timeout沒有影響 +------------------------------+----------+ 13 rows in set (0.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #global級別的interactive_timeout沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #global級別的wait_timeout沒有影響 +------------------------------+----------+ 13 rows in set (0.00 sec)
  • 從上面的結果可以看到,設置session級別的interactive_timeout對wait_timeout的session和global級別都沒有影響
1.3.1.2. global級別修改interactive_timeout
### 回到第一個會話中,設置global interactive_timeout=20 MySQL [(none)]> set global interactive_timeout=20; Query OK, 0 rows affected (0.00 sec) MySQL [(none)]> select sleep(3);show session variables like '%timeout%';show global variables like '%timeout%'; +----------+ | sleep(3) | +----------+ | 0 | +----------+ 1 row in set (3.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 2 | #session級別的interactive_timeout沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #session級別的wait_timeout沒有影響 +------------------------------+----------+ 13 rows in set (0.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 20 | #global級別的interactive_timeout改變了 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #global級別的wait_timeout沒有影響 +------------------------------+----------+ 13 rows in set (0.00 sec) # 第二個會話斷開之后重連,再執行show語句 MySQL [(none)]> show session variables like '%timeout%';show global variables like '%timeout%'; +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 20 | #session級別的interactive_timeout改變了 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 20 | #session級別的wait_timeout改變了 +------------------------------+----------+ 13 rows in set (0.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 20 | #global級別的interactive_timeout改變了 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #global級別的wait_timeout沒有影響 +------------------------------+----------+ 13 rows in set (0.00 sec)
  • 從上面的結果中可以看到:如果改變了global級別的interactive_timeout值,對當前連接不生效,對后續新連接的wait_timeout的session級別生效,global級別不生效,interactive_timeout的global級別和session級別都生效
1.3.2. wait_timeout:(MySQL命令行客戶端)
1.3.2.1. session級別修改wait_timeout
  • 這里為了驗證后續的值不產生混亂,先把interactive_timeout的值恢復為172800並重連連接(connect_timeout默認是10,此時已經是這個值了,不用再修改),然后再修改wait_timeout
MySQL [(none)]> set global interactive_timeout=172800; Query OK, 0 rows affected (0.00 sec) MySQL [(none)]> Ctrl-C -- exit! Aborted [root@localhost ~]# mysql -uqogir_env -p'letsg0' -S /home/mysql/data/mysqldata1/sock/mysql.sock Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 21 Server version: 5.6.30-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show session variables like '%timeout%';show global variables like '%timeout%'; +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | +------------------------------+----------+ 13 rows in set (0.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | +------------------------------+----------+ 13 rows in set (0.00 sec) 
  • 現在,開始1.3.2.小節的驗證

# 打開第一個會話,修改session級別wait_timeout=2 MySQL [(none)]> set session wait_timeout=2; Query OK, 0 rows affected (0.00 sec) MySQL [(none)]> select sleep(3);show session variables like '%timeout%';show global variables like '%timeout%'; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 22 Current database: *** NONE *** #從這里可以看到,當前連接被斷開並重連了 +----------+ | sleep(3) | +----------+ | 0 | +----------+ 1 row in set (3.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #重連之后的session級別參數, interactive_timeout 沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #重連之后的session級別參數,wait_timeout恢復了172800 +------------------------------+----------+ 13 rows in set (0.01 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #重連之后的global級別參數, interactive_timeout 沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #重連之后的global級別參數,wait_timeout恢復了172800,即新的連接不受影響 +------------------------------+----------+ 13 rows in set (0.00 sec) # 打開第二個會話,第二個會話注意要重連 MySQL [(none)]> show session variables like '%timeout%';show global variables like '%timeout%'; +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #session級別的interactive_timeout沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #session級別的wait_timeout沒有影響 +------------------------------+----------+ 13 rows in set (0.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #global級別的interactive_timeout沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #global級別的wait_timeout沒有影響 +------------------------------+----------+ 13 rows in set (0.00 sec) # 對於超時斷開的連接,錯誤日志中會報如下錯誤: 2016-11-07 19:08:24 3391 [Warning] Aborted connection 21 to db: 'unconnected' user: 'qogir_env' host: 'localhost' (Got timeout reading communication packets) 
  • 從上面的結果中可以看到:
  • session級別的wait_timeout變量在連接初始化時,繼承global的interactive_timeout參數值
  • session級別的wait_timeout對當前交互連接生效(即當前連接的超時使用的是session wait_timeout,session interactive_timeout不生效)
  • 有一點要注意,如果是新的連接(即斷開重連的或者新的連接),session級別的wait_timeout會使用global級別的interactive_timeout值覆蓋,因為interactive_timeout值是對后續新連接生效(參考1.2.2小節驗證過程)
1.3.2.2. global級別修改wait_timeout

# 打開第一個會話,修改global wait_timeout=2 MySQL [(none)]> set global wait_timeout=2; Query OK, 0 rows affected (0.00 sec) MySQL [(none)]> select sleep(3);show session variables like '%timeout%';show global variables like '%timeout%'; +----------+ | sleep(3) | +----------+ | 0 | +----------+ 1 row in set (3.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #session級別的interactive_timeout沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #session級別的wait_timeout沒有影響 +------------------------------+----------+ 13 rows in set (0.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #global級別的interactive_timeout沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 2 | #global級別的wait_timeout改變了 +------------------------------+----------+ 13 rows in set (0.00 sec) # 打開第二個會話,注意需要斷開重連,再執行show語句 MySQL [(none)]> show session variables like '%timeout%';show global variables like '%timeout%'; +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #session級別的interactive_timeout沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 172800 | #session級別的wait_timeout沒有影響,因為前面說過,這里新連接的session的wait_timeout會繼承global interactive_timeout的值 +------------------------------+----------+ 13 rows in set (0.00 sec) +------------------------------+----------+ | Variable_name | Value | +------------------------------+----------+ | connect_timeout | 10 | | delayed_insert_timeout | 300 | | innodb_flush_log_at_timeout | 1 | | innodb_lock_wait_timeout | 120 | | innodb_rollback_on_timeout | ON | | interactive_timeout | 172800 | #global級別的interactive_timeout沒有影響 | lock_wait_timeout | 31536000 | | net_read_timeout | 30 | | net_write_timeout | 60 | | rpl_semi_sync_master_timeout | 10000 | | rpl_stop_slave_timeout | 31536000 | | slave_net_timeout | 10 | | wait_timeout | 2 | #global級別的wait_timeout改變了 +------------------------------+----------+ 13 rows in set (0.00 sec) 
  • 從上面的結果中可以看到:global級別的wait_timeout變量在初始化時,繼承global的wait_timeout參數值,默認8小時
1.3.3. interactive_timeout和wait_timeout參數(python MySQL driver)
  • 本小節演示的python代碼如下:

#cat test_timeout.py #!/bin/env python # coding=utf8 import MySQLdb import sys import time # 設置wait_timeout的值 wait_timeout=5 # 設置interactive_timeout的侄 interactive_timeout=10 # MySQL帳號 mysql_user='qbench' # MySQL密碼 mysql_password='qbench' # MySQL ip地址 mysql_ip='10.10.30.68' rest_conn = MySQLdb.connect(user=mysql_user,passwd=mysql_password,host=mysql_ip) rest_cur = rest_conn.cursor() rest_cur.execute("show variables like '%timeout%';") datas = rest_cur.fetchall() datas = dict(datas) rest_wait_timeout = datas['wait_timeout'] rest_interactive_timeout = datas['interactive_timeout'] rest_cur.close() rest_conn.close() def new_connect(info,timeout): new_conn = MySQLdb.connect(user=mysql_user,passwd=mysql_password,host=mysql_ip) new_cur = new_conn.cursor() print '%s \n%s' % ('-' * 50,str(info)) #sql = "select sleep(%s);" % int(timeout+1) #print "執行sleep sql語句:%s" % str(sql) new_cur.execute("show variables like '%timeout%';") new_datas = new_cur.fetchall() new_datas = dict(new_datas) print 'wait_timeout=%s' % new_datas['wait_timeout'] print 'interactive_timeout=%s' % new_datas['interactive_timeout'] print "sleep %s 秒之后再次執行sql---" % int(timeout) time.sleep(int(timeout)) #new_cur.execute("%s" % str(sql)) new_cur.execute("show variables like '%timeout%';") new_datas = new_cur.fetchall() new_datas = dict(new_datas) print 'wait_timeout=%s' % new_datas['wait_timeout'] print 'interactive_timeout=%s' % new_datas['interactive_timeout'] new_cur.close() new_conn.close() def current_connect(): curr_conn = MySQLdb.connect(user=mysql_user,passwd=mysql_password,host=mysql_ip) curr_cur = curr_conn.cursor() print "在第一個連接中修改global wait_timeout為:%s" % wait_timeout curr_cur.execute("set global wait_timeout=%s;" % wait_timeout) curr_cur.execute("show variables like '%timeout%';") curr_datas1 = curr_cur.fetchall() curr_datas1 = dict(curr_datas1) print "%s\n第一個連接保持不斷開的session級別的超時信息:" % ('-' * 100) print 'wait_timeout=%s' % curr_datas1['wait_timeout'] print 'interactive_timeout=%s' % curr_datas1['interactive_timeout'] new_connect(info='第一個連接修改global wait_timeout為:%s之后,登錄新的連接的session級別的超時信息如下:' % wait_timeout,timeout=wait_timeout) restore() curr_cur.close() curr_cur = curr_conn.cursor() print "在第一個連接中修改global interactive_timeout為:%s" % interactive_timeout curr_cur.execute("set global interactive_timeout=%s;" % interactive_timeout) curr_cur.execute("show variables like '%timeout%';") curr_datas2 = curr_cur.fetchall() curr_datas2 = dict(curr_datas2) print "%s\n第一個連接保持不斷開的session級別的超時信息:" % ('-' * 100) print 'wait_timeout=%s' % curr_datas2['wait_timeout'] print 'interactive_timeout=%s' % curr_datas2['interactive_timeout'] new_connect(info='第一個連接修改global interactive_timeout為:%s之后,登錄新的連接的session級別的超時信息如下:' % interactive_timeout,timeout=interactive_timeout) curr_cur.close() curr_conn.close() def restore(): print "開啟新的連接執行恢復參數初始設置----------------------" rest_conn = MySQLdb.connect(user=mysql_user,passwd=mysql_password,host=mysql_ip) rest_cur = rest_conn.cursor() rest_cur.execute("set global wait_timeout=%s,interactive_timeout=%s;" % (rest_wait_timeout,rest_interactive_timeout)) rest_cur.close() rest_conn.close() print '=' * 100 try: current_connect() except Exception,e: print e else: restore() print '=' * 100 
  • 跑一下這個腳本,打印結果如下:

#python test_timeout.py ==================================================================================================== 在第一個連接中修改global wait_timeout為:5 ---------------------------------------------------------------------------------------------------- 第一個連接保持不斷開的session級別的超時信息: wait_timeout=5 interactive_timeout=172800 -------------------------------------------------- 第一個連接修改global wait_timeout為:5之后,登錄新的連接的session級別的超時信息如下: wait_timeout=5 interactive_timeout=172800 sleep 5 秒之后再次執行sql--- (2013, 'Lost connection to MySQL server during query') ==================================================================================================== 
  • 從上面的結果中可以看到,第一個會話中修改global wait_timeout=5之后,新的連接上來,超過5秒沒有發送新的數據包,連接就被斷開。
  • 綜合1.3小節演示結果來看
  • MySQL命令行客戶端下:global級別的interactive_timeout修改對當前連接不生效,但能影響新的連接的globa interactive_timeout、session interactive_timeout、session wait_timeout數值
  • MySQL命令行客戶端下:session級別的interactive_timeout的修改除了能使session interactive_timeout數值改變之外沒有什么作用
  • MySQL命令行客戶端下:global級別的wait_timeout的修改除了能使global wait_timeout數值改變之外沒有什么作用
  • MySQL命令行客戶端下:session級別的wait_timeout能改變session wait_timeout數值其對當前連接生效。
  • python MySQL driver:修改global wait_timeout對當前連接不生效,但能影響新的連接的global wait_timeout、session wait_timeout
  • python MySQL driver:修改session wait_timeout只對當前連接生效
  • python MySQL driver:修改global interactive_timeout對當前連接不生效,能影響新的連接的global interactive_timeout、session interactive_timeout
  • python MySQL driver:修改session interactive_timeout除了能使session interactive_timeout數值改變之外沒有什么作用
  • PS:思考?
  • 為什么MySQL命令行客戶端中新的連接的session wait_timeout不是使用的global wait_timeout的值,而是使用的interactive_timeout的值?但是,為什么python MySQL driver中,新的連接的session wait_timeout就是按照正常的邏輯使用的是global wait_timeout的值?這里先賣個關子,問題的答案得去源碼中找,參考鏈接:http://dev.mysql.com/doc/refman/5.6/en/mysql-real-connect.html
1.4. net_write_timeout
  • mysql服務端向客戶端寫(發送)數據時,服務端等待客戶端響應的超時時間,當服務端正在寫數據到客戶端時,net_write_timeout控制何時超時
  • 對於這個參數,session和global級別並沒有什么特別,session級別只對當前連接生效,global級別只對新的連接生效。默認值是60S
  • 下面使用tc命令模擬網絡延遲來進行演示

## 使用sysbench在MySQL server上造數一張500W行數據的表 ## tc命令對MySQL客戶端的網卡加延遲 tc qdisc add dev eth0 root netem delay 1s ## MySQL 客戶端登錄server,修改net_write_timeout參數為1S mysql -uqbench -pqbench -h 10.10.30.68 mysql > set global net_write_timeout=1; Query OK, 0 rows affected (0.00 sec) ## 在MySQL客戶端使用mysqldump備份 [root@20bc83fd-1489-4b60-976b-d1823e7dc36e data] # time mysqldump -uqbench -pqbench -h 10.10.30.68 --single-transaction --master-data=2 sbtest sbtest2 > sbtest2.sql Warning: Using a password on the command line interface can be insecure. mysqldump: Error 2013: Lost connection to MySQL server during query when dumping table `sbtest2` at row: 85 #從這里可以看到,不到一分鍾時間,連接就被斷開了 real 0m54.049s user 0m0.009s sys 0m0.011s ## MySQL客戶端登錄server,修改net_write_timeout參數為默認的60S mysql -uqbench -pqbench -h 10.10.30.68 mysql > set global net_write_timeout=60; Query OK, 0 rows affected (0.00 sec) ## 在MySQL客戶端使用mysqldump重試備份 [root@20bc83fd-1489-4b60-976b-d1823e7dc36e data]# time mysqldump -uqbench -pqbench -h 10.10.30.68 --single-transaction --master-data=2 sbtest sbtest2 > sbtest2.sql Warning: Using a password on the command line interface can be insecure. real 14m41.744s user 0m18.662s sys 0m7.886s [root@20bc83fd-1489-4b60-976b-d1823e7dc36e data]# ls -lh total 963M drwxr-xr-x 12 mysql mysql 137 Dec 30 15:04 mysqldata1 drwxr-xr-x 2 mysql mysql 6 Dec 30 15:04 recovery -rw-r--r-- 1 root root 963M Dec 30 15:30 sbtest2.sql #這里可以看到,消耗15分鍾之后,備份成功,備份文件大小接近1G [root@20bc83fd-1489-4b60-976b-d1823e7dc36e data]# 
1.5. net_read_timeout
  • mysql服務端從客戶端讀取(接收)數據時,服務端等待客戶端響應的超時時間,當服務端正在從客戶端讀取數據時,net_read_timeout控制何時超時
  • 對於這個參數,session和global級別並沒有什么特別,session級別只對當前連接生效,global級別只對新的連接生效。默認值是30S
  • 下面接着1.4小節進行演示,使用1.4小節中的備份結果導入數據庫

## MySQL客戶端登錄server,先查看一下net_read_timeout參數的侄
Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15453 Server version: 5.6.30-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> show variables like '%net_read_timeout%'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | net_read_timeout | 30 | +------------------+-------+ 1 row in set (0.00 sec) mysql> ## 現在,把1.4小節備份出來的sbtest2.sql文件導入server中的sbtest庫 [root@20bc83fd-1489-4b60-976b-d1823e7dc36e data]# time mysql -uqbench -pqbench -h 10.10.30.68 sbtest < sbtest2.sql Warning: Using a password on the command line interface can be insecure. real 37m17.831s #導入成功,耗時38分鍾左右 user 0m22.797s sys 0m3.436s ## 現在,使用MySQL客戶端登錄server,修改net_read_timeout參數 [root@555f12f7-850d-4f42-867c-2d12890beb40 data]# mysql -uqbench -pqbench -h 10.10.30.68 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 17040 Server version: 5.6.30-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> set global net_read_timeout=1; Query OK, 0 rows affected (0.00 sec) mysql> ## 修改tc模擬規則,模擬丟包10%,損壞包20%,延遲2秒,包亂序20% tc qdisc del dev eth0 root tc qdisc add dev eth0 root netem corrupt 20% loss 10% delay 2s reorder 20% ## 使用備份文件再次嘗試導入 time mysql -uqbench -pqbench -h 10.10.30.68 sbtest < sbtest2.sql ## 很囧的一個事情發生了。此時反復查看server端的processlist,只發現客戶端連接上來了,但是一直是sleep狀態 mysql> show processlist; +-------+--------+-------------------+--------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +-------+--------+-------------------+--------+---------+------+-------+------------------+ | 17129 | qbench | 10.10.30.78:16167 | sbtest | Sleep | 207 | | NULL | | 17159 | qbench | 10.10.30.68:47148 | NULL | Query | 0 | init | show processlist | +-------+--------+-------------------+--------+---------+------+-------+------------------+ 2 rows in set (0.00 sec) mysql> kill 17129; ## 嘗試kill掉這個連接 Query OK, 0 rows affected (0.00 sec) mysql> show processlist; +-------+--------+-------------------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +-------+--------+-------------------+------+---------+------+-------+------------------+ | 17159 | qbench | 10.10.30.68:47148 | NULL | Query | 0 | init | show processlist | +-------+--------+-------------------+------+---------+------+-------+------------------+ 1 row in set (0.00 sec) mysql> use sbtest Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select count(*) from sbtest2; ## 然后再查詢一下sbtest2表的數據,發現是空的 +----------+ | count(*) | +----------+ | 0 | +----------+ 1 row in set (0.00 sec) ## 此時,查看客戶端的導入數據的連接 [root@20bc83fd-1489-4b60-976b-d1823e7dc36e data]# time mysql -uqbench -pqbench -h 10.10.30.68 sbtest < sbtest2.sql ERROR 2006 (HY000) at line 47: MySQL server has gone away ## 發現斷開了,囧。。 real 5m42.419s user 0m0.031s sys 0m0.017s 
  • 從上面的結果中可以看到:修改net_read_timeout=1,並在客戶端導入數據到server的時候,並沒有如預期的超時斷開客戶端連接。猜測可能是客戶端導入數據到server端的時候,server端接收包超時之后沒有發起kill掉客戶端的操作,所以不手動執行一把kill的話,客戶端一直在那里不動,而server端的連接線程也一直處於sleep狀態
  • PS:
  • 1.4和1.5小節演示用數據庫帳號權限:SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, INDEX, ALTER, SUPER, LOCK TABLES, PROCESS
  • 與net_read_timeout和net_write_timeout相關的還有一個參數,net_retry_count,官方描述如下:
    If a read or write on a communication port is interrupted, retry this many times before giving up.

2、鎖類超時

2.1. innodb_lock_wait_timeout
  • 官方描述:
    The length of time in seconds an InnoDB transaction waits for a row lock before giving up
  • innodb使用這個參數能夠有效避免在資源有限的情況下產生太多的鎖等待;指的是事務等待獲取資源時等待的最長時間,超過這個時間還未分配到資源則會返回應用失敗;參數的時間單位是秒,最小可設置為1s(一般不會設置得這么小),最大可設置1073741824秒(34年,一條語句鎖等待超過30分鍾估計業務該有反饋了),默認安裝時這個值是50s,超過這個時間會報 ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

## 第一個會話,創建測試數據,並設置innodb_lock_wait_timeout=1 MySQL [(none)]> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MySQL [test]> create table test(id int); Query OK, 0 rows affected (0.03 sec) MySQL [test]> insert into test values(1); Query OK, 1 row affected (0.01 sec) MySQL [test]> select * from test; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec) MySQL [test]> set innodb_lock_wait_timeout=1; Query OK, 0 rows affected (0.00 sec) MySQL [test]> ## 打開第二個會話,注意第二個會要重連,然后打開一個事務,使用select...for update不提交 MySQL [test]> use test Database changed MySQL [test]> begin; Query OK, 0 rows affected (0.00 sec) MySQL [test]> select * from test where id=1 for update; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec) MySQL [test]> ## 此時 回到第一個會話中,執行相同的select..for update語句,等到1S之后會話超時終止 MySQL [test]> select * from test where id=1 for update; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction MySQL [test]> 
  • 對於這個參數,session和global級別並沒有什么特別,session級別只對當前連接生效,global級別只對新的連接生效
  • 從上面的結果中可以看到,把innodb_lock_wait_timeout設置為1S之后,對於同一行的操作,鎖等待超過1S就被終止事務了
  • PS:
  • 測試結果是在RR隔離級別下基於innodb表的DML操作
2.2. innodb_rollback_on_timeout
  • 官方描述:
    In MySQL 5.6, InnoDB rolls back only the last statement on a transaction timeout by default. If --innodb_rollback_on_timeout is specified, a transaction timeout causes InnoDB to abort and roll back the entire transaction
  • 默認情況下innodb_lock_wait_timeout 超時后只是超時的sql執行失敗,整個事務並不回滾,也不做提交,如需要事務在超時的時候回滾,則需要設置innodb_rollback_on_timeout=ON,該參數默認為OFF

## 先測試一下innodb_rollback_on_timeout為默認值時的情況,打開第一個會話,顯式開啟一個事務,插入幾行測試數據,不提交: MySQL [test]> show variables like '%rollback%'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_rollback_on_timeout | OFF | | innodb_rollback_segments | 128 | +----------------------------+-------+ 2 rows in set (0.00 sec) MySQL [test]> use test Database changed MySQL [test]> show tables; Empty set (0.00 sec) MySQL [test]> create table test(id int); Query OK, 0 rows affected (0.05 sec) MySQL [test]> begin; Query OK, 0 rows affected (0.00 sec) MySQL [test]>insert into test(id) values(1),(2),(3),(4); Query OK, 1 row affected (0.00 sec) MySQL [test]> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | +------+ 4 rows in set (0.00 sec) ## 現在,打開第二個會話,顯式開啟一個事務,並插入數據5,不提交 MySQL [(none)]> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A MySQL [(none)]> begin; Query OK, 0 rows affected (0.00 sec) MySQL [test]> insert into test values(5); Query OK, 1 row affected (0.00 sec) MySQL [test]> select * from test; +------+ | id | +------+ | 5 | +------+ 2 rows in set (0.00 sec) ## 再回到第一個會話中,更新id為5的數據行為6 MySQL [test]> update test set id=6 where id=5; #因為第二個會話插入第=5這行數據時,對5及其以后的范圍加了鎖,也沒有提交,所以這個這里的操作需要進行鎖等待 ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction MySQL [test]> select * from test ; #這里可以看到,超時之后,第一個會話最開始在顯式事務中插入的幾行數據並沒有回滾 +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | +------+ 4 rows in set (0.00 sec) ##此時,你需要自行決定會話1中插入的數據是要提交,還是需要回滾,當然,如果斷開連接,事務會自動回滾,為了方便后續的測試,先在兩個會話中都做rollback操作 
  • 從上面的結果中可以看到,默認情況下innodb_rollback_on_timeout為OFF,此時超時終止的會話中的事務DML修改的數據不會自動回滾。
  • 現在,把innodb_rollback_on_timeout參數在my.cnf中加入並改為ON,重啟mysql,再次插入相同數據試試看

## 第一個會話中顯示開啟一個事務,插入幾行數據,不提交 MySQL [test]> show variables like '%rollback%'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_rollback_on_timeout | ON | | innodb_rollback_segments | 128 | +----------------------------+-------+ 2 rows in set (0.00 sec) MySQL [test]> use test Database changed MySQL [test]> begin; Query OK, 0 rows affected (0.00 sec) MySQL [test]>insert into test(id) values(1),(2),(3),(4); Query OK, 1 row affected (0.00 sec) MySQL [test]> select * from test; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | +------+ 4 rows in set (0.00 sec) ## 現在,打開第二個會話,顯式開啟一個事務,並插入數據5,不提交 MySQL [(none)]> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A MySQL [(none)]> begin; Query OK, 0 rows affected (0.00 sec) MySQL [test]> insert into test values(5); Query OK, 1 row affected (0.00 sec) MySQL [test]> select * from test; +------+ | id | +------+ | 5 | +------+ 2 rows in set (0.00 sec) ## 再回到第一個會話中,更新id為5的數據行為6 MySQL [test]> update test set id=6 where id=5; #因為第二個會話插入第=5這行數據時,對5及其以后的范圍加了鎖,也沒有提交,所以這個這里的操作需要進行鎖等待 ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction MySQL [test]> select * from test ; #這里可以看到,超時之后,第一個會話最開始在顯式事務中插入的幾行數據已經回滾 Empty set (0.00 sec) 
  • 從上面的結果中可以看到,把參數innodb_rollback_on_timeout設置為ON之后(注意,這個變量是只讀變量,需要添加到my.cnf中並重啟mysql),如果一個事務發生鎖等待超時,那么這個事務沒有提交的數據都會被回滾掉。
2.3. lock_wait_timeout
  • 官方描述:
    This variable specifies the timeout in seconds for attempts to acquire metadata locks.
  • 這里不得不提一下2.1小節的innodb_lock_wait_timeout超時參數,相信有不少人是沒有搞太清楚這兩者的區別,從字面上來看,前者是innodb的dml操作的行級鎖的等待時間 后面是獲取MDL鎖的等待時間,默認值是31536000秒=1年。那么,下面來演示一把吧

## 打開第一個會話,顯示開啟一個會話,執行select...for update語句,不提交事務 MySQL [test]> begin; Query OK, 0 rows affected (0.00 sec) MySQL [test]> select * from test for update; +------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | +------+ 5 rows in set (0.00 sec) ## 現在,打開第二個會話,修改session lock_wait_timeout=5,並執行DDL語句 MySQL [test]> set lock_wait_timeout=5; Query OK, 0 rows affected (0.00 sec) MySQL [test]> use test Database changed MySQL [test]> alter table test add column test varchar(100); #DDL語句執行被阻塞,5秒之后超時終止 ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction MySQL [test]> 
  • 從上面的結果中可以看到,DDL語句的超時時間是受lock_wait_timeout參數控制的
  • PS:注意,凡是需要獲取MDL鎖的操作都受到這個超時參數的影響,不單單是DDL語句,包含在表上的DML、DDL操作,以及視圖、存儲過程、存儲函數、lock table,flush table with read lock語句等。但不適用於隱式訪問系統表的語句,如:grant和revoke等

3、復制類超時

3.1. delayed_insert_timeout
  • 官方描述:
    How many seconds an INSERT DELAYED handler thread should wait for INSERT statements beforeterminating.
  • 為MyISAM INSERT DELAY設計的超時參數,表示INSERT DELAY handler線程在INSERT DELAY語句終止前等待這個INSERT語句的時間,注意是表示insert delay延遲插入的超時時間,不是insert語句。默認值是300S,從5.6.7開始被棄用(因為delayed insert功能被棄用)后續版本將移除。
3.2. rpl_semi_sync_master_timeout
  • 官方描述:
    A value in milliseconds that controls how long the master waits on a commit for acknowledgment from a slave before timing out and reverting to asynchronous replication. The default value is 10000 (10 seconds).

This variable is available only if the master-side semisynchronous replication plugin is installed.

  • 為semi-sync復制時,主庫在某次事務提交時,如果等待超過rpl_semi_sync_master_timeout多秒之后仍然沒有接收到任何從庫做回包響應,那么主庫自動降級為異步復制模式,當主庫探測到有備庫恢復回包時,主庫自動恢復到semi-sync復制模式。默認值為10000毫秒=10秒
3.3. rpl_stop_slave_timeout
  • 官方描述:
    In MySQL 5.6.13 and later, you can control the length of time (in seconds) that STOP SLAVE waits before timing out by setting this variable. This can be used to avoid deadlocks between STOP SLAVE and other slave SQL statements using different client connections to the slave. The maximum and default value of rpl_stop_slave_timeout is 31536000 seconds (1 year). The minimum is 2 seconds.
  • 5.6.13之后引入的參數,控制stop slave 的執行時間,在重放一個大的事務的時候,突然執行stop slave,命令 stop slave會執行很久,這個時候可能產生死鎖或阻塞,嚴重影響性能,可以通過rpl_stop_slave_timeout參數控制stop slave 的執行時間。默認值是31536000秒=1年
3.4. slave_net_timeout
  • 官方描述:
    The number of seconds to wait for more data from a master/slave connection before aborting the read.
  • Slave判斷主庫是否掛掉的超時設置,在設定時間內依然沒有獲取到Master的回應就認為Master已經掛掉了,后續根據超時重連參數設置進行重連主庫的操作。默認值:3600S

4、IO類超時

4.1. innodb_flush_log_at_timeout
    • 官方描述:
      Write and flush the logs every N seconds. innodb_flush_log_at_timeout was introduced in MySQL 5.6.6. It allows the timeout period between flushes to be increased in order to reduce flushing and avoid impacting performance of binary log group commit. Prior to MySQL 5.6.6, flushing frequency was once per second. The default setting for innodb_flush_log_at_timeout is also once per second.
    • 5.6.6引入,參數innodb_flush_log_at_trx_commit=1時,此超時參數不起作用,當innodb_flush_log_at_trx_commit=0/2時才起作用。5.6.6之后表示每innodb_flush_log_at_timeout秒一次的頻率刷新redo log(在5.6.6之前是固定每秒一次刷新redo log,5.6.6之后刷新頻率可以通過這個參數設置,當然,這個參數本身默認值也是1S)。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM