Django 1.6 w / gunicorn - OperationalError:(2006,'MySQL服务器已经消失')

时间:2015-07-29 13:31:57

标签: mysql django gunicorn

在升级到Django 1.6的过程中,我开始经常向我用来运行django应用程序的gunicorn服务器发送OperationalError: (2006, 'MySQL server has gone away')消息。这些错误从服务器启动的那一刻起立即发生,对于只需要一秒钟的请求,这让我怀疑它是一个超时问题。项目的旧1.4分支上不存在此错误,如果仅通过django-admin.py runserver提供1.6分支,则该分支不会以这种方式运行。

我通常通过一个sv进程运行gunicorn(虽然如果我手动运行也会出错)使用命令django-admin.py run_gunicorn --workers=4 -b localhost:8000导致许多请求,甚至是静态媒体请求,返回:

[ERROR] 2015-07-29 14:30:27,931 - gunicorn.error:260 - Error handling request
Traceback (most recent call last):
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 125, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
    self.load_middleware()
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
    mw_instance = mw_class()
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/middleware/locale.py", line 24, in __init__
    for url_pattern in get_resolver(None).url_patterns:
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 365, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 360, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/opt/apps/maplecroft/versions/current/websites/maplecroft/urls.py", line 2, in <module>
    from maplecroft.views import RiskAtlasesLandingView
  File "/opt/apps/maplecroft/versions/current/maplecroft/views.py", line 40, in <module>
    import maplecroft.search as _search
  File "/opt/apps/maplecroft/versions/current/maplecroft/search.py", line 71, in <module>
    class MaplecroftSearchForm(SearchForm):
  File "/opt/apps/maplecroft/versions/current/maplecroft/search.py", line 111, in MaplecroftSearchForm
    choices=model_choices(),
  File "/opt/apps/maplecroft/versions/current/maplecroft/search.py", line 57, in model_choices
    for category in reversed(Category.objects.filter(parent=None)):
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/db/models/query.py", line 77, in __len__
    self._fetch_all()
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/db/models/query.py", line 857, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/opt/envs/maplecroft/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (2006, 'MySQL server has gone away')

然而,如果我放到--workers=1,一切似乎都顺利进行,那么我目前的想法是,这是一个与枪支的线程工作者功能有关的问题?

编辑:我刚尝试将gunicorn升级到最新版本(在0.17.2上),但这似乎没有什么区别。

我想知道这个问题:https://serverfault.com/questions/407612/error-2006-mysql-server-has-gone-away是否相关,但很难将其与我当前的问题叠加在一起

1 个答案:

答案 0 :(得分:0)

看起来它毕竟是枪支版本 - 我尝试升级gunicorn但仍然使用启动服务器的django-admin.py run_gunicorn方法。升级gunicorn并切换到未弃用的gunicorn wsgi:application方法解决了它。