redis-cli Could not connect to Redis at 127.0.0.1:6379: Connection refused

64 阅读1分钟

1. 启动 Redis 服务

在 macOS 上:

# 如果使用 Homebrew 安装的 Redis
brew services start redis

# 或者手动启动
redis-server /usr/local/etc/redis.conf

在 Linux 上:

# Ubuntu/Debian
sudo systemctl start redis-server
sudo systemctl enable redis-server

# CentOS/RHEL
sudo systemctl start redis
sudo systemctl enable redis

在 Windows 上:

# 如果作为服务安装
redis-server --service-start

2. 检查 Redis 服务状态

macOS (Homebrew):

brew services list

Linux:

sudo systemctl status redis-server
# 或
sudo systemctl status redis

3. 检查 Redis 配置

查看 Redis 是否在非默认端口运行:

ps aux | grep redis

如果 Redis 在其他端口运行,连接时指定端口:

redis-cli -p 6380  # 替换为实际端口

4. 安装 Redis(如果尚未安装)

macOS:

brew install redis

Ubuntu/Debian:

sudo apt update
sudo apt install redis-server

CentOS/RHEL:

sudo yum install redis
# 或
sudo dnf install redis

5. 测试连接

服务启动后,测试连接:

redis-cli ping

应该返回 PONG

6. 检查防火墙设置

确保防火墙没有阻止 Redis 端口:

# 检查防火墙状态
sudo ufw status  # Ubuntu
sudo firewall-cmd --list-all  # CentOS

7. 查看 Redis 日志

如果启动失败,查看日志:

# macOS
tail -f /usr/local/var/log/redis.log

# Linux
sudo tail -f /var/log/redis/redis-server.log

选择适合你系统的方法,通常启动 Redis 服务后问题就能解决。