Error: could not connect to redis at 127.0.0.1:6379: connection refused
Solution
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:
CODE_BLOCK_PLACEHOLDER_0
If it's not running, start it:
CODE_BLOCK_PLACEHOLDER_1
- Verify Redis is listening on the correct port:
CODE_BLOCK_PLACEHOLDER_2
You should see an entry for 127.0.0.1:6379
- Check Redis configuration:
Open the Redis configuration file:
CODE_BLOCK_PLACEHOLDER_3
Ensure these lines are present and uncommented:
```
bind 127.0.0.1
port 6379
```
- Test Redis connection:
CODE_BLOCK_PLACEHOLDER_5
If it returns "PONG", Redis is running and accessible
- Check firewall settings:
If using UFW:
CODE_BLOCK_PLACEHOLDER_6
If it's active, allow Redis:
CODE_BLOCK_PLACEHOLDER_7
- Restart Redis after changes:
CODE_BLOCK_PLACEHOLDER_8 - 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:
CODE_BLOCK_PLACEHOLDER_10 - Keep Redis updated to the latest stable version
If the problem persists after these steps, check Redis logs for more detailed error information:
CODE_BLOCK_PLACEHOLDER_11
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.exceptions.responseerror noauth authentication required
- 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
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
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