Quick answer: Start Redis: sudo systemctl start redis or redis-server /etc/redis/redis.conf. If it still fails, check bind 127.0.0.1 and protected-mode yes in the config.
This error – Error: connect ECONNREFUSED 127.0.0.1:6379 – means your client sent a TCP handshake to Redis but got back a RST (reset) packet. The server isn't listening on that port. Usually that's because Redis isn't started, or it's started but bound to a different interface (like localhost only when you're connecting from the same box, or the reverse). I've seen this mostly on fresh Ubuntu 22.04 installs where Redis is installed but the systemd service is disabled. Also happens when you run multiple Redis instances and forget which port is which.
Fix Steps
- Check if Redis is running
Runsudo systemctl status redisorps aux | grep redis-server. If it's not there, start it:sudo systemctl start redis. For the background: systemd may be installed but the service unit file might be missing on some minimal Docker images – then useredis-server &to start it directly. - Verify the port and bind address
Open/etc/redis/redis.confand look forbindandport. Default isbind 127.0.0.1andport 6379. If you're connecting from the same machine, that's fine. But if you setbind 0.0.0.0and still get refused, it's probablyprotected-mode. Also see ifportis commented out – that'll make Redis use 0, meaning no TCP port. - Disable protected mode for remote access
If you need to connect from another host, setprotected-mode noin the config. The reason: protected mode only allows connections from localhost if no password is set. Without this, Redis will reject remote connections even ifbind 0.0.0.0. - Restart Redis
After any config change:sudo systemctl restart redisorredis-server /etc/redis/redis.conf. Verify withredis-cli ping– you should seePONG. - Check firewall
If connecting remotely, runsudo ufw status(oriptables -L). Port 6379 must be open. On a cloud server, also check the security group rules.
Alternative Fixes If the Main Steps Don't Work
- Try a different client IP
If you're connecting via a Docker container, usehost.docker.internalon Mac/Windows or172.17.0.1on Linux. Reason: inside Docker,127.0.0.1is the container's own loopback, not the host's. - Manually start Redis in foreground
Runredis-server --port 6379 --bind 0.0.0.0directly in terminal. If it starts, the issue is with your config file or service unit. If it fails, you'll see the exact error message likeFatal error, can't open config fileorAddress already in use. - Kill stale Redis process
Sometimes a zombie process holds the port.sudo lsof -i :6379shows the PID. Thensudo kill -9 <PID>and restart. - Clear Redis socket file
If you use Unix socket (path like/tmp/redis.sock), delete it manually:sudo rm /tmp/redis.sock. This helps when Redis crashes and leaves a stale socket.
Prevention Tip
Add a simple health check in your deployment script or cron job: redis-cli ping > /dev/null 2>&1 || systemctl restart redis. Also set logfile /var/log/redis/redis.log in config – when it fails again, check that log. The line # Server started, Redis version 7.2.4 confirms a clean start. If you see Can't bind: Address already in use, you've got a port conflict.
One more tip: always test the connection with redis-cli from the same machine your app runs on. That isolates network issues from config issues. I've spent hours debugging only to find the app was using a different Redis hostname than localhost.