纠结的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小时链接一次
参考地址: