在用mysql客戶端對數據庫進行操作時,打開終端窗口,如果一段時間沒有操作,再次操作時,常常會報如下錯誤:
| ERROR 2013 (HY000): Lost connection to MySQL server during queryERROR 2006 (HY000): MySQL server has gone awayNo connection. Trying to reconnect... |
這個報錯信息就意味著當前的連接已經斷開,需要重新建立連接。
那么,連接的時長是如何確認的?
其實,這個與interactive_timeout和wait_timeout的設置有關。
首先,看看官方文檔對于這兩個參數的定義
interactive_timeout
默認是28800,單位秒,即8個小時
| 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(). See also wait_timeout. |
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參數
| mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout');+---------------------+----------------+| variable_name | variable_value |+---------------------+----------------+| INTERACTIVE_TIMEOUT | 28800 || WAIT_TIMEOUT | 28800 |+---------------------+----------------+2 rows in set (0.03 sec)mysql> set session WAIT_TIMEOUT=10;Query OK, 0 rows affected (0.00 sec)-------等待10s后再執行mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout');ERROR 2013 (HY000): Lost connection to MySQL server during query |
可以看到,等待10s后再執行操作,連接已經斷開。
新聞熱點
疑難解答