MySQL Database ('2006, 'MySQL server has gone away') error

Hi PythonAnywhere,

We are having some trouble with the "OperationalError: (2006, 'MySQL server has gone away')" error, even though we have read your help page ( and all functions in starts and ends with respectively "django.db.close_old_connections()" and "connection.close()" in order to close connections. Most of the time we're getting this error when hitting F5 or clicking on a link after a 5 minute break.

Do you have any idea how we can fix this?

Thanks in advance!

Best regards, Karsten

That sounds strange -- if you're using Django, you don't need to close connections manually -- it does that for you. Are the views that are raising that exception taking a particularly long time to process?

Thanks for your fast reply! No, at least not that (> 5 minutes) long :) There are some forums where they suggest to close connections manually, e.g., which is the reason why we added these statements. Do you need some error log or other kind of information, or do you have any idea what to do or try next?

In the server log we found the following errors ...

2019-03-24 15:55:36 Internal Server Error: /rooster/#012Traceback (most recent call last):#012  File "/home/ziekenhuisroosters/.virtualenvs/myenvontwikkel/lib/python3.6/site-packages/django/contrib/sessions/backends/", line 190, in _get_session#012    return self._session_cache#012AttributeError: 'SessionStore' object has no attribute '_session_cache'#012#012During handling of the above exception, another exception occurred:#012#012Traceback (most recent call last):#012  File "/home/ziekenhuisroosters/.virtualenvs/myenvontwikkel/lib/python3.6/site-packages/django/db/backends/", line 85, in _execute#012    return self.cursor.execute(sql, params)#012  File "/home/ziekenhuisroosters/.virtualenvs/myenvontwikkel/lib/python3.6/site-packages/django/db/backends/mysql/", line 71, in execute#012    return self.cursor.execute(query, args)#012  File "/home/ziekenhuisroosters/.virtualenvs/myenvontwikkel/lib/python3.6/site-packages/MySQLdb/", line 198, in execute

... just after the "(2006, 'MySQL server has gone away')" appeared in the browser. Is it possible that the

'SessionStore' object has no attribute '_session_cache'

is causing the 2006 MySQL error?

I can't see any obvious way they might be connected, but from this Stack Overflow post I get the impression that the _session_cache error normally means that you don't have a sessions table in your database. Do you have django.contrib.sessions.middleware.SessionMiddlewarein your MIDDLEWARE_CLASSES setting in If so, have you run python migrate?

There is a django_session table in our database and this is our MIDDLE_WARE setting in


The "_CLASSES" part of "MIDDLEWARE_CLASSES" is missing, is that a problem? :)

Yes, we did a migrate and just a few minutes ago we did it again by executing "python3.6 migrate" in the virtual environment "myenvontwikkel".

no i think the newer versions of django use the variable MIDDLEWARE instead of MIDDLEWARE_CLASSES in now.

just to double check, do a makemigrations and then migrate if you create any new migrations?

Yes, of course...

python3.6 migrate

... resulted in "No changes detected".

Please feel free to check our files and logs if necessary :)

Thanks! I think I've tracked it down. The problem is the CONN_MAX_AGE setting in your DATABASES config. Try setting that to a value lower than 300 -- say, 290. Idle database connections on PythonAnywhere time out after five minutes, and your config is set to only close them on the Django side after an hour.

Thank you so much!

We changed the "conn_max_age" setting in to 290 and it looks like we don't get that error anymore! :)

Excellent, thanks for confirming!

I am getting the same error in my Django application in python shell what to do?

Looks like you need to close the old db connection. Run from django.db import connection; connection.close() in your shell, and then try again to do what you want to do.