Forums

Lost connection to MySQL server during query

Often got this error message and I'm wondering why ...

... 
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (OperationalError) (2013, 'Lost connection to MySQL server during query') ...

It could be a few different things. If your query takes a long time, then it might get disconnected -- I think we limit queries (and connections) to a 5-minute timeout. Alternatively, it could be Amazon's DNS service being flaky. We suffer from that to, and we've got an outstanding support request with them...

I get this once a day on average, using Python 3.3 + SqlAlchemy 0.9.8 + PyMySQL 0.6.2.None.

It's not normally in a long query at the time, seems more random.

Jim

Then I'm going to have to blame amazon's dns service I'm afraid :-/ sorry there's not much else I can say...

Thanks Harry.

Anyone else get this? I'm using a long-running app in a Bash shell. Sometimes fails after a couple of hours, other times a day or three.

Could you build in a retry loop, with a try/except?

One other possibility is that the connection is timing out after a long period of inactivity. That said, SQLAlchemy should be doing connection-pooling and handling those kinds of disconnects on your behalf. But could that be it?

yes, that is it.

I often have this error in my site sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (2013, 'Lost connection to MySQL server during query') [SQL: SELECT count(*) AS count_1 FROM (SELECT ROLE.id AS ROLE_id, ROLE.rol AS ROLE_rol FROM ROLE) AS anon_1]

But as you can see it is a simple query to check if the user has an specific role. It can damage the user experience.

what web framework are you running, and what are your database connection pooling settings?