在用mysql客戶端對數據庫進行操做時,打開終端窗口,若是一段時間沒有操做,再次操做時,經常會報以下錯誤:java
?mysql
1sql 2數據庫 3服務器 |
|
這個報錯信息就意味着當前的鏈接已經斷開,須要從新創建鏈接。測試
那麼,鏈接的時長是如何確認的?網站
其實,這個與interactive_timeout和wait_timeout的設置有關。
首先,看看官方文檔對於這兩個參數的定義
interactive_timeout
默認是28800,單位秒,即8個小時
1 |
|
wait_timeout
默認一樣是28800s
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_timeout value, depending on the type of client (as defined by the CLIENT_INTERACTIVE connect option to mysql_real_connect()). See also interactive_timeout.
根據上述定義,二者的區別顯而易見
1> interactive_timeout針對交互式鏈接,wait_timeout針對非交互式鏈接。所謂的交互式鏈接,即在mysql_real_connect()函數中使用了CLIENT_INTERACTIVE選項。
說得直白一點,經過mysql客戶端鏈接數據庫是交互式鏈接,經過jdbc鏈接數據庫是非交互式鏈接。
2> 在鏈接啓動的時候,根據鏈接的類型,來確認會話變量wait_timeout的值是繼承於全局變量wait_timeout,仍是interactive_timeout。
下面來測試一下,確認以下問題
1. 控制鏈接最大空閒時長的是哪一個參數。
2. 會話變量wait_timeout的繼承問題
Q1:控制鏈接最大空閒時長的是哪一個參數
A1:wait_timeout
驗證
只修改wait_timeout參數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
能夠看到,等待10s後再執行操做,鏈接已經斷開。
只修改interactive_timeout參數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Q2:會話變量wait_timeout的繼承問題
A2:若是是交互式鏈接,則繼承全局變量interactive_timeout的值,若是是非交互式鏈接,則繼承全局變量wait_timeout的值。
驗證:
只修改全局變量interactive_timeout的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
開啓另一個mysql客戶端,查看會話變量的值
1 2 3 4 5 6 7 8 |
|
發現,WAIT_TIMEOUT的值已經變爲10了。
但經過jdbc測試,wait_timeout的值依舊是28800
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
結果輸出以下:
INTERACTIVE_TIMEOUT: 10
WAIT_TIMEOUT: 28800
只修改全局變量wait_timeout的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
開啓另一個mysql客戶端,查看會話變量的值
1 2 3 4 5 6 7 8 |
|
WAIT_TIMEOUT的值依舊是28800.
查看jdbc的結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
查看jdbc的結果
1 2 |
|
同時,新增了一段程序,等待20s後,再次執行查詢,報以下錯誤:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
總結
1. 控制鏈接最大空閒時長的wait_timeout參數。
2. 對於非交互式鏈接,相似於jdbc鏈接,wait_timeout的值繼承自服務器端全局變量wait_timeout。
對於交互式鏈接,相似於mysql客戶單鏈接,wait_timeout的值繼承自服務器端全局變量interactive_timeout。
3. 判斷一個鏈接的空閒時間,可經過show processlist輸出中Sleep狀態的時間
1 2 3 4 5 6 7 8 |
|
以上所述是小編給你們介紹的MySQL中interactive_timeout和wait_timeout的區別,但願對你們有所幫助,若是你們有任何疑問請給我留言,小編會及時回覆你們的。在此也很是感謝你們對腳本之家網站的支持!