【问题标题】:Django 1.7 Multiple Databases - OperationalError: (2006, 'MySQL server has gone away') Reset ConnectionDjango 1.7 多个数据库 - OperationalError: (2006, 'MySQL server has gone away') 重置连接
【发布时间】:2015-07-22 05:49:33
【问题描述】:

尝试重置与远程数据库的连接时出现错误。它不是默认数据库。似乎connections.close 只对默认的数据库配置有影响。

是我遗漏了什么还是有办法重置与特定数据库的连接(不是默认)?

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'

尝试的输出

>>> 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 小时重新启动服务的优雅解决方案。仍在寻求帮助。

【问题讨论】:

    标签: python mysql django database-connection


    【解决方案1】:

    django.db.connection 指的是默认连接(基于您尝试代码的输出)。如果要连接非默认数据库,请使用django.db.connections,将数据库名称指定为索引,即

    from django.db import connections
    connections['BaSS'].close()
    

    也就是说,默认情况下您不必手动重置连接(django 默认关闭end of each request 处的连接),尽管我明白为什么长时间运行的管理脚本/命令可能需要这样做不活动。

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 2012-12-19
      • 2013-12-16
      • 2015-10-20
      • 2011-02-04
      • 2010-09-09
      • 2012-10-30
      • 2019-10-13
      • 2015-12-02
      相关资源
      最近更新 更多