gpt4 book ai didi

python - 超时重新连接 MySQL

转载 作者:太空狗 更新时间:2023-10-29 20:15:43 27 4
gpt4 key购买 nike

我有一个 Python 程序,它在后台运行了数周,并且每隔一段时间就进行一次数据库查询。为此,我正在使用 ORM peewee (版本 2.2.1)。我正在使用 MySQL 作为后端。

最近我在访问数据库时遇到了一个反复出现的问题,通常是在运行程序几天之后。 peewee 引发的错误是

peewee.OperationalError: (2006, 'MySQL server has gone away')

回溯在 peewee 中很深。我把它贴在这里,但由于我的 virtualenv 使文件名太长,我正在缩短它们:

  File ".../local/lib/python2.7/site-packages/peewee.py", line 2910, in save
ret_pk = self.insert(**field_dict).execute()
File ".../local/lib/python2.7/site-packages/peewee.py", line 2068, in execute
return self.database.last_insert_id(self._execute(), self.model_class)
File ".../local/lib/python2.7/site-packages/peewee.py", line 1698, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File ".../local/lib/python2.7/site-packages/peewee.py", line 2232, in execute_sql
self.commit()
File ".../local/lib/python2.7/site-packages/peewee.py", line 2104, in __exit__
reraise(new_type, new_type(*exc_value.args), traceback)
File ".../local/lib/python2.7/site-packages/peewee.py", line 2223, in execute_sql
res = cursor.execute(sql, params or ())
File ".../local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File ".../local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
peewee.OperationalError: (2006, 'MySQL server has gone away')

我发现的可能的解决方案尝试:

  • this question ,其中一条评论建议每隔一段时间对 MySQL 服务器执行一次 ping 操作,以使其(连接?)保持事件状态。不过,我不确定如何通过 ORM 进行操作。 (比如说,我应该每小时简单地 SELECT 1 吗?)
  • this github peewee issue ,它是 4 个月前打开的,但提到了同样的错误,不过,它声称它已解决(而且我使用的是较新的版本)。
  • 7 year old issue trac,一个建议是将 MySQL 的超时时间增加 3 天。
  • 在此forum discussion ,建议增加 MySQL 的超时选项,但提供了“使用 MySQL JDBC 连接器的自动重新连接选项”的替代方法。我试图找出 Python 的 MySQLdb 模块是否存在这样的选项,但找不到。
  • 我找到了这个MySQL reference page关于重新连接行为,但我对 MySQL 的理解有点复杂(通常我只使用 ORM),而且我不知道如何从 peewee.

即使我能够 ping 数据库以使连接保持更长的时间,我认为在不需要连接时保持连接被认为是一种不好的做法。有没有办法通过 ORM 重新打开连接?我认为 ping 和增加 MySQL 的超时都是解决方法,而真正的解决方案是在需要时重新连接(真正的解决方案正是我所要求的)。

最佳答案

我遇到了同样的问题,对于使用 MySQLdb 的 peewee,我在初始化 MySQL 数据库实例时得到了以下解决方案:

db = MySQLDatabase(db_name, user=db_username, passwd=db_password, host=db_host, port=db_port)
db.get_conn().ping(True)

ping 函数在哪里:

Checks whether or not the connection to the server is working. If it has gone down, an automatic reconnection is attempted.

This function can be used by clients that remain idle for a long while, to check whether or not the server has closed the connection and reconnect if necessary.

New in 1.2.2: Accepts an optional reconnect parameter. If True, then the client will attempt reconnection. Note that this setting is persistent. By default, this is on in MySQL<5.0.3, and off thereafter.

Non-standard. You should assume that ping() performs an implicit rollback; use only when starting a new transaction. You have been warned.

db.get_conn().ping.__doc__ 中。请注意,如果您再次创建另一个连接,则必须使用 db.get_conn().ping(True)。因此,如果您重新连接(例如通过 db.connect()),您必须重复 ping。

关于python - 超时重新连接 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852760/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com