Forums

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 (https://help.pythonanywhere.com/pages/ManagingDatabaseConnections) and all functions in views.py 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. https://stackoverflow.com/questions/26958592/django-after-upgrade-mysql-server-has-gone-away, 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/base.py", 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/utils.py", 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/base.py", line 71, in execute#012    return self.cursor.execute(query, args)#012  File "/home/ziekenhuisroosters/.virtualenvs/myenvontwikkel/lib/python3.6/site-packages/MySQLdb/cursors.py", 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 settings.py? If so, have you run python manage.py migrate?

There is a django_session table in our database and this is our MIDDLE_WARE setting in settings.py:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

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 manage.py migrate" in the virtual environment "myenvontwikkel".

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

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

Yes, of course...

python3.6 manage.py 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 settings.py 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 manage.py 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.