解决Redis 连接池报错:ERR max number of clients reached
前言
redis maxclients 是redis server的重要配置,它决定了客户端的最大连接数量,最大客户端连接数量。由于redis不区分连接是客户端连接还是内部打开文件或者和slave连接等,所以maxclients最小存在32个连接数,如果超过了设置的maxclients,redis会给新的连接发送"max number of clients reached
",并关闭连接。
在Redis 2.4中,可以同时处理的最大客户端数量存在硬编码限制。 在Redis 2.6中,此限制是动态的:默认情况下,它设置为10000个客户端,除非Redis.conf中的maxclients指令另有说明。
解决思路
绝大部分原因是由于客户端很多空闲连接都没有被及时释放掉从而导致connected_clients非常高,其他可能就是maxclients设置的太少了,或者就是软硬件存在限制。
客户端
- 设置超时时间
JedisPoolConfig jedisPoolConfig = initPoolConfig();
jedisPool = new JedisPool(jedisPoolConfig, "*.*.*.*", 6379);
================================================>
JedisPoolConfig jedisPoolConfig = initPoolConfig();
jedisPool = new JedisPool(jedisPoolConfig, "*.*.*.*", 6379,2*1000);
- 回收连接资源
Jedis jedis = JedisUtils.getJedis();try {//Todo }catch (Exception e){throw new RuntimeException(e.getMessage(), e);JedisUtils.returnBrokenResource(jedis);}finally {JedisUtils.returnResource(jedis);}
/*** 回收Jedis对象资源* * @param jedis*/public synchronized void returnResource(Jedis jedis) {if (jedis != null) {jedisPool.returnResource(jedis);}}/*** Jedis对象出异常的时候,回收Jedis对象资源* * @param jedis*/public synchronized void returnBrokenResource(Jedis jedis) {if (jedis != null) {jedisPool.returnBrokenResource(jedis);}}
在Jedis 2.6.2 以后,由于重写了jedis.close()
,实现自动关闭,2.6.3以后正式使用,详见Deprecates JedisPool returnResource and returnBrokenResource,这样可以代码使用JDK7中新增的try-with-resource语法糖,这样代码会简洁很多如下:
try (Jedis jedis = JedisUtils.getJedis()) {//Todo } catch (Exception e) {throw new RuntimeException(e.getMessage(), e);}
后续贴一个JedisUtils工具类
服务端
断开空闲连接
建议先断开部分客户端,不然由于报错:max number of clients reached,无法创建连接。
- 使用redis-cli进入命令行,使用
info clients
查看客户端连接数,然后使用client list
查看当前连接的客户端。
- 设置参数timeout(客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能)自动断开连接,键入
config set timeout 600
可以看到连接数由3430变为709减低了不少,当然也可以选择使用CLIENT KILL
命令来杀掉客户端连接。
-------------------------到此这个问题基本可以解决,下面部分选读-----------------------------------------
设置maxclients
- 配置文件redis.conf
maxclients 10000
- 启动参数
$ ./redis-server --maxclients 10000
- 命令
CONFIG set maxclients 10000
系统平台
由于创建连接数需要分配和消耗系统资源,所以在设置maxclients的时候系统是有预设限制的,如上,10000个连接数是现在redis版本的默认值,由于系统软硬件限制,redis-server启动时候是无法分配到资源的
$ ./redis-server --maxclients 10000
[41422] 23 Jan 11:28:33.179 # Unable to set the max number of files limit to 10032 (Invalid argument), setting the max clients configuration to 4096.
# 获取redis PID
$ ps -ef | grep redis-server
# 获取redis的能够打开的最大文件描述符
cat /proc/{PID}/limints
图中Max open file这行参数可以看得到进程能够打开的最大文件描述符为4096(ll /proc/{PID}/fd | wc -l
可以查看已经产生的个数)并非10000,受到了软硬件限制,这可能产生报错的最根本原因。
为了设置maxclients这个参数,官网给与了解决方案,使用ulimit为系统范围的设置,详见Maximum number of clients
这个方式的前置条件是 hard limit is big enough ,会看上面的截图,Max open file的硬件限制也是4096,达到了最大值,于是有了如下的解决方案
- 编辑/etc/security/limits.conf
# 具体设置多少根据实际情况而定
* soft nofile 65536 # open files (-n)
* hard nofile 65536* soft nproc 65565
* hard nproc 65565 # max user processes (-u)
- 保存重新登录服务器即可
参考文章
- JedisPool returnResource and returnBrokenResource https://github.com/xetorthio/jedis/pull/912
- redis报错“max number of clients reached" https://blog.51cto.com/11819159/1916133
- Redis连接的客户端数过高 https://blog.csdn.net/cxhgg/article/details/67640263
- Redis Clients Handling https://redis.io/topics/clients#maximum-number-of-clients
- linux中ulimit作用 https://www.cnblogs.com/kongzhongqijing/p/5784293.html
解决Redis 连接池报错:ERR max number of clients reached相关推荐
- Redis问题:ERR max number of clients reached
redis高并发下,可能有些连接没有释放,导致连接数耗尽,会提示 ERR max number of clients reached 这个时候通过命令查看客户端使用连接数 [root@home-ser ...
- spingboot使用redis连接池报错
配置如下,增加了连接池相关 redis:host: localhostport: 6379database: 7lettuce:pool:max-active: 20max-idle: 20max-w ...
- python连接数据库mysql失败_解决python连接mysql报错问题
解决python连接mysql报错问题 最近跑python脚本时遇到一个问题,当sql语句中有中文时,执行python脚本报以下错误: Traceback (most recent call last ...
- win7设置ipsec_解决win7连接IPsec报错789和809错误
解决win-7连接IPsec报错789和809错误 萌新一枚,大神请飘过~~~ 最近遇到一个很沙雕的问题,win7连接IPsec一直报错789,百度方法解决掉后又报错809,809解决掉后继续报789 ...
- 阿里连接池报错:Failed to configure a DataSource ‘url‘ attribute is not specified and no embedd
阿里连接池报错:Failed to configure a DataSource: 'url' attribute is not specified and no embedd 明明已经配置数据库连接 ...
- 【pureftp】解决filezilla连接ftp报错 服务器发回了不可路由的地址 使用服务器地址代替
问题 服务器设置pureftp,设置完成之后,使用filezilla连接,报错返回 服务器发回了不可路由的地址.使用服务器地址代替. 解决方案 1 配置返回主动模式 2 站点管理器配置主动模式 3 检 ...
- 详解Jedis连接池报错处理
在使用Jedis连接池模式下,比较常见的报错如下: redis.clients.jedis.exceptions.JedisConnectionException:Could not get a re ...
- c3p0连接池报错 A client timed out while waiting
今天在看c3p0连接池的时候,因为连接数据库疯狂报错 com.mchange.v2.resourcepool.TimeoutException: A client timed out while wa ...
- Druid连接池报错:org.postgresql.util.PSQLException: FATAL: too many connections for role “*********“
一 错误描述 1 负责的BI中的数据数据可视化项目一直运行的比较稳定,突然早上用户反馈,报表查询非常慢,非常卡,而且有些网页直接打不开(单体应用,没有降级和熔断). 2 没一会CPU就报警了,CPU使 ...
最新文章
- 避障机器人程序c语言,基于51单片机小车寻迹、避障源程序(注释很详细)
- BugKuCTF 杂项 眼见非实(ISCCCTF)
- spring入门案例plus
- hdp amabri mysql_MySQL数据库之Ambari 安装配置 MySql
- iOS 常用公共方法
- C# Windows Workflow Fundation之状态机
- mac 安装memcached服务
- 电子工业出版社PPT图书优秀作者上海书城讲座
- 01-artDialog4.1.7常用整理
- 算法:从有序数组中移除重复的数据26. Remove Duplicates from Sorted Array
- Alex 的 Hadoop 菜鸟教程: 第15课 Impala 安装使用教程
- php怎么做一个音乐播放器,实现一个HTML5音乐播放器的实例
- 如何缩小jpg图片大小kb?手机照片怎么压缩大小?
- Neuralizing Regular Expressions for Slot Filling 神经网络转回自动机
- yii mysql 查询 类型转换_yii2 数据库查询结果字段类型的问题
- 解决 Javascript 中 atob 方法解码中文字符乱码问题
- 小米手机fastboot模式出现Press any key to shutdown字样解决方法
- JAVA学习6-集合工具类、流
- java设计五子棋_JAVA课程设计+五子棋(团队博客)
- delta和gamma中性_套期保值中性技术操作之构建delta—gamma中性