Django - OperationalError :( 2006,'MySQL服务器已经消失') [英] Django - OperationalError: (2006, 'MySQL server has gone away')

查看:1275
本文介绍了Django - OperationalError :( 2006,'MySQL服务器已经消失')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:如何在django中刷新MySQL连接?

遵循一个 MySQL服务器走了错误我发现 MySQL文档和其他来源(此处 )建议增加 wait_timeout MySQL参数。对我来说,这似乎是一种解决方法,而不是解决方案。我宁愿保持合理的 wait_timeout 并刷新代码中的连接。

Following a MySQL server has gone away error I found that MySQL documentation and other sources (here) suggest increasing the wait_timeout MySQL parameter. To me this seems like a workaround rather than a solution. I'd rather keep a reasonable wait_timeout and refresh the connection in the code.

错误:

  File "C:\my_proj\db_conduit.py", line 147, in load_some_model
    SomeModel.objects.update()
  File "C:\Python26\lib\site-packages\django-1.3-py2.6.egg\django\db\models\manager.py", line 177, in update
    return self.get_query_set().update(*args, **kwargs)
  File "C:\Python26\lib\site-packages\django-1.3-py2.6.egg\django\db\models\query.py", line 469, in update
    transaction.commit(using=self.db)
  File "C:\Python26\lib\site-packages\django-1.3-py2.6.egg\django\db\transaction.py", line 142, in commit
    connection.commit()
  File "C:\Python26\lib\site-packages\django-1.3-py2.6.egg\django\db\backends\__init__.py", line 201, in commit
    self._commit()
  File "C:\Python26\lib\site-packages\django-1.3-py2.6.egg\django\db\backends\__init__.py", line 46, in _commit
    return self.connection.commit()
OperationalError: (2006, 'MySQL server has gone away')

设置:Django 1.3.0,MySQL 5.5.14,innodb 1.1.8,Python 2.6.6,Win7 64bit

Setup: Django 1.3.0 , MySQL 5.5.14 , innodb 1.1.8 , Python 2.6.6, Win7 64bit

推荐答案

具有相同的问题。
我需要了解如何检查django中MySQLdb连接的连接状态。
i猜测它可以通过

having the same issue. I need idea how to check connection state for MySQLdb connection in django. i guess it can be achieved by

try:
    cursor.execute(sql)
catch OperationalError:
    reconnect

是否有更好的主意?

更新

我的决定

self.connection.stat()
if self.connection.errno()!=0:

检查mysqldb连接状态如果错误重新连接

check state of mysqldb connection if error recreate connection

更新更新

如果连接关闭,您还需要提供案例

you also need to serve case if connection is close

if self.connection.open:
    self.connection.stat()

刷新连接只是重新创建它

refresh connection is just recreating it

    db_settings = settings.DATABASES['mysql_db']
    try:
        self.connection = MySQLdb.connect(host=db_settings['HOST'],port=int(db_settings['PORT']),db=db_settings['NAME'],user=db_settings['USER'],passwd=db_settings['PASSWORD'])
    except MySQLdb.OperationalError, e:
        self.connection = None

这篇关于Django - OperationalError :( 2006,'MySQL服务器已经消失')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆