What's Actually Happening
When you try to connect to Redis and get "connection refused", the Redis server is not accepting connections on the specified host and port. This could mean Redis isn't running, is on a different port, or there's a network issue.
The Error You'll See
```bash $ redis-cli ping Could not connect to Redis at 127.0.0.1:6379: Connection refused
# Or in application logs: redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused. ```
Why This Happens
- 1.Redis not running - Server process stopped or crashed
- 2.Wrong port - Redis listening on different port
- 3.Wrong host - Connecting to wrong IP address
- 4.Bind address - Redis only listening on specific interface
- 5.Firewall blocking - Port blocked by iptables or firewall
- 6.Permission denied - Socket file permission issues
Step 1: Check if Redis is Running
```bash # Check Redis process ps aux | grep redis-server
# Check systemd service systemctl status redis # Or: systemctl status redis-server ```
If not running:
# Start Redis
systemctl start redis
# Or:
redis-server /etc/redis/redis.confStep 2: Check Redis Port
# Check what port Redis is listening on
netstat -tlnp | grep redis
# Or:
ss -tlnp | grep redisOutput:
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1234/redis-serverIf Redis is on different port:
```bash # Connect to correct port redis-cli -p 6380 ping
# Check config grep "^port" /etc/redis/redis.conf ```
Step 3: Check Bind Address
# Check Redis bind address
grep "^bind" /etc/redis/redis.confDefault:
bind 127.0.0.1If Redis only binds to localhost, external connections fail.
To allow external connections:
```bash # Edit config sudo vim /etc/redis/redis.conf
# Change bind to include your IP or 0.0.0.0 (with caution) bind 0.0.0.0 # Or specific IPs: bind 127.0.0.1 192.168.1.100 ```
Restart Redis:
systemctl restart redisStep 4: Test Connection
```bash # Test with redis-cli redis-cli ping
# Should return: # PONG
# Test with specific host and port redis-cli -h 127.0.0.1 -p 6379 ping ```
Step 5: Check Firewall
If connecting from remote host:
```bash # On Redis server, check firewall sudo iptables -L -n | grep 6379
# For ufw sudo ufw status
# Allow Redis port sudo ufw allow 6379/tcp
# For firewalld sudo firewall-cmd --add-port=6379/tcp --permanent sudo firewall-cmd --reload ```
Step 6: Check Redis Logs
# Check Redis logs
tail -f /var/log/redis/redis-server.log
# Or:
journalctl -u redis -fLook for errors:
``` # Address already in use Creating Server TCP listening socket *:6379: bind: Address already in use
# Permission denied Can't open /var/lib/redis/dump.rdb: Permission denied ```
Step 7: Check for Protected Mode
Redis 3.2+ has protected mode. If you bind to 0.0.0.0 without password:
redis-cli ping
# DENIED Redis is running in protected modeFix by setting password:
```bash # Edit config sudo vim /etc/redis/redis.conf
# Set password requirepass your_strong_password
# Restart systemctl restart redis ```
Connect with password:
redis-cli -a your_strong_password pingOr disable protected mode (not recommended for production):
protected-mode noStep 8: Check Unix Socket
If using Unix socket:
```bash # Check socket exists ls -la /var/run/redis/redis.sock
# Check permissions stat /var/run/redis/redis.sock
# Connect via socket redis-cli -s /var/run/redis/redis.sock ping ```
Verify the Fix
```bash # Test connection redis-cli ping # PONG
# Test from remote host redis-cli -h redis-server-ip -p 6379 -a password ping # PONG
# Check Redis info redis-cli info server ```
Prevention Tips
```bash # Enable Redis to start on boot systemctl enable redis
# Monitor Redis health redis-cli info stats | grep total_connections
# Set up monitoring for Redis # Use Prometheus redis_exporter ```