当前位置: 代码迷 >> Rational >> 扭结的SqlAlchemy: SQLError: (OperationalError) (2006, ‘MySQL server has gone away’)
  详细解决方案

扭结的SqlAlchemy: SQLError: (OperationalError) (2006, ‘MySQL server has gone away’)

热度:9213   发布时间:2013-02-26 00:00:00.0
纠结的SqlAlchemy: SQLError: (OperationalError) (2006, ‘MySQL server has gone away’)

纠结的SqlAlchemy: SQLError: (OperationalError) (2006, ‘MySQL server has gone away’)问题处理!

?

发现问题,应用启动后,第二天来访问就出现该问题。

?

在网上go了一下,发现是mysql长连接超时。

?

1. 查Mysql超时情况。

show variables like '%timeout%'

?

wait_timeout = 28800

?

mysql默认等待超时是8个小时

?

过了1晚上肯定有问题。

?

2. 查看sqlalchemy链接问题

?

create_engine(url,pool_size=20, max_overflow=-1, pool_recycle=true,echo=True)?

?

查了源码:

?

:param pool_recycle=-1: this setting causes the pool to recycle

? ? ? ? connections after the given number of seconds has passed. It

? ? ? ? defaults to -1, or no timeout. For example, setting to 3600

? ? ? ? means connections will be recycled after one hour. Note that

? ? ? ? MySQL in particular will disconnect automatically if no

? ? ? ? activity is detected on a connection for eight hours (although

? ? ? ? this is configurable with the MySQLDB connection itself and the

? ? ? ? server configuration as well).

?

?

pool_recycle is an integer number of seconds which to wait before?

? ?reopening a conneciton. ?However, setting it to "True" is equivalent to?
? ? ? ? ? 1 which means it will reopen connections constantly.?

?

? ? ? ??pool_recycle=true标识1秒重新链接一下

?

3. 修改后pool_recycle=7200

2小时链接一次

?

?

参考地址:

http://groups.google.com/group/sqlalchemy/browse_thread/thread/9412808e695168ea/c31f5c967c135be0?pli=1

? ? ? ??http://dba007.blog.51cto.com/2876338/788315

? ? ? ??http://hi.baidu.com/limodou/item/2d00f3333cc3b884c3cf29f7

  相关解决方案