Django 1.7 Multiple Databases - OperationalError: (2006, 'MySQL server has gone away') Reset Connection
Django 1.7 Multiple Databases - OperationalError: (2006, 'MySQL server has gone away') Reset Connection
我在尝试重置与远程数据库的连接时遇到错误。它不是默认数据库。似乎 connections.close 只对默认数据库配置有影响。
我是不是遗漏了什么或者有没有办法重置与特定数据库的连接(不是默认的)?
Django 1.7
Python2.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'
尝试的输出
>>> from django.db import connection; connection.close()
>>> from thunderdome.RemoteDB_models import RoutersLastupdate, RemoteDBRouters
>>> from thunderdome.models import InventoryWarehouse as 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
for row in compiler.results_iter():
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 700, in results_iter
for 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')
>>>
编辑:
BaSS 数据库是一个远程数据库,我只有对所有表的读取权限。我不控制此数据库的服务器端超时。
更新
在服务级别重新启动 MySQl 会重置此设置并允许连接通过。这仍然不是每 8 小时重新启动服务的优雅解决方案。仍在寻求帮助。
django.db.connection
指的是默认连接(基于您尝试代码的输出)。如果要连接非默认数据库,请使用 django.db.connections,指定数据库名称作为索引,即
from django.db import connections
connections['BaSS'].close()
就是说,默认情况下您不必手动重置连接(默认情况下 django 会在 end of each request 处关闭连接),但我明白为什么这可能需要很长时间 运行 管理员 scripts/commands 长时间不活动。
我在尝试重置与远程数据库的连接时遇到错误。它不是默认数据库。似乎 connections.close 只对默认数据库配置有影响。
我是不是遗漏了什么或者有没有办法重置与特定数据库的连接(不是默认的)?
Django 1.7 Python2.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'
尝试的输出
>>> from django.db import connection; connection.close()
>>> from thunderdome.RemoteDB_models import RoutersLastupdate, RemoteDBRouters
>>> from thunderdome.models import InventoryWarehouse as 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
for row in compiler.results_iter():
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 700, in results_iter
for 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')
>>>
编辑:
BaSS 数据库是一个远程数据库,我只有对所有表的读取权限。我不控制此数据库的服务器端超时。
更新
在服务级别重新启动 MySQl 会重置此设置并允许连接通过。这仍然不是每 8 小时重新启动服务的优雅解决方案。仍在寻求帮助。
django.db.connection
指的是默认连接(基于您尝试代码的输出)。如果要连接非默认数据库,请使用 django.db.connections,指定数据库名称作为索引,即
from django.db import connections
connections['BaSS'].close()
就是说,默认情况下您不必手动重置连接(默认情况下 django 会在 end of each request 处关闭连接),但我明白为什么这可能需要很长时间 运行 管理员 scripts/commands 长时间不活动。