Getting the "Redis connection refused 127.0.0.1:6379" error in your Node.js, Python, or Java application? This complete guide shows you how to start Redis server and fix connection issues on Windows, macOS, and Linux.
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
Unhandled error event: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1144:16)
Error: Redis connection failed: ECONNREFUSED 127.0.0.1:6379
redis.exceptions.ConnectionError: Error 111 connecting to 127.0.0.1:6379. Connection refused. Traceback (most recent call last): File "app.py", line 5, inr = redis.Redis(host='localhost', port=6379) redis.exceptions.ConnectionError: Connection refused
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
The Problem: Redis server is not running on port 6379
redis-serverredis-cli ping (should return PONG)brew services start redisredis-cli ping (should return PONG)sudo systemctl start redisredis-cli ping (should return PONG)ECONNREFUSED on port 6379 means:
# Method 1: Check if Redis process is running tasklist | findstr redis # Method 2: Check if port 6379 is listening netstat -an | findstr :6379 # Method 3: Try connecting with redis-cli redis-cli ping
Expected Output if Running:
redis-server.exe 5432 Console 1 45,678 K TCP 0.0.0.0:6379 0.0.0.0:0 LISTENING PONG
# Method 1: Using Homebrew services brew services list | grep redis # Method 2: Check process ps aux | grep redis-server # Method 3: Check if port 6379 is listening lsof -i :6379 # Method 4: Try connecting redis-cli ping
Expected Output if Running:
redis started username ~/Library/LaunchAgents/homebrew.mxcl.redis.plist redis-ser 12345 user 6u IPv4 0x... TCP localhost:6379 (LISTEN) PONG
# Method 1: Using systemctl sudo systemctl status redis # Method 2: Check process ps aux | grep redis-server # Method 3: Check if port 6379 is listening sudo lsof -i :6379 # or sudo netstat -tulpn | grep :6379 # Method 4: Try connecting redis-cli ping
Expected Output if Running:
● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled) Active: active (running) since Mon 2024-01-15 10:30:00 UTC PONG
Method 1: Manual Start (Foreground)
# Navigate to Redis directory (adjust path) cd C:\Program Files\Redis # Start Redis server redis-server # Or specify config file redis-server redis.windows.conf
Method 2: Install as Windows Service
# Run as Administrator cd C:\Program Files\Redis # Install Redis as service redis-server --service-install redis.windows.conf # Start the service redis-server --service-start # Check service status sc query Redis
Method 1: Using Homebrew Services (Recommended)
# Start Redis and run at login (persistent) brew services start redis # Or just start once (stops on logout/restart) brew services run redis # Check status brew services list
Method 2: Manual Start (Foreground)
# Start Redis manually (runs in foreground) redis-server # Or with config file redis-server /opt/homebrew/etc/redis.conf
# Install Redis via Homebrew brew install redis # Then start it brew services start redis
Using systemctl (Ubuntu/Debian/CentOS/RHEL)
# Start Redis sudo systemctl start redis # Enable auto-start on boot sudo systemctl enable redis # Check status sudo systemctl status redis # Restart Redis sudo systemctl restart redis
Manual Start (if systemctl not available)
# Start Redis manually redis-server # Or with config file redis-server /etc/redis/redis.conf
# Ubuntu/Debian sudo apt-get update sudo apt-get install redis-server # CentOS/RHEL sudo yum install redis # Then start it sudo systemctl start redis
After starting Redis, confirm it's actually listening on port 6379.
# Windows netstat -an | findstr :6379 # macOS/Linux lsof -i :6379 # or netstat -an | grep 6379
Expected Output:
# You should see Redis listening on 6379: TCP 0.0.0.0:6379 0.0.0.0:0 LISTENING TCP [::]:6379 [::]:0 LISTENING # Or with lsof: redis-ser 12345 user 6u IPv4 0x... TCP localhost:6379 (LISTEN)
/etc/redis/redis.conf (Linux), /opt/homebrew/etc/redis.conf (macOS), or redis.windows.conf for the port setting.
# Test basic connection redis-cli ping # Should return: PONG # Connect to Redis CLI redis-cli # Try some commands > SET test "hello" > GET test > QUIT
Node.js / ioredis:
const Redis = require('ioredis');
const redis = new Redis({
host: 'localhost',
port: 6379,
// password: 'yourpassword', // if required
});
redis.ping()
.then(() => console.log('✓ Redis connected successfully'))
.catch(err => console.error('✗ Redis connection error:', err.message));
Node.js / node-redis (v4+):
const redis = require('redis');
const client = redis.createClient({
socket: {
host: 'localhost',
port: 6379
}
});
client.connect()
.then(() => client.ping())
.then(() => console.log('✓ Redis connected successfully'))
.catch(err => console.error('✗ Redis connection error:', err.message));
Python / redis-py:
import redis
try:
r = redis.Redis(
host='localhost',
port=6379,
# password='yourpassword', # if required
decode_responses=True
)
r.ping()
print('✓ Redis connected successfully')
except Exception as e:
print(f'✗ Redis connection error: {e}')
Check Redis configuration file for connection settings.
# Find redis.conf location: # Linux: /etc/redis/redis.conf # macOS: /opt/homebrew/etc/redis.conf # Windows: C:\Program Files\Redis\redis.windows.conf # Important settings to check: bind 127.0.0.1 # Allow connections from localhost port 6379 # Default Redis port protected-mode yes # Enable protected mode (requires password or bind)
protected-mode no (not recommended for production)requirepass yourpasswordbind directive to allow specific IPsSymptoms: redis-cli ping works, but your app gets connection refused
Cause: Application is trying to connect from different host or protected mode is enabled
Solutions:
# Check Redis bind address grep bind /etc/redis/redis.conf # If it shows: bind 127.0.0.1 # This means Redis only accepts local connections # Solution 1: Disable protected mode (development only) # Edit redis.conf: protected-mode no # Solution 2: Set password (recommended) requirepass yourpassword # Then restart Redis sudo systemctl restart redis
Check which port Redis is actually using:
# Check redis.conf for port setting grep port /etc/redis/redis.conf # Check which ports Redis is listening on sudo lsof -i -P | grep redis # or sudo netstat -tulpn | grep redis
If Redis is on a different port, update your connection string to match.
Allow port 6379 through firewall:
# Windows - Allow port 6379 netsh advfirewall firewall add rule name="Redis" dir=in action=allow protocol=TCP localport=6379 # Linux (Ubuntu/Debian) - Allow port 6379 sudo ufw allow 6379/tcp # macOS - Check firewall settings # System Preferences → Security & Privacy → Firewall
If using Redis in Docker:
# Check if Redis container is running docker ps | grep redis # If not running, start it docker start redis-container # Or run a new Redis container docker run -d -p 6379:6379 --name redis redis:latest # Check container logs docker logs redis-container # Connect to Redis in Docker docker exec -it redis-container redis-cli
Docker Compose example:
version: '3.8'
services:
redis:
image: redis:latest
ports:
- "6379:6379"
restart: always
volumes:
- redis-data:/data
volumes:
redis-data:
REDIS SERVICE COMMANDS BY OS ============================ Windows: Check: tasklist | findstr redis Start: redis-server Service: redis-server --service-start CLI: redis-cli macOS (Homebrew): Check: brew services list | grep redis Start: brew services start redis Stop: brew services stop redis Restart: brew services restart redis Linux (systemd): Check: sudo systemctl status redis Start: sudo systemctl start redis Stop: sudo systemctl stop redis Enable: sudo systemctl enable redis Restart: sudo systemctl restart redis VERIFY REDIS ============ Port check: lsof -i :6379 (Mac/Linux) | netstat -an | findstr :6379 (Win) Test connect: redis-cli ping (should return PONG) Connection URL: redis://localhost:6379
Debugging checklist:
Visit our Interactive Diagnostic Wizard for personalized help.