SQLAlchemy错误MySQL服务器已经消失 [英] SQLAlchemy error MySQL server has gone away
问题描述
错误 OperationalError :( OperationalError)(2006,'MySQL server has gone away')
我已经收到这个错误,当我在Flask上编码的项目,但我不能明白为什么我得到这个错误。
<$>我有代码(是的,如果代码小,执行速度快, p $ p $ db_engine = create_engine('mysql://root@127.0.0.1/mind?charset = utf8',pool_size = 10,pool_recycle = 7200)
Base.metadata.create_all db_engine)
Session = sessionmaker(bind = db_engine,autoflush = True)
Session = scoped_session(Session)
session = Session()
#有许多类和函数
session.close()
返回错误'MySQL服务器已经消失'
,但在一段时间后,当我在脚本中使用暂停时返回它。
使用openserver.ru(它是像wamp这样的web服务器)的Mysql。
谢谢..
SQLAlchemy现在对如何使用ping命令对连接的新鲜性表示悲观:
http ://docs.sqlalchemy.org/en/latest/core/pooling.html#disconnect-handling-pessimistic
从那里开始,
from sqlalchemy import exc
from sqlalchemy导入事件$ b $ from sqlalchemy.pool导入池
())
def ping_connection(dbapi_connection,connection_record,connection_proxy):
cursor = dbapi_connection.cursor()
try:
cursor.execute() SELECT 1)
除外:
#可选 - 处理整个池
#而不是无效e一次
#connection_proxy._pool.dispose()
#raise DisconnectionError - 池会尝试
#再次连接三次,然后再升起。
raise exc.DisconnectionError()
cursor.close()
测试,以确保上述工作:
$ pre $从$ sq $' tiger @ localhost / test,echo_pool = True)
c1 = e.connect()
c2 = e.connect()
c3 = e.connect()
c1。 close()
c2.close()
c3.close()
#池大小现在是三。
打印重新启动服务器
raw_input()
在xrange(10)中:
c = e.connect()
打印c.execute(select 1)。fetchall()
c.close()
Error OperationalError: (OperationalError) (2006, 'MySQL server has gone away')
i'm already received this error when i coded project on Flask, but i cant understand why i get this error.
I have code (yeah, if code small and executing fast, then no errors) like this \
db_engine = create_engine('mysql://root@127.0.0.1/mind?charset=utf8', pool_size=10, pool_recycle=7200)
Base.metadata.create_all(db_engine)
Session = sessionmaker(bind=db_engine, autoflush=True)
Session = scoped_session(Session)
session = Session()
# there many classes and functions
session.close()
And this code returns me error 'MySQL server has gone away'
, but return it after some time, when i use pauses in my script.
Mysql i use from openserver.ru (it's web server like such as wamp).
Thanks..
SQLAlchemy now has a great write-up on how you can use pinging to be pessimistic about your connection's freshness:
http://docs.sqlalchemy.org/en/latest/core/pooling.html#disconnect-handling-pessimistic
From there,
from sqlalchemy import exc
from sqlalchemy import event
from sqlalchemy.pool import Pool
@event.listens_for(Pool, "checkout")
def ping_connection(dbapi_connection, connection_record, connection_proxy):
cursor = dbapi_connection.cursor()
try:
cursor.execute("SELECT 1")
except:
# optional - dispose the whole pool
# instead of invalidating one at a time
# connection_proxy._pool.dispose()
# raise DisconnectionError - pool will try
# connecting again up to three times before raising.
raise exc.DisconnectionError()
cursor.close()
And a test to make sure the above works:
from sqlalchemy import create_engine
e = create_engine("mysql://scott:tiger@localhost/test", echo_pool=True)
c1 = e.connect()
c2 = e.connect()
c3 = e.connect()
c1.close()
c2.close()
c3.close()
# pool size is now three.
print "Restart the server"
raw_input()
for i in xrange(10):
c = e.connect()
print c.execute("select 1").fetchall()
c.close()
这篇关于SQLAlchemy错误MySQL服务器已经消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!