简体   繁体   English

sqlalchemy + MySQL连接超时

[英]sqlalchemy + MySQL connection timeouts

I have a daemon that uses sqlalchemy to interact with MySQL database. 我有一个使用sqlalchemy与MySQL数据库交互的守护进程。 Since interaction is seldom, the connections are prone to timing out. 由于交互很少,因此连接很容易超时。 I've tried to fix the problem by setting various flags when creating the database engine, eg pool_recycle=3600 , but nothing seems to help. 我试图通过在创建数据库引擎时设置各种标志来解决问题,例如pool_recycle=3600 ,但似乎没有任何帮助。

To help me debug the problem, I set the timeout of my local mysql server to 10 seconds, and tried the following program. 为了帮助我调试问题,我将本地mysql服务器的超时设置为10秒,并尝试了以下程序。

import time
import sqlalchemy

engine = sqlalchemy.engine.create_engine("mysql://localhost")

while True:
    connection = engine.connect()
    result = connection.execute("SELECT 1")
    print result.fetchone()
    connection.close()
    time.sleep(15)

Surprisingly, I continue to get exceptions like the following: 令人惊讶的是,我继续得到如下例外情况:

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

However, if I remove the call to connection.close() , the problem goes away. 但是,如果我删除connection.close()的调用,问题就会消失。 What is going on here? 这里发生了什么? Why isn't sqlalchemy trying to establish a new connection each time I call connect() ? 为什么sqlalchemy每次调用connect()时都不会尝试建立新连接?

I'm using Python 2.7.3 with sqlalchemy 0.9.8 and MySQL 5.5.40. 我正在使用Python 2.7.3与sqlalchemy 0.9.8和MySQL 5.5.40。

the document mention: 文件提到:

MySQL features an automatic connection close behavior, MySQL具有自动连接关闭行为,
for connections that have been idle for eight hours or more. 对于闲置8小时或更长时间的连接。 To circumvent having this issue, use the pool_recycle option which controls the maximum age of any connection: 要避免出现此问题,请使用pool_recycle选项控制任何连接的最大年龄:

engine = create_engine('mysql+mysqldb://...', pool_recycle=3600) engine = create_engine('mysql + mysqldb:// ...',pool_recycle = 3600)

You can just put "pool_recycle" parameter when the create_engine is invoked. 您可以在调用create_engine时放入“pool_recycle”参数。

I'm not 100% sure if this is going to fix your problem or not, but I ran into a similar issue when dealing with mysql. 我不是100%确定这是否能解决您的问题,但在处理mysql时我遇到了类似的问题。 It was only fixed when I used pymysql to connect to the database. 只有在我使用pymysql连接数据库时才会修复它。

You can install like this: 你可以像这样安装:

pip install pymysql

Which will work for both linux and windows (if you have it installed) 哪个适用于linux和windows(如果安装了它)

Then give your connection string like this: 然后给出你的连接字符串:

import time
import sqlalchemy

engine = sqlalchemy.engine.create_engine("mysql+pymysql://localhost")

while True:
    connection = engine.connect()
    result = connection.execute("SELECT 1")
    print result.fetchone()
    connection.close()
    time.sleep(15)

I get the following output when I run it: 我运行时得到以下输出:

(1,)
(1,)
(1,)
(1,)

On another note, I have found certain queries to break with SQLAlchemy 0.9.8. 另一方面,我发现某些查询打破了SQLAlchemy 0.9.8。 I had to install version 0.9.3 in order for my applications to not break anymore. 我不得不安装0.9.3版本,以便我的应用程序不再破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM