Dragonfly Cloud announces new enterprise security features - learn more

Error: could not connect to redis at 127.0.0.1:6379: connection refused

Understanding the "could not connect to redis at 127.0.0.1:6379: connection refused" error

This error occurs when your application fails to establish a connection with Redis at the default localhost address and port. It usually indicates that the Redis server is not running or is inaccessible.

Root Causes

  1. Redis server is not running
  2. Redis is running on a different port
  3. Firewall blocking the connection
  4. Redis configured to listen on a different interface

Solution - How to Resolve It

Follow these steps to resolve the Redis connection error:

  1. Check if Redis is running:
sudo systemctl status redis

If it's not running, start it:

sudo systemctl start redis
  1. Verify Redis is listening on the correct port:
sudo netstat -lnp | grep redis

You should see an entry for 127.0.0.1:6379

  1. Check Redis configuration: Open the Redis configuration file:
sudo nano /etc/redis/redis.conf

Ensure these lines are present and uncommented:

bind 127.0.0.1
port 6379
  1. Test Redis connection:
redis-cli ping

If it returns "PONG", Redis is running and accessible

  1. Check firewall settings: If using UFW:
sudo ufw status

If it's active, allow Redis:

sudo ufw allow 6379
  1. Restart Redis after changes:
sudo systemctl restart redis
  1. Verify in your application code: Ensure you're using the correct host and port in your Redis connection string:
redis://127.0.0.1:6379

Prevention

  • Regularly monitor Redis status with system monitoring tools
  • Set up automatic Redis startup on system boot:
sudo systemctl enable redis
  • Keep Redis updated to the latest stable version

If the problem persists after these steps, check Redis logs for more detailed error information:

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

Was this content helpful?

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost