{"id":201301,"date":"2025-05-10T09:23:49","date_gmt":"2025-05-10T01:23:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201301\/"},"modified":"2025-05-10T09:23:49","modified_gmt":"2025-05-10T01:23:49","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:\/\/server.hk\/cnblog\/201301\/","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 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\"><\/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","protected":false},"excerpt":{"rendered":"<p>Redis&nbsp;\u5f02\u5e38&#038;nb&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201301","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201301"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201301\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}