在 CentOS 上让 Redis 开机自启,常见有以下几种方式,取决于你是如何安装 Redis 的。
如果你是通过 yum 安装的 Redis:
yum install redis -y
systemctl enable redis
systemctl start redis
systemctl status redis
✅ 这种方式自带 systemd 服务,最省心
如果你是 源码安装(如 /usr/local/redis):
vi /etc/systemd/system/redis.service
内容示例(按实际路径修改):
[Unit]
Description=Redis Server
After=network.target
[Service]
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
User=redis
Group=redis
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable redis
systemctl start redis
如果你用的是 CentOS 6:
chkconfig redis on
service redis start
redis.conf 中必须关闭守护进程
daemonize no
(systemd 模式下不要设为 yes)
确认端口和权限
ps -ef | grep redis
netstat -lntp | grep 6379
防火墙(如需远程)
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload
reboot
redis-cli ping
# 返回 PONG 表示成功
如果你愿意,可以告诉我:
我可以给你完全对应你环境的配置方案。