{"id":6384,"date":"2018-01-24T10:51:14","date_gmt":"2018-01-24T02:51:14","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6384"},"modified":"2018-01-24T10:51:14","modified_gmt":"2018-01-24T02:51:14","slug":"tornado%e4%b8%ad%e4%bd%bf%e7%94%a8django%e9%81%87%e5%88%b0%e7%9a%84mysql-has-gone-away%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6384.html","title":{"rendered":"Tornado\u4e2d\u4f7f\u7528Django\u9047\u5230\u7684MySQL has gone away\u95ee\u9898"},"content":{"rendered":"<p>\u80cc\u666f\uff0cweb\u6846\u67b6\u4f7f\u7528tornado\uff0c\u6570\u636e\u5e93ORM\u4f7f\u7528django\uff0c\u7136\u540e\u78b0\u5230Mysql\u91cd\u542f\u540e\uff0c\u53d1\u73b0django\u540e\u53f0\u6b63\u5e38\uff0ctornado web\u670d\u52a1\u5374\u4e00\u76f4\u62a5500\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nOperationalError: (2006, 'MySQL server has gone away')\r\n<\/pre>\n<p>\u76f4\u89c2\u5206\u6790\uff0c\u611f\u89c9\u662fdjango\u5efa\u7acb\u7684mysql\u8fde\u63a5\u6ca1\u6709\u88ab\u5173\u95ed\uff0c\u8fd8\u662f\u4f7f\u7528\u7684mysql\u91cd\u542f\u524d\u7684\u3002<\/p>\n<p>\u8fd8\u6709\u53e6\u4e00\u79cd\u53ef\u80fd\uff0cmysql\u6709\u4e00\u4e2a\u7a7a\u95f2\u65f6\u95f4\uff0c\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\u4e00\u76f4\u672a\u4f7f\u7528\u7684\u8fde\u63a5\u4f1a\u88ab\u5173\u95ed\u6389\uff0c\u8fd9\u4e2a\u65f6\u95f4\u53ef\u4ee5\u901a\u8fc7sql\u67e5\u8be2\u67e5\u770b\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nSHOW SESSION VARIABLES LIKE 'wait_timeout'\r\n<\/pre>\n<p>\u800cdjango\u7684settings\u4e2d\u6709\u4e2a\u53c2\u6570\uff0cCONN_MAX_AGE \uff0c\u63a7\u5236\u7684\u662f\u957f\u8fde\u63a5\u6700\u957f\u4fdd\u6301\u7684\u65f6\u95f4\uff0c\u53ea\u8981\u8fd9\u4e2a\u503c\u8bbe\u7f6e\u7684\u6bd4mysql\u7684wait_timeout\u5c0f\u5c31\u53ef\u4ee5\u907f\u514d\u4e0a\u9762\u8bf4\u7684\u95ee\u9898\u3002<\/p>\n<p>\u4f46\u662f\u6211\u9047\u5230\u7684\uff0c\u662fmysql\u91cd\u542f\u540etornado\u62a5\u9519\uff0c\u800cdjango\u81ea\u5df1\u7684admin\u8fd0\u884c\u6b63\u5e38\uff0c\u8fd9\u8bf4\u660edjango\u7684\u7ba1\u7406\u540e\u53f0\u6709\u5904\u7406\u91cd\u65b0\u5173\u95ed\u3001\u518d\u6253\u5f00mysql\u8fde\u63a5\u7684\u903b\u8f91\uff0c\u800c\u6211\u4eectornado\u5c42\u6ca1\u6709\u3002<\/p>\n<p>\u5728 django.db.__init__.py \u4e2d\uff0c\u6709\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# Register an event to reset transaction state and close connections past\r\n# their lifetime.\r\ndef close_old_connections(**kwargs):\r\n    for conn in connections.all():\r\n        conn.close_if_unusable_or_obsolete()\r\nsignals.request_started.connect(close_old_connections)\r\nsignals.request_finished.connect(close_old_connections)\r\n\r\nsignals.request_started \u548c signals.request_finished \u5206\u522b\u662fdjango\u5904\u7406\u4e00\u4e2ahttp\u8bf7\u6c42\u7684\u5f00\u59cb\u4e0e\u7ed3\u675f\u4fe1\u53f7\u3002\r\n<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u4eff\u7167\u8fd9\u4e2a\u5199\u6cd5\uff0c\u5728tornado\u7684\u5904\u7406http\u8bf7\u6c42\u5f00\u59cb\u4e0e\u7ed3\u675f\u8c03\u7528\u4e00\u4e0b close_old_connections \u5c31ok\u4e86\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom django.db import connections\r\nfrom tornado.web import RequestHandler\r\n\r\n\r\ndef close_old_connections():\r\n    for conn in connections.all():\r\n        conn.close_if_unusable_or_obsolete()\r\n\r\nclass CustomHandler(RequestHandler):\r\n\r\n\tdef prepare(self, *args, **kwargs):\r\n\t    close_old_connections()\r\n\t    super(MoviexHandler, self).prepare(*args, **kwargs)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f\uff0cweb\u6846\u67b6\u4f7f\u7528tornado\uff0c\u6570\u636e\u5e93ORM\u4f7f\u7528django\uff0c\u7136\u540e\u78b0\u5230Mysql\u91cd\u542f\u540e\uff0c\u53d1\u73b0django\u540e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6384","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6384"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6384\/revisions"}],"predecessor-version":[{"id":6385,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6384\/revisions\/6385"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}