redis client连接数达到最大限制

本文介绍了一种当Redis客户端连接数达到上限时的处理方法,并提供了一个用于批量关闭Redis客户端连接的脚本。该脚本通过遍历连接列表并逐个关闭冗余连接来释放资源。

最近碰到reids客户端连接数达到1000,超过reids.confi里的

# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#

# maxclients 10000

查看reids-cli里的client list 发现很多连接,使用client kill命令结束。因为数量多,要一个一个Kill比较麻烦

所以需要些个脚本批处理,后面附上脚本代码

主要原因是本地测试项目时候直接关闭后,redis 连接没有主动关闭。有这几个redis配置可以主动关闭

空闲的连接

# Close the connection after a client is idle for N seconds (0 to disable)

timeout 0

# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.

tcp-keepalive 300

这个主动连接客户端,但耗性能。

后面附上结束redis客户端脚本

1 #/bin/bash
  2
  3 function read_line()
  4 {
  5  (redis-cli -h 127.0.0.1 -p 6379 -a leapmotor10214.pwd client list) >> /tmp/redis.txt
  6  cat /tmp/redis.txt | while read line
  7  do
  8   linestr="$line"
  9   #idbg=$[$lidbg+3]
 10   lidbg1=$(expr index "$linestr" r)
 11   lidbg=$[$lidbg1 + 2]
 12   lidend=$(expr index "$linestr" f)
 13   lidend=$[$lidend-$lidbg]
 14   port1=`expr substr "$linestr" $lidbg $lidend`
 15   redis-cli -h 127.0.0.1 -p 6379 -a leapmotor10214.pwd client kill $port1
 16   echo $port2
 17  done
 18 }
 19
 20 read_line


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值