温馨提示×

centos如何配置redis连接数

小樊
53
2026-10-02 22:20:13
栏目: 云计算

在 CentOS 上配置 Redis 的连接数,核心是通过 redis.conf 中的 maxclients 参数来控制,同时还要注意 系统级别的限制(如文件描述符限制),否则配置可能不生效。

下面按步骤说明:


一、修改 Redis 配置(maxclients)

1. 找到 redis.conf

常见位置:

/etc/redis.conf

或

/etc/redis/redis.conf

2. 修改 maxclients

vim /etc/redis.conf

找到并修改:

maxclients 10000

默认一般是 10000
如果 Redis 以 redis 用户运行,受系统 ulimit 限制

3. 重启 Redis

systemctl restart redis

或

systemctl restart redis-server

二、修改系统文件描述符限制(非常重要)

Redis 连接数受 文件描述符(fd) 限制。

1. 查看当前限制

ulimit -n

2. 修改系统限制

编辑:

vim /etc/security/limits.conf

添加:

* soft nofile 65535
* hard nofile 65535
redis soft nofile 65535
redis hard nofile 65535

3. 修改 systemd 限制(CentOS 7+)

vim /usr/lib/systemd/system/redis.service

在 [Service] 下添加:

LimitNOFILE=65535

重载 systemd:

systemctl daemon-reload
systemctl restart redis

三、验证配置是否生效

1. 登录 Redis

redis-cli

2. 查看最大连接数

CONFIG GET maxclients

3. 查看当前连接数

INFO clients

输出示例:

connected_clients:12
maxclients:10000

四、常见问题说明

❌ maxclients 不生效

原因通常是:

  • 系统 nofile 限制太小
  • Redis 以 systemd 启动但未配置 LimitNOFILE

❌ 报错 “max number of clients reached”

说明:

  • 实际连接数已超 maxclients
  • 或 fd 不够

五、示例推荐配置(中等规模)

maxclients 20000

系统:

nofile = 65535

如果你愿意,可以告诉我:

  • Redis 版本
  • CentOS 版本
  • 是否用 Docker / 源码安装

我可以给你更精确的配置方案。

0 踩