python - Django 1.7 Multiple Databases - OperationalError: (2006, 'MySQL server has gone away') Reset Connection -
i getting errors when trying reset connection remote database. not default database. seems connections.close has affect on default database configuration.
am missing or there way reset connection specific database (not default)?
django 1.7 python 2.7.9
databases = { 'default': { 'engine': 'django.db.backends.mysql', 'name': 'defaultdb', 'user': 'xxxxx', 'password': 'xxxxxx', 'host': '', 'port': '', }, 'bass': { 'engine': 'django.db.backends.mysql', 'name': 'thedb', 'user': 'xxxx', 'password': 'xxxxx', 'host': '10.x.x.x', 'port': 'xxxx', } }
remotedb_models.py:
class remotedbrouters(models.model): hostname = models.charfield(max_length=63) role = models.charfield(max_length=20, blank=true) infodate = models.datefield() infotime = models.timefield() serialnum = models.charfield(max_length=20) iostype = models.charfield(max_length=50) iosver = models.charfield(max_length=15) imagefilename = models.charfield(max_length=256, blank=true) model = models.charfield(max_length=20) cfgver = models.decimalfield(max_digits=3, decimal_places=2, blank=true, null=true) filename = models.charfield(max_length=256) cfghostname = models.charfield(max_length=63, blank=true) medium = models.charfield(max_length=20, blank=true) dmtype = models.charfield(max_length=20, blank=true) t1size = models.integerfield(blank=true, null=true) spid1 = models.charfield(max_length=20, blank=true) spid2 = models.charfield(max_length=20, blank=true) mrtglink = models.charfield(max_length=256, blank=true) loopbackip = models.charfield(max_length=15, blank=true) tunnelip = models.charfield(max_length=15, blank=true) managementip = models.charfield(max_length=15, blank=true) snmplocation = models.charfield(max_length=200, blank=true) uid = models.integerfield(primary_key=true) tun8inet = models.charfield(max_length=31, blank=true) tun9inet = models.charfield(max_length=31, blank=true) snmpcontact = models.charfield(max_length=300, blank=true) class meta: managed = false db_table = 'routers' class routerslastupdate(models.model): uid = models.foreignkey(bassrouters) hostname = models.foreignkey(bassrouters) infodate = models.datefield() infotime = models.timefield() deconverted = models.charfield(max_length=1) class meta: managed = false db_table = 'routers_lastupdate'
ouputput of attempts
>>> django.db import connection; connection.close() >>> thunderdome.remotedb_models import routerslastupdate, remotedbrouters >>> thunderdome.models import inventorywarehouse inventory_warehouse >>> serial = "0123456789" >>> inventory_warehouse.objects.filter(serial_numb=serial).first() <inventorywarehouse: inventorywarehouse object> >>>> >>>> >>> remotedbrouters.objects.using("bass").filter(hostname="router1").last() traceback (most recent call last): file "<console>", line 1, in <module> file "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 520, in last return qs[0] file "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 177, in __getitem__ return list(qs)[0] file "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 141, in __iter__ self._fetch_all() file "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 966, in _fetch_all self._result_cache = list(self.iterator()) file "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 265, in iterator row in compiler.results_iter(): file "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 700, in results_iter rows in self.execute_sql(multi): file "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql cursor.execute(sql, params) file "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute return super(cursordebugwrapper, self).execute(sql, params) file "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) file "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) file "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) file "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute return self.cursor.execute(query, args) file "/usr/local/lib/python2.7/site-packages/mysqldb/cursors.py", line 205, in execute self.errorhandler(self, exc, value) file "/usr/local/lib/python2.7/site-packages/mysqldb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue operationalerror: (2006, 'mysql server has gone away') >>>
edit:
the bass database remote database have read access tables. not control timeout on serverside db.
update
restarting mysql @ service level reset , allow connections through. still not elegant solution restart service every 8 hours. looking still.
django.db.connection
refers default connection (based on output of attempts code). if want connection non default database, use django.db.connections, specifying database name index i.e.
from django.db import connections connections['bass'].close()
that said, shouldn't have reset connection manually default (django default closes connection @ end of each request), though see why might needed long running admin scripts/commands long periods of inactivity.
Comments
Post a Comment