{"id":43867,"date":"2024-12-01T08:31:00","date_gmt":"2024-12-01T00:31:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43867\/"},"modified":"2024-12-01T08:31:00","modified_gmt":"2024-12-01T00:31:00","slug":"redis-%e5%bc%82%e5%b8%b8-read-error-on-connection-%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43867\/","title":{"rendered":"Redis\u00a0\u5f02\u5e38\u00a0read\u00a0error\u00a0on\u00a0connection\u00a0\u7684\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis&nbsp;\u5f02\u5e38&nbsp;read&nbsp;error&nbsp;on&nbsp;connection&nbsp;\u7684\u89e3\u51b3\u65b9\u6848<\/h1>\n<p><span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aRedis&nbsp;\u5f02\u5e38&nbsp;read&nbsp;error&nbsp;on&nbsp;connection&nbsp;\u7684\u89e3\u51b3\u65b9\u6848\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<h2>\u4e00\u3001\u9020\u6210\u539f\u56e0\u4e0e\u573a\u666f<\/h2>\n<p>\u5728\u63a5\u53e3\u8fde\u63a5 redis \u65f6\uff0c\u4f7f\u7528\u4e86 pconnect \u8fdb\u884c redis \u957f\u8fde\u63a5\uff0c\u5728 Redis-&gt;auth() \u4e2d\u629b\u51fa\u5f02\u5e38\uff1a<\/p>\n<pre>PHP Fatal error:  Uncaught exception 'RedisException' with message 'read error on connection' <\/pre>\n<p><strong>\u6ce8\uff1a<\/strong>\u7531\u4e8e php-fpm \u5bf9\u8fde\u63a5\u7684\u91cd\u7528\u6027\uff0c\u6240\u4ee5\uff0c\u5f53\u5b58\u5728cgi\u8fdb\u7a0b\u7ba1\u7406\u5668[\u5982php-fpm,spawn-cgi]\uff0c\u4f7f\u7528 pconnect \u6548\u7387\u6bd4 connect \u9ad8\u3002\u4f46\u662f\u7531\u4e8e\u6bcf\u4e2a\u6301\u4e45\u8fde\u63a5\u4f1a\u5360\u7528\u5185\u5b58\uff0c\u5f53\u5e76\u53d1\u91cf\u5f88\u5927\u7684\u65f6\uff0c\u5f88\u53ef\u80fd\u9020\u6210\u5185\u5b58\u5403\u7d27<\/p>\n<h2>\u4e8c\u3001\u51fa\u73b0\u539f\u56e0<\/h2>\n<p>\u901a\u8fc7 pconnect \u7684 redis \u957f\u8fde\u63a5\u4e0d\u4f1a\u4e3b\u52a8\u91ca\u653e\uff0c\u4f1a\u4e00\u76f4<strong>\u4fdd\u6301\u5728php-fpm\u8fdb\u7a0b\u4e2d\uff0c\u7b49\u5f85\u5b8c\u6210\u4e0b\u4e00\u4e2a\u8bf7\u6c42\u7684\u4efb\u52a1\uff0c\u5373\u4f7f\u901a\u8fc7 close \u4e5f\u4ec5\u662f\u4f7f\u5f53\u524dphp\u4e0d\u80fd\u518d\u8fdb\u884credis\u8bf7\u6c42\uff0c\u4f46\u65e0\u6cd5\u771f\u6b63\u5173\u95edredis\u957f\u8fde\u63a5\uff0c\u8fde\u63a5\u5728\u540e\u7eed\u8bf7\u6c42\u4e2d\u4ecd\u7136\u4f1a\u88ab\u91cd\u7528\uff0c\u76f4\u81f3fpm\u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7ed3\u675f\u3002<\/strong><\/p>\n<p><strong>\u8fd9\u6837\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\uff1a<\/strong><\/p>\n<h3>1\uff09\u539f\u56e01 \u4f7f\u7528\u4e86\u5df2\u7ecf\u65ad\u5f00\u7684\u8fde\u63a5<\/h3>\n<p>\u5047\u5982\u8fd9\u4e2a\u8fde\u63a5\u5df2\u7ecf\u65ad\u5f00\u4e86\uff0c\u4e0b\u4e2a\u8bf7\u6c42\u53ef\u80fd\u76f4\u63a5\u4f7f\u7528\u4e0a\u4e2a\u65ad\u5f00\u7684\u8fde\u63a5\uff0c\u5f53 php-fpm \u5904\u7406\u4e0b\u4e00\u4e2a\u8bf7\u6c42 reuse \u4e00\u4e2a\u65ad\u5f00\u7684\u8fde\u63a5\u53ef\u80fd\u5bfc\u81f4 read error on connection \u7684\u5f02\u5e38\uff0c<strong>\u4ee5\u4e0b\u662f phpredis \u6e90\u7801\u7684\u89e3\u91ca\uff1a<\/strong><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167258165163b19213a0a20.png\" class=\"aligncenter\" title=\"Redis\u00a0\u5f02\u5e38\u00a0read\u00a0error\u00a0on\u00a0connection\u00a0\u7684\u89e3\u51b3\u65b9\u6848\u63d2\u56fe\" alt=\"Redis\u00a0\u5f02\u5e38\u00a0read\u00a0error\u00a0on\u00a0connection\u00a0\u7684\u89e3\u51b3\u65b9\u6848\u63d2\u56fe\" \/><\/p>\n<h3>2\uff09\u539f\u56e02\uff1a\u6267\u884c\u8d85\u65f6<\/h3>\n<ul>\n<li><strong>a\uff09\u5ba2\u6237\u7aef\u8bbe\u7f6e\u7684\u8d85\u65f6\u65f6\u95f4\u8fc7\u77ed\u5bfc\u81f4\u7684\uff1b<\/strong><\/li>\n<li><strong>b\uff09\u5ba2\u6237\u7aef\u672a\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff0c\u4f46\u662f\u670d\u52a1\u7aef\u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u4e86\u9ed8\u8ba4\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e<\/strong><\/li>\n<\/ul>\n<p>php \u6709\u4e00\u4e2a\u914d\u7f6e\u9879 default_socket_timeout \u8868\u793a\u4e00\u4e2a&nbsp;socket \u8fde\u63a5\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba4\u662f60s\uff0c\u800cphpredis \u8fd9\u4e2a\u6269\u5c55\u7684\u5e95\u5c42\u8fde\u63a5 redis \u662f\u4f7f\u7528\u7684 socket\uff0c\u6240\u4ee5\u5f53\u4e00\u4e2a\u8fde\u63a5\u572860\u79d2\u5185\u6ca1\u6709\u88ab\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u4e0b\u6b21\u8fde\u63a5\u5c31\u4f1a\u629b\u51fa\u8fd9\u4e2a\u5f02\u5e38\u3002<\/p>\n<h2>\u4e09\u3001\u89e3\u51b3\u65b9\u6848<\/h2>\n<h3>1\uff09\u5982\u679c\u662f\u539f\u56e01<\/h3>\n<p><strong>\u957f\u8fde\u63a5\u6539\u4e3a\u77ed\u8fde\u63a5 \u6216\u8005 \u5728 pconnect \u4e2d\u589e\u52a0\u8d85\u65f6\u65f6\u95f4<\/strong><\/p>\n<pre>$this-&gt;redis-&gt;pconnect($host, $port, 2);       \/\/@todo 2\u79d2\u540e\u8be5\u8fde\u63a5\u8d85\u65f6\uff0c\u5c06\u4f7f\u7528\u65b0\u7684\u957f\u8fde\u63a5<\/pre>\n<h3>2\uff09\u5982\u679c\u662f\u539f\u56e02\uff1a\u4fee\u6539\u8d85\u65f6\u65f6\u95f4<\/h3>\n<p><strong>(\u4e0d\u63a8\u8350) \u65b9\u68481\uff1a<\/strong><\/p>\n<p>php.ini \u4e2d\u8bbe\u7f6e<\/p>\n<pre>default_socket_timeout = -1<\/pre>\n<p><strong>\u7136\u540e\u91cd\u542fphp-fpm<\/strong><\/p>\n<p><strong>\u65b9\u68482\uff1a<\/strong><\/p>\n<p>\u52a8\u6001\u4fee\u6539 php.ini&nbsp;<\/p>\n<pre>ini_set('default_socket_timeout', -1); \/\/ \u4e0d\u8d85\u65f6<\/pre>\n<p><strong>\u65b9\u68483\uff1a<\/strong><\/p>\n<p><strong>\u8bbe\u7f6e redis \u7684\u8d85\u65f6\u65f6\u95f4\u4e3a\u4e0d\u8d85\u65f6\uff1a<\/strong><\/p>\n<pre>$this-&gt;redis-&gt;setOption(Redis::OPT_READ_TIMEOUT, -1);<\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aRedis&nbsp;\u5f02\u5e38&nbsp;read&nbsp;error&nbsp;on&nbsp;connection&nbsp;\u7684\u89e3\u51b3\u65b9\u6848\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff01<\/p>\n<p>     \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1a\u811a\u672c\u4e4b\u5bb6 \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Django\u4f7f\u7528Redis\u8fdb\u884c\u7f13\u5b58\u8be6\u7ec6\u6b65\u9aa4\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Redisson\u4e3b\u4ece\u4e00\u81f4\u6027\u95ee\u9898\u8be6\u89e3\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Redis&nbsp;\u5f02\u5e38&nbsp;read&nbsp;error&nbsp;on&nbsp;connection&nbsp;\u7684\u89e3\u51b3\u65b9\u6848 0\u6d4f\u89c8 \u6536\u85cf \u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aRedis&nbsp;\u5f02\u5e38&nbsp;read&nbsp;error&nbsp;on&nbsp;connection&nbsp;\u7684\u89e3\u51b3\u65b9\u6848\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~ \u4e00\u3001\u9020\u6210\u539f\u56e0\u4e0e\u573a\u666f \u5728\u63a5\u53e3\u8fde\u63a5 redis \u65f6\uff0c\u4f7f\u7528\u4e86 pconnect \u8fdb\u884c redis \u957f\u8fde\u63a5\uff0c\u5728 Redis-&gt;auth() \u4e2d\u629b\u51fa\u5f02\u5e38\uff1a PHP Fatal error: Uncaught exception &#8216;RedisException&#8217; with message &#8216;read error on connection&#8217; \u6ce8\uff1a\u7531\u4e8e php-fpm \u5bf9\u8fde\u63a5\u7684\u91cd\u7528\u6027\uff0c\u6240\u4ee5\uff0c\u5f53\u5b58\u5728cgi\u8fdb\u7a0b\u7ba1\u7406\u5668[\u5982php-fpm,spawn-cgi]\uff0c\u4f7f\u7528 pconnect \u6548\u7387\u6bd4 connect \u9ad8\u3002\u4f46\u662f\u7531\u4e8e\u6bcf\u4e2a\u6301\u4e45\u8fde\u63a5\u4f1a\u5360\u7528\u5185\u5b58\uff0c\u5f53\u5e76\u53d1\u91cf\u5f88\u5927\u7684\u65f6\uff0c\u5f88\u53ef\u80fd\u9020\u6210\u5185\u5b58\u5403\u7d27 \u4e8c\u3001\u51fa\u73b0\u539f\u56e0 \u901a\u8fc7 pconnect \u7684 redis \u957f\u8fde\u63a5\u4e0d\u4f1a\u4e3b\u52a8\u91ca\u653e\uff0c\u4f1a\u4e00\u76f4\u4fdd\u6301\u5728php-fpm\u8fdb\u7a0b\u4e2d\uff0c\u7b49\u5f85\u5b8c\u6210\u4e0b\u4e00\u4e2a\u8bf7\u6c42\u7684\u4efb\u52a1\uff0c\u5373\u4f7f\u901a\u8fc7 close \u4e5f\u4ec5\u662f\u4f7f\u5f53\u524dphp\u4e0d\u80fd\u518d\u8fdb\u884credis\u8bf7\u6c42\uff0c\u4f46\u65e0\u6cd5\u771f\u6b63\u5173\u95edredis\u957f\u8fde\u63a5\uff0c\u8fde\u63a5\u5728\u540e\u7eed\u8bf7\u6c42\u4e2d\u4ecd\u7136\u4f1a\u88ab\u91cd\u7528\uff0c\u76f4\u81f3fpm\u8fdb\u7a0b\u751f\u547d\u5468\u671f\u7ed3\u675f\u3002 \u8fd9\u6837\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\uff1a 1\uff09\u539f\u56e01 \u4f7f\u7528\u4e86\u5df2\u7ecf\u65ad\u5f00\u7684\u8fde\u63a5 \u5047\u5982\u8fd9\u4e2a\u8fde\u63a5\u5df2\u7ecf\u65ad\u5f00\u4e86\uff0c\u4e0b\u4e2a\u8bf7\u6c42\u53ef\u80fd\u76f4\u63a5\u4f7f\u7528\u4e0a\u4e2a\u65ad\u5f00\u7684\u8fde\u63a5\uff0c\u5f53 php-fpm \u5904\u7406\u4e0b\u4e00\u4e2a\u8bf7\u6c42 reuse \u4e00\u4e2a\u65ad\u5f00\u7684\u8fde\u63a5\u53ef\u80fd\u5bfc\u81f4 read error on connection \u7684\u5f02\u5e38\uff0c\u4ee5\u4e0b\u662f phpredis [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-43867","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43867"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43867\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}