MySQL server has gone away的解决方法

用跑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