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
- Redis server is not running
- Redis is running on a different port
- Firewall blocking the connection
- Redis configured to listen on a different interface
Solution - How to Resolve It
Follow these steps to resolve the Redis connection error:
- Check if Redis is running:
sudo systemctl status redis
If it's not running, start it:
sudo systemctl start redis
- Verify Redis is listening on the correct port:
sudo netstat -lnp | grep redis
You should see an entry for 127.0.0.1:6379
- 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
- Test Redis connection:
redis-cli ping
If it returns "PONG", Redis is running and accessible
- Check firewall settings: If using UFW:
sudo ufw status
If it's active, allow Redis:
sudo ufw allow 6379
- Restart Redis after changes:
sudo systemctl restart redis
- 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?
Other Common Redis Errors (with Solutions)
- redis error server closed the connection
- redis.exceptions.responseerror: value is not an integer or out of range
- redis.exceptions.responseerror moved
- redis-server failed to start advanced key-value store
- spring boot redis unable to connect to localhost 6379
- unable to configure redis to keyspace notifications
- redis.clients.jedis.exceptions.jedismoveddataexception
- could not get resource from pool redis
- failed to restart redis service unit redis service not found
- job for redis-server.service failed because a timeout was exceeded
- failed to start redis-server.service unit redis-server.service is masked
- failed to stop redis-server.service unit redis-server.service not loaded
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