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. 1.Redis not running - Server process stopped or crashed
  2. 2.Wrong port - Redis listening on different port
  3. 3.Wrong host - Connecting to wrong IP address
  4. 4.Bind address - Redis only listening on specific interface
  5. 5.Firewall blocking - Port blocked by iptables or firewall
  6. 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:

bash
# Start Redis
systemctl start redis
# Or:
redis-server /etc/redis/redis.conf

Step 2: Check Redis Port

bash
# Check what port Redis is listening on
netstat -tlnp | grep redis
# Or:
ss -tlnp | grep redis

Output:

bash
tcp  0  0 127.0.0.1:6379  0.0.0.0:*  LISTEN  1234/redis-server

If 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

bash
# Check Redis bind address
grep "^bind" /etc/redis/redis.conf

Default:

bash
bind 127.0.0.1

If 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:

bash
systemctl restart redis

Step 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

bash
# Check Redis logs
tail -f /var/log/redis/redis-server.log
# Or:
journalctl -u redis -f

Look 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:

bash
redis-cli ping
# DENIED Redis is running in protected mode

Fix 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:

bash
redis-cli -a your_strong_password ping

Or disable protected mode (not recommended for production):

bash
protected-mode no

Step 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 ```