Hello all,
I am getting the following error, relatively frequently, even when there is no particular heavy load on the MySQL database (ie: simply loading the home page):
Exception on /index [GET]
Traceback (most recent call last):
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
self.dialect.do_execute(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 747, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.10/site-packages/MySQLdb/cursors.py", line 206, in execute
res = self._query(query)
File "/usr/local/lib/python3.10/site-packages/MySQLdb/cursors.py", line 319, in _query
db.query(q)
File "/usr/local/lib/python3.10/site-packages/MySQLdb/connections.py", line 254, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/eeb2/.local/lib/python3.10/site-packages/flask/app.py", line 2528, in wsgi_app
response = self.full_dispatch_request()
File "/home/eeb2/.local/lib/python3.10/site-packages/flask/app.py", line 1825, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/eeb2/.local/lib/python3.10/site-packages/flask/app.py", line 1823, in full_dispatch_request
rv = self.dispatch_request()
File "/home/eeb2/.local/lib/python3.10/site-packages/flask/app.py", line 1799, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/home/eeb2/eeb2/app/routes.py", line 26, in index
guestcount = Guest.query.count()
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 3140, in count
self._legacy_from_self(col).enable_eagerloads(False).scalar()
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 2833, in scalar
ret = self.one()
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 2806, in one
return self._iter().one() # type: ignore
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 2855, in _iter
result: Union[ScalarResult[_T], Result[_T]] = self.session.execute(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 2229, in execute
return self._execute_internal(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 2124, in _execute_internal
result: Result[Any] = compile_state_cls.orm_execute_statement(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/orm/context.py", line 253, in orm_execute_statement
result = conn.execute(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1414, in execute
return meth(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", line 485, in _execute_on_connection
return connection._execute_clauseelement(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1638, in _execute_clauseelement
ret = self._execute_context(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1842, in _execute_context
return self._exec_single_context(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1983, in _exec_single_context
self._handle_dbapi_exception(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2325, in _handle_dbapi_exception
raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
self.dialect.do_execute(
File "/home/eeb2/.local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 747, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.10/site-packages/MySQLdb/cursors.py", line 206, in execute
res = self._query(query)
File "/usr/local/lib/python3.10/site-packages/MySQLdb/cursors.py", line 319, in _query
db.query(q)
File "/usr/local/lib/python3.10/site-packages/MySQLdb/connections.py", line 254, in query
_mysql.connection.query(self, query)
sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (2013, 'Lost connection to MySQL server during query')
[SQL: SELECT count(*) AS count_1
FROM (SELECT guest.id AS guest_id, guest.name AS guest_name, guest.section AS guest_section, guest.email AS guest_email, guest.registered AS guest_registered, guest.rsvp AS guest_rsvp FROM guest) AS anon_1] (Background on this error at: https://sqlalche.me/e/20/e3q8)
I looked at the different topics here and on the broader web and have ameneded a few of the environment variables as follow, but no luck:
SQLALCHEMY_POOL_RECYCLE = 600
SQLALCHEMY_POOL_TIMEOUT = 600
SQLALCHEMY_TRACK_MODIFICATIONS = False
connect_timeout = 600
net_read_timeout = 600
wait_timeout = 28800
interactive_timeout = 28800
Looking at the link provided in the error message, I see this:
OperationalError
Exception raised for errors that are related to the database’s operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name is not found, a transaction could not be processed, a memory allocation error occurred during processing, etc.
This error is a DBAPI Error and originates from the database driver (DBAPI), not SQLAlchemy itself.
The OperationalError is the most common (but not the only) error class used by drivers in the context of the database connection being dropped, or not being able to connect to the database. For tips on how to deal with this, see the section Dealing with Disconnects.
Any ideas on how to stop getting these? I am nearly ready to go to production. :)