Search code examples
mysqldjangotimeout

Django, after upgrade: MySQL server has gone away


I recently upgraded from Django 1.4 to Django 1.7 and since I keep getting the following error message for some scripts, sometimes:

OperationalError: (2006, 'MySQL server has gone away')

The scripts are very long or continuously running tasks that might involve phases of not communicating with the db for several minutes, so the connection times out. However, before I upgraded, that was no problem, as Django seemed to automatically re-establish a connection. Now it doesn't which means the tasks often stop and fail in the middle.

Does anyone know what has changed and how I can fix it?

Is it perhaps related to that ticket/fix: https://code.djangoproject.com/ticket/21463

Thanks a lot!


Solution

  • The reason of such behavior is persistent connect to database, which was introduced in Django 1.6.

    To prevent connection timeout error you should set CONN_MAX_AGE in settings.py to value which is less than wait_timeout in MySQL config (my.cnf). In that case Django detects that connection need to be reopen earlier than MySQL throws it. Default value for MySQL 5.7 is 28800 seconds.

    settings.py:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'CONN_MAX_AGE': 3600,
            <other params here>
        }
    }
    

    Documentation: https://docs.djangoproject.com/en/1.7/ref/settings/#conn-max-age

    my.cnf:

    wait_timeout = 28800
    

    Documentation: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_wait_timeout