问题
I'm using Flask-SQLAlchemy in my app and after 60 seconds of idle time, MySQL returns a "MySQL server has gone away" error.
Here's my traceback:
Traceback (most recent call last):
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app response = self.full_dispatch_request()
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request rv = self.handle_user_exception(e)
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/flask/app.py", line 1356, in full_dispatch_request rv = self.preprocess_request()
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/flask/app.py", line 1539, in preprocess_request rv = func()
File "./project/controllers/__init__.py", line 18, in set_domain_and_config \ current_site = Site.query.filter(Site.url == host).first_or_404()
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/flask_sqlalchemy.py", line 367, in first_or_404 rv = self.first()
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2181, in first ret = list(self[0:1])
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2048, in __getitem__ return list(res)
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2252, in __iter__ return self._execute_and_instances(context)
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2267, in _execute_and_instances result = conn.execute(querycontext.statement,\ self._params)
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 664, in execute params)
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 764, in _execute_clauseelement compiled_sql, distilled_params
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 878, in _execute_context context)
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 871, in _execute_context context)
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 320, in do_execute cursor.execute(statement, parameters)
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/newrelic-1.10.0.28/newrelic/hooks/database_dbapi2.py", line 36, in execute return self._nr_cursor.execute(sql, *args, **kwargs)
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 201, in execute self.errorhandler(self, exc, value)
File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue
OperationalError: (OperationalError) (2006, 'MySQL server has gone away')
回答1:
60 seconds seem a low number, but you can set SQLALCHEMY_POOL_RECYCLE in your application config to be <60 seconds if your server is more aggressively killing idle connections.
来源:https://stackoverflow.com/questions/14936319/flask-app-returning-mysql-server-has-gone-away-after-60-seconds-of-idle-time