mysql的wait_timeout和interactive_timeout

hotdog04發表於2015-06-30

###wait_timeout interactive_timeout####

wait_timeout :對於非互動連線,mysql在關閉它之前的等待時間
              這個值的初始化根據連線客戶端的連線方式不同:有兩種方式:全域性的wait_timeout或者是全域性的interactive_timeout

interactive_timeout:對於互動連線,mysql在關閉它之前的等待時間

測試:
root:3406:(none)>show variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| have_statement_timeout      | YES      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 21       |
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 60       |
| thread_pool_idle_timeout    | 60       |
| wait_timeout                | 21       |
+-----------------------------+----------+
14 rows in set (0.00 sec)

root:3406:(none)>show global variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| have_statement_timeout      | YES      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 21       |
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 60       |
| thread_pool_idle_timeout    | 60       |
| wait_timeout                | 14       |
+-----------------------------+----------+
14 rows in set (0.00 sec)

root:3406:(none)>show global variables like '%timeout%';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    4
Current database: *** NONE ***

+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| have_statement_timeout      | YES      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 21       |
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 60       |
| thread_pool_idle_timeout    | 60       |
| wait_timeout                | 14       |
+-----------------------------+----------+
14 rows in set (0.00 sec)

上面是測試互動輸入的方式,
wait_timeout =14
interactive_timeout=21
可以看到session級別的wait_timeout的初始化使用了interactive_timeout
當超過了21s後,再執行命令連線已經被幹掉了


綜上:
針對client其實真正生效的是session級別的wait_timeout,空閒連線(互動和非互動),
超過其session級別的wait_timeout時間就會被回收掉:
虛擬碼:
if (client_connection is interactive) then
  session  wait_timeout= global interactive_timeout
else if(client_connection is noninteractive ) then
   session  wait_timeout= global wait_timeout
end if

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/20625855/viewspace-1717890/,如需轉載,請註明出處,否則將追究法律責任。

相關文章