【问题标题】:How to fix MySQL Server has gone away error in Python如何修复 Python 中的 MySQL Server has gone away 错误
【发布时间】:2019-07-05 14:05:33
【问题描述】:

我正在运行实时检测 python 脚本。所以 python 脚本 24/7 运行而不关闭,但几个小时后,我得到 MySQL has hone away 错误

这是我建立连接的代码

def connectToMySQL(config_str):
    return mysql.connector.connect(
        host=config_str[1].strip('\n').strip('\r\n'),
        user=config_str[2].strip('\n').strip('\r\n'),
        passwd=config_str[3].strip('\n').strip('\r\n'),
        database=config_str[4].strip('\n').strip('\r\n')
    )
mydb = connectToMySQL(config_str)

mycursor = mydb.cursor()

如何将超时限制设置为无穷大?

【问题讨论】:

标签: python mysql mysql-connector mysql-connector-python


【解决方案1】:

我在多线程环境中使用连接时遇到了这个问题。我通过从每个线程再次建立连接(并在其中关闭它)来修复它。

【讨论】:

  • 也为我工作
【解决方案2】:

在 MySQL 中执行以下查询。

SET @@GLOBAL.wait_timeout=31536000;
SET @@GLOBAL.interactive_timeout=31536000;

(对于 windows 使用 2147483 值)

重启 MySQL 服务器。

另外,请务必在使用后关闭游标和连接。

mycursor.close()
mydb.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-04
    • 2014-08-09
    • 2019-10-13
    • 2012-12-19
    • 2016-01-19
    • 2013-12-16
    • 2011-12-11
    相关资源
    最近更新 更多