MySQL timeout相關引數解析和測試 • cenalulu's Tech Blog
MySQL中有兩個關於連線超時的配置項:
wait_timeout
和interactive_timeout
。他們之間在某些條件下會互相繼承,那究竟這兩個引數會在什麼情況下起作用呢? 本文將會通過一些測試例項來證明總結兩者的相互關係。
引數介紹
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
mysql_real_connect()
. See alsowait_timeout.
wait_timeout
The number of seconds the server waits for activity on a noninteractive connection before closing it. Before MySQL 5.1.41, this timeout applies only to TCP/IP connections, not to connections made through Unix socket files, named pipes, or shared memory.
On thread startup, the session wait_timeout
wait_timeout
value or from the global interactive_timeout
value, depending on the type of client (as defined by the CLIENT_INTERACTIVE
connect option to mysql_real_connect()
). See also interactive_timeout.
CLIENT_INTERACTIVE
Permit interactive_timeout
seconds (instead of wait_timeout
wait_timeout
variable is set to the value of the session interactive_timeout
variable.
簡單的說 interactive就是互動式的終端,例如在shell裡面直接執行mysql,出現形如mysql>
的提示符後就是互動式的連線。而mysql -e ‘select 1’ 這樣的直接返回結果的方式就是非互動式的連線。
測試及驗證
繼承關係
Q:通過socket連線 timeout會從哪個global timeout繼承 A:由下例可見,通過socket登入,timeout 繼承於global.interactive_timeout;
Q:通過TCP/IP client 連線, timeout會從哪個global timeout繼承
A:由下例可見,通過TCP/IP client 連線後的wait_timeout
仍然繼承於 global.interactive_timeout
起效關係
Q:timeout值,對於處於執行狀態SQL語句是否起效(即是否等價於執行超時)? A:由下例可見SQL正在執行狀態的等待時間不計入timeout時間。即SQL執行再久也不會因為timeout的配置而中斷
Q:同一個session中,wait_timeout
和 interacitve_timeout
是否都會生效。
A:只有wait_timeout
會真正起到超時限制的作用
Q:global timeout和session timeout是否都會作為超時判斷依據? A:只有session級別 timeout 會起作用。即一個session開始後,無論如何修改global級別的timeout都不會影響該session
- 測試1:
- 測試2:
總結
由以上的階段測試可以獲得以下結論。
- 超時時間只對非活動狀態的connection進行計算。
- 超時時間只以session級別的
wait_timeout
為超時依據,global級別只決定session初始化時的超時預設值。 - 互動式連線的
wait_timeout
繼承於global的interactive_timeout
。非互動式連線的wait_timeout
繼承於global的wait_timeout
- 繼承關係和超時對 TCP/IP 和 Socket 連線均有效果