January 1, 2016

124 words 1 min read

MySQL server has gone away on Django

Often I need to generate reports of some sort from a “Django made” system, most of the time it’s easier to just open up a django shell and interact directly with the records from the database, however when my fingers are too slow and there are special configurations on the database server, I receive the following error:

OperationalError: (2006, ‘MySQL server has gone away’)

This happens because the connection to the database was idle for too long, the fix couldn’t be easier, just close the current connection forcing django to open up a new connection next time it sends a query:

from django.db import connection

Here is the one-liner for the copy & paste fix:

from django.db import connection as c; c.close()