2

I sow post on

How to enable MySQL client auto re-connect with MySQLdb?

I had the same problem, I use pythonanywhere.com and when I write p.save() I had error like below.

It says something about updating mysql to fix it, but I don't know what code or shell commands I need to write.

Can you help me?

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/daro822/.virtualenvs/django16/local/lib/python2.7/site-packages/django/db/models/base.py", line 545, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/daro822/.virtualenvs/django16/local/lib/python2.7/site-packages/django/db/models/base.py", line 570, in save_base
    with transaction.commit_on_success_unless_managed(using=using, savepoint=False):
  File "/home/daro822/.virtualenvs/django16/local/lib/python2.7/site-packages/django/db/transaction.py", line 280, in __enter__
    connection.set_autocommit(False)
  File "/home/daro822/.virtualenvs/django16/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 340, in set_autocommit
    self._set_autocommit(autocommit)
  File "/home/daro822/.virtualenvs/django16/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 461, in _set_autocommit
    self.connection.autocommit(autocommit)
  File "/home/daro822/.virtualenvs/django16/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/daro822/.virtualenvs/django16/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 461, in _set_autocommit
    self.connection.autocommit(autocommit)
  File "/home/daro822/.virtualenvs/django16/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 243, in autocommit
    _mysql.connection.autocommit(self, on)
OperationalError: (2006, 'MySQL server has gone away')
Community
  • 1
  • 1
darek_82
  • 361
  • 1
  • 3
  • 13

2 Answers2

8

Your connection to the database was closed because it was idle for too long. From the traceback, it looks like you were doing your queries interactively. If you know that you've been idle for more than about 5 min, you should do this:

from django.db import connection; connection.close()
Glenn
  • 7,262
  • 1
  • 17
  • 23
  • 1
    This was the most straightforward answer on the topic (including other questions), but I would note that Django disconnects with this same error similar traceback when doing a very large `delete()`. – WAF Jan 06 '15 at 12:26
2

I solved the problem inserting this code where the connection will need to be restored and refreshed:

        try:
            cursor = connections['default'].cursor()
            db = cursor.db
            assert issubclass(db.__class__, BaseDatabaseWrapper)
            if db.connection is None or not db.is_usable():
                db.close_if_unusable_or_obsolete()
                with db.wrap_database_errors:
                    db.connect()
                logger.info('Restoring the Mysql Connection')
        except Exception as e:
            logger.exception('DB Connection error')

Please notice that I use connections['default'] because I have multiple databases configured so you can set a specific connection, notice also that I use db.is_usable() so to call the .ping() to allow to restore a connection when is possible instead of always closing the connection.

PaoloC
  • 3,451
  • 2
  • 15
  • 18