(2006,“MySQL 服务器已经消失”)WSGI django
我在 WSGI 下有一个 MySQL 和 Django 一起消失了。我在 stackoverflow 上找到了这个问题的条目,但没有专门针对 Django 的条目。除了解决方法(例如每隔一段时间轮询一次网站,或增加数据库超时)之外,谷歌没有提供任何帮助。没有什么确定的。从技术上讲,如果服务器挂起连接,Django 和/或 MySQLdb(我使用最新的 1.2.3c1)应该尝试重新连接,但这种情况不会发生。如果没有解决方法,我该如何解决这个问题?
I have a MySQL gone away with Django under WSGI. I found entries for this problem on stackoverflow, but nothing with Django specifically. Google does not help, except for workarounds (like polling the website every once in a while, or increasing the database timeout). Nothing definitive. Technically, Django and/or MySQLdb (I'm using the latest 1.2.3c1) should attempt a reconnect if the server hanged the connection, but this does not happen. How can I solve this issue without workarounds ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显示“wait_timeout”等变量;
此设置将抛出“mysql gone away”错误
将其设置为一个非常大的值以防止它“消失”
或者在一段时间后简单地重新 ping mysql 连接
show variables like 'wait_timeout';
this is the setting will throw back the "mysql gone away" error
set it to a very large value to prevent it "gone away"
or simple re-ping the mysql connection after certain period
Django 开发人员在 https://code.djangoproject 中对所有此类问题给出了一个简短的答案。 com/ticket/21597#comment:29
Django developers gave one short answer for all questions like this in https://code.djangoproject.com/ticket/21597#comment:29
您可以创建中间件来 ping() MySQL 连接(其中
在处理视图之前将重新连接(如果超时)
您还可以添加中间件来捕获异常,重新连接,然后重试
视图(我想我更喜欢上面的解决方案更简单,但是假设超时很少见,它在技术上应该可行并且具有高性能。这也假设失败的视图没有副作用,这是一个理想的属性,但可能很难做到,尤其是如果您在视图中写入文件系统以及数据库。)
You could create middleware to ping() the MySQL connection (which
will reconnect if it timed out) before processing the view
You could also add middleware to catch the exception, reconnect, and retry the
view (I think I would prefer the above solution as simpler, but it should technically work and be performant assuming timeouts are rare. This also assumes a failed view has no side effects, which is a desirable property but can be difficult to do, especially if you write to a filesystem as well as a db in your view.)