关于redis:解决Redis连接报错ERR-max-number-of-clients-reached

2次阅读

共计 1361 个字符,预计需要花费 4 分钟才能阅读完成。

问题产生起因

  • redis应用过程当中个别会由客户端进行连贯资源管理,例如调配连贯、监控连贯状态、回收连接池资源
  • 默认设置下,redis不会被动断开连接
  • redistimeout 参数配置项,默认单位是秒,当 timeout 是 0 的时候,redis不会被动敞开连贯
  • 因为 redis 默认的最大连接数是 10000,然而始终不敞开连贯的话随着时间推移,连贯越积越多,最终导致没有连贯可用

最终导致 redis 客户端连贯的时候报错,显示"ERR max number of clients reached"

获取以后 redis 配置

$ redis-cli
127.0.0.1:6379> CONFIG get * 
# 获取 timeout 能够执行
127.0.0.1:6379> CONFIG get timeout
1) "timeout"
2) "0"

解决方案

配置 timout 参数值

compose.yaml配置如下

redis:
    image: redis
    command: redis-server --requirepass testpasswd --timeout 10
    container_name: demo-redis
    restart: always
    oom_kill_disable: true
    mem_limit: 2g
    ports:
      - 127.0.0.1:6379:6379
    healthcheck:
      test: 'redis-cli ping || exit 1'

启动命令

redis-server --requirepass testpasswd --timeout 10

  • --requirepass testpasswd: 配置 redis 启动命令,设置启用明码连贯,明码设置为testpasswd
  • --timeout 10: 配置当连贯无操作 10s 之后连贯会断开

其余参数解释

oom_kill_disable: true

  • 禁止当内存占用太多时候敞开容器

mem_limit: 2g

  • 限度最大应用 2G 内存

healthcheck

  • 应用健康检查容器是否服务失常

ports

  • 端口凋谢配置 127.0.0.1:6379:6379 示意不对外部机器凋谢 6379 端口

验证

$ docker exec -it demo-redis /bin/sh
# redis-cli
127.0.0.1:6379> auth testpasswd
127.0.0.1:6379> config get timeout
1) "timeout"
2) "10"

# 此处期待 10 秒之后再次输出命令, 发现 Broken pipe, 这个是失常的,因为以后连贯 10s 内没有操作,所以 redis 服务器敞开了该连贯
127.0.0.1:6379> CONFIG get timeout
Error: Broken pipe
not connected> 

查看 clients 连贯

127.0.0.1:6379> INFO clients
# Clients
connected_clients:168
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:20567
client_recent_max_output_buffer:0
blocked_clients:1
tracking_clients:0
clients_in_timeout_table:1

浏览参考

redis 文件配置详解
redis 官网文档

正文完
 0