当前位置:网站首页>Redis 异常 read error on connection 解决方案
Redis 异常 read error on connection 解决方案
2022-04-23 09:30:00 【潘广宇 de 小白笔记】
一、造成原因与场景:
在接口连接 redis 时,使用了 pconnect 进行 redis 长连接,在 Redis->auth() 中抛出异常:
PHP Fatal error: Uncaught exception 'RedisException' with message 'read error on connection'
注:由于 php-fpm 对连接的重用性,所以,当存在cgi进程管理器[如php-fpm,spawn-cgi],使用 pconnect 效率比 connect 高。但是由于每个持久连接会占用内存,当并发量很大的时,很可能造成内存吃紧
二、出现原因
通过 pconnect 的 redis 长连接不会主动释放,会一直保持在php-fpm进程中,等待完成下一个请求的任务,即使通过 close 也仅是使当前php不能再进行redis请求,但无法真正关闭redis长连接,连接在后续请求中仍然会被重用,直至fpm进程生命周期结束。
这样会出现以下两种情况:
1)原因1 使用了已经断开的连接
假如这个连接已经断开了,下个请求可能直接使用上个断开的连接,当 php-fpm 处理下一个请求 reuse 一个断开的连接可能导致 read error on connection 的异常,以下是 phpredis 源码的解释:

2)原因2:执行超时
a)客户端设置的超时时间过短导致的;
b)客户端未设置超时时间,但是服务端执行时间超过了默认超时时间设置
php 有一个配置项 default_socket_timeout 表示一个 socket 连接的超时时间,默认是60s,而phpredis 这个扩展的底层连接 redis 是使用的 socket,所以当一个连接在60秒内没有被使用的时候,下次连接就会抛出这个异常。
三、解决方案
1)如果是原因1:
长连接改为短连接 或者 在 pconnect 中增加超时时间
$this->redis->pconnect($host, $port, 2); //@todo 2秒后该连接超时,将使用新的长连接
2)如果是原因2:修改超时时间
(不推荐) 方案1:
php.ini 中设置
default_socket_timeout = -1
然后重启php-fpm
方案2:
动态修改 php.ini
ini_set('default_socket_timeout', -1); // 不超时
方案3:
设置 redis 的超时时间为不超时:
$this->redis->setOption(Redis::OPT_READ_TIMEOUT, -1);
参考:
版权声明
本文为[潘广宇 de 小白笔记]所创,转载请带上原文链接,感谢
https://panguangyu.blog.csdn.net/article/details/124296439
边栏推荐
- Operation not allowed for a result set of type resultset TYPE_ FORWARD_ ONLY. Explain in detail
- 108. Convert an ordered array into a binary search tree
- NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
- Machine learning (VI) -- Bayesian classifier
- NLLLoss+log_ SoftMax=CE_ Loss
- Go language learning notes - structure | go language from scratch
- JS case to find the maximum value, reverse the array, bubble sort
- js 原型链的深入
- Using sqlmap injection to obtain the account and password of the website administrator
- Comparison of overloading, rewriting and hiding
猜你喜欢
![Buuctf [actf2020 freshman competition] include1](/img/47/b8f46037f7e9476b8e01e8d6a7857a.png)
Buuctf [actf2020 freshman competition] include1

node安装

数据清洗 ETL 工具Kettle的安装

Acquisition of DOM learning elements JS

Go language learning notes - array | go language from scratch

Distributed message oriented middleware framework selection - Digital Architecture Design (7)

501. Mode in binary search tree

【SQL server速成之路】数据库的视图和游标

Applet error: cannot read property'currenttarget'of undefined

Go language learning notes - language interface | go language from scratch
随机推荐
Give the method of instantiating the object to the new object
ALV tree (ll LR RL RR) insert delete
Flink SQL realizes the integration of stream and batch
108. Convert an ordered array into a binary search tree
Employee probation application (Luzhou Laojiao)
Cloud computing competition -- basic part of 2020 competition [task 3]
First principle mind map
112. Path sum
Leetcode-199 - right view of binary tree
Production practice elk
What is monitoring intelligent playback and how to use intelligent playback to query video recording
搞不懂时间、时间戳、时区,快来看这篇
OpenCV中的图像处理 —— 轮廓入门+轮廓特征
Failed to download esp32 program, prompting timeout
NPM installation yarn
MySQL of database -- basic common query commands
[geek challenge 2019] havefun1
MySQL - Chapter 1 (data type 2)
JS what is an event? Event three elements and operation elements
Installation of data cleaning ETL tool kettle