用跑python 寫了一個鏈接mysql數據庫的類,發現過了一天鏈接就斷掉了,原來mysql有一個超時時間的設置
查看超時設置:
show global variables like '%timeout%';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
+
----------------------------+-------+
| Variable_name | Value |
+
----------------------------+-------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout |
OFF
|
| interactive_timeout | 28800 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| table_lock_wait_timeout | 50 |
| wait_timeout | 28800 |
+
----------------------------+-------+
rows
in
set
(0.00 sec)
|
其中的 interactive_timeout 就是設置的鏈接超時時間,我們把它改大一點,比如改為15天:
set global interactive_timeout=1296000;
這樣就可以了
-----------------------------------------------------------------------------------------------------------------
如果以上方法不起作用,可以寫一個線程循環去訪問:
例如:
def keep_mysql(): while True: sql_utils.sql_utils.select_case("tablename") # 執行select語句,根據自己的代碼更改 #print("keep live---------------") time.sleep(7200) if __name__ == "__main__": import threading threading.Thread(target=keep_mysql).start()
這樣每隔2個小時會執行select語句保證mysql鏈接gone away(間隔時間自己調整)
原文鏈接: https://www.cnblogs.com/gaigaige/p/11088310.html