1. 程式人生 > >MySQL和連線相關的timeout 整理

MySQL和連線相關的timeout 整理

slave-net-timeout

The number of seconds to wait for more data from the master before the slave considers the connection broken, aborts the read, and tries to reconnect. The first retry occurs immediately after the timeout. The interval between retries is controlled by the MASTER_CONNECT_RETRY option for the CHANGE MASTER TO statement, and the number of reconnection attempts is limited by the --master-retry-count option. The default is 3600 seconds (one hour).
當slave認為連線master的連線有問題時,就等待N秒,然後斷開連線,重新連線master

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
在終止讀之前,從一個連接獲得資料而等待的時間秒數;當服務正在從客戶端讀取資料時,net_read_timeout控制何時超時。 

net_write_timeout:

The number of seconds to wait for a block to be written to a connection before aborting the write.When the server is writing to the client, net_write_timeout is the timeout value controlling when to abort。
在終止寫之前,等待多少秒把block寫到連線;當服務正在寫資料到客戶端時,net_write_timeout控制何時超時

wait_timeout

The number of seconds the server waits for activity on a noninteractive connection before closing it.
與伺服器端無互動狀態的連線,直到被伺服器端強制關閉而等待的時間

interactive_timeout :

The number of seconds the server waits for activity on an interactive connection before closing it.
與伺服器端無互動狀態的連線,直到被伺服器端強制關閉而等待的時間

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.

等待一個連線響應的時間 

connect_timeout:在獲取連線階段(authenticate)起作用

interactive_timeout和wait_timeout:在連線空閒階段(sleep)起作用
net_read_timeout和net_write_timeout:則是在連線繁忙階段(query)起作用。 

獲取MySQL連線是多次握手的結果,除了使用者名稱和密碼的匹配校驗外,還有IP->HOST->DNS->IP驗證,任何一步都可能因為網路問題導致執行緒阻塞。為了防止執行緒浪費在不必要的校驗等待上,超過connect_timeout的連線請求將會被拒絕。 

即使沒有網路問題,也不能允許客戶端一直佔用連線。對於保持sleep狀態超過了wait_timeout(或interactive_timeout,取決於client_interactive標誌)的客戶端,MySQL會主動斷開連線。

即使連線沒有處於sleep狀態,即客戶端忙於計算或者儲存資料,MySQL也選擇了有條件的等待。在資料包的分發過程中,客戶端可能來不及響應(傳送、接收、或者處理資料包太慢)。為了保證連線不被浪費在無盡的等待中,MySQL也會選擇有條件(net_read_timeout和net_write_timeout)地主動斷開連線。