温馨提示×

Centos Redis如何配置开机启动

小樊
42
2026-07-20 02:30:54
栏目: 云计算

CentOS 上配置 Redis 开机自启,常见有两种方式:systemd(推荐,CentOS 7+)chkconfig(CentOS 6)。下面分别说明。


一、CentOS 7 / 8 / Stream(使用 systemd,推荐)

1. 确认 Redis 已安装

redis-server --version

如果未安装:

yum install -y redis
# 或
dnf install -y redis

2. 启动并设置开机自启

systemctl start redis
systemctl enable redis

查看状态:

systemctl status redis

3. 配置文件路径(常见)

  • 配置文件:/etc/redis.conf
  • 服务文件:/usr/lib/systemd/system/redis.service

如需修改配置:

vim /etc/redis.conf

常见参数:

bind 0.0.0.0
daemonize no
requirepass yourpassword

修改后重启:

systemctl restart redis

二、CentOS 6(使用 chkconfig)

1. 启动 Redis

service redis start

2. 设置开机启动

chkconfig redis on

查看:

chkconfig --list redis

三、源码安装 Redis(无 systemd 服务)

如果你是通过源码安装的 Redis,需要手动创建 systemd 服务。

1. 创建服务文件

vim /etc/systemd/system/redis.service

内容示例:

[Unit]
Description=Redis Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

2. 重新加载 systemd 并启用

systemctl daemon-reload
systemctl start redis
systemctl enable redis

四、验证是否开机自启成功

systemctl is-enabled redis

输出:

enabled

重启服务器测试:

reboot
redis-cli ping

五、常见问题

1. Redis 启动失败

查看日志:

journalctl -u redis

2. 端口 / 权限问题

  • 确认端口未被占用
  • 确认 pid 文件目录存在且有权限

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

  • ✅ CentOS 版本
  • ✅ Redis 安装方式(yum / 源码)

我可以给你完全针对你环境的配置方案

0