Linux 上装完 Redis,敲 redis-cli 直接报 Could not connect to Redis at 127.0.0.1:6379: Connection refused。不是客户端坏了,是服务压根没起来。

包管理器装的,用 systemctl 起

用 yum 或 apt 装的 Redis 一般带 systemd 服务,先 systemctl start redis 试试。服务名不一定是 redis,用 systemctl list-unit-files 加上 grep redis 查一下,有的发行版叫 redis-server。确认后 systemctl enable redis 设成开机自启,再 systemctl status redis 看状态。

源码编译的,手动带配置文件启动

进到解压目录的 src 下,执行 ./redis-server ../redis.conf,或者用绝对路径 redis-server /etc/redis.conf。想让它后台运行,把配置里的 daemonize 改成 yes,否则关掉终端进程就没了。启动日志里出现 Ready to accept connections 才算成功。

起不来先看这三处

  • 端口被占用:ss -lntp 里看 6379 是不是已经有进程,有就先停掉旧的。
  • 配置路径不对:redis-server 不指定配置文件会读默认值,端口和密码都跟你想的不一样。
  • 权限问题:日志目录和 dump 文件目录要可写,用 redis 用户跑就别用 root 启动。

如果提示 Job for redis.service failed because the control process exited with error code,先 journalctl -u redis -n 50 看最后几行,配置文件语法错误会直接写在第几行。验证很简单,redis-cli ping 返回 PONG 就通了。

连不上再看 bind 和 protected-mode,默认只监听 127.0.0.1,远程访问要改 bind 并配合防火墙放行。生产环境建议交给 systemd 管,别用 nohup 挂后台,机器一重启就没了。开机自启没生效,检查 systemctl is-enabled redis 的输出,显示 enabled 才算设好。