温馨提示×

centos redis配置怎样生效

小樊
49
2026-09-05 23:54:26
栏目: 云计算

在 CentOS 上,让 Redis 配置生效通常取决于你修改的是哪个配置文件以及Redis 是如何启动的。下面按常见情况说明:


一、修改 redis.conf 后让配置生效

1️⃣ 找到 Redis 配置文件

常见位置:

/etc/redis.conf
/etc/redis/redis.conf

查看 Redis 实际使用的配置文件:

ps -ef | grep redis

例如:

redis-server /etc/redis.conf

2️⃣ 修改配置

vi /etc/redis.conf

常见修改示例:

bind 0.0.0.0
protected-mode no
requirepass yourpassword
maxmemory 512mb

3️⃣ 让配置生效(重点)

✅ 方法一:重启 Redis(最常用)

systemctl restart redis

systemctl restart redis.service

确认状态:

systemctl status redis

✅ 方法二:先停止再启动

systemctl stop redis
systemctl start redis

4️⃣ 验证配置是否生效

redis-cli
CONFIG GET bind
CONFIG GET requirepass
CONFIG GET maxmemory

二、不重启 Redis 临时生效(在线修改)

适合 不能重启 的场景:

redis-cli
CONFIG SET maxmemory 512mb
CONFIG SET requirepass yourpassword

⚠️ 注意:

  • 临时生效
  • Redis 重启后失效
  • 不会写入 redis.conf

三、使用 systemd 时的注意事项

如果 Redis 是通过 systemd 启动的:

✅ 必须保证:

systemctl enable redis

否则重启服务器后不会自动启动。


四、Docker 中的 Redis(补充)

如果是 Docker:

docker restart redis

或修改 docker run 参数 / docker-compose.yml 后重启容器。


五、常见坑

❌ 改了配置但没重启
❌ 改错配置文件(有多个 redis.conf)
❌ Redis 以 redis-server 直接启动,没用 systemd
❌ 配置写错导致 Redis 启动失败(看日志)

查看日志:

journalctl -u redis

tail -f /var/log/redis/redis.log

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

  • CentOS 版本(7 / 8 / Stream)
  • Redis 安装方式(yum / 源码 / Docker)
  • 具体改了哪项配置

我可以给你精确的操作步骤

0