Django 得到 MySQL 异常 2014:命令不同步

Django got MySQL exception 2014: Commands out of sync

RHEL7 上 Django 的 Web 应用程序,Gunicorn 服务得到一个 MySQL 异常 #2014,说 "Commands out of sync; you can't run this command now"。

只有获取一个属性元素(“/api/admin/eav/attribute/24/”),其他元素都正常时才会出现此错误,例如下面的日志显示“/api/admin/eav/attribute/28/”正常。

我已尝试重启 Gunicorn 服务,但问题依旧。

此 Web 应用程序多年来一直运行良好,我们最近才注意到此错误。 Django Python 和 MySQL 的版本现在已经很旧了。

非常感谢任何有关修复的建议。

我也非常感谢一般的调试建议,例如:

查看下面的详细信息,如果您需要更多信息,请告诉我。感谢您的帮助。

版本:

日志:

[2020-03-20 18:17:37 +0000] [29744] [DEBUG] GET /api/admin/eav/attribute/
 - - [20/Mar/2020:18:17:38 -0600] "GET /api/admin/eav/attribute/ HTTP/1.0" 200 56843 "https://domain.under.test/api/admin/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
[2020-03-20 18:17:57 +0000] [29744] [DEBUG] GET /api/admin/eav/attribute/28/
 - - [20/Mar/2020:18:17:59 -0600] "GET /api/admin/eav/attribute/28/ HTTP/1.0" 200 280775 "https://domain.under.test/api/admin/eav/attribute/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
[2020-03-20 18:18:00 +0000] [29744] [DEBUG] GET /api/admin/jsi18n/
 - - [20/Mar/2020:18:18:00 -0600] "GET /api/admin/jsi18n/ HTTP/1.0" 200 2372 "https://domain.under.test/api/admin/eav/attribute/28/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
[2020-03-20 18:18:14 +0000] [29741] [DEBUG] GET /api/admin/eav/attribute/
 - - [20/Mar/2020:18:18:14 -0600] "GET /api/admin/eav/attribute/ HTTP/1.0" 200 56843 "https://domain.under.test/api/admin/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
[2020-03-20 18:18:23 +0000] [29738] [DEBUG] GET /api/admin/eav/attribute/24/
[2020-03-20 18:18:54 +0000] [29705] [CRITICAL] WORKER TIMEOUT (pid:29738)
[2020-03-20 18:18:54 +0000] [29738] [INFO] Worker exiting (pid: 29738)
Exception _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x7f82a329ec50>> ignored
Exception _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") in <generator object cursor_iter at 0x7f82a328e500> ignored
Exception _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x7f82a32a7cd0>> ignored
[2020-03-20 18:18:54 +0000] [29915] [INFO] Booting worker with pid: 29915
/webapp/EBACKEND/eav/models.py:42: RemovedInDjango19Warning: django.contrib.contenttypes.generic is deprecated and will be removed in Django 1.9. Its contents have been moved to the fields, forms, and admin submodules of django.contrib.contenttypes.
  from django.contrib.contenttypes import generic

[2020-03-20 18:21:40 +0000] [29915] [DEBUG] GET /api/admin/eav/attribute/
 - - [20/Mar/2020:18:21:40 -0600] "GET /api/admin/eav/attribute/ HTTP/1.0" 200 56843 "https://domain.under.test/api/admin/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"

看起来 Django 生成了一些低效的 SQL 查询,给系统造成了巨大的工作负载,导致数据库端出错。

如何打印SQL while 运行 Django,需要修改settings.py,详见下文link: log all sql queries