The error message:
No connection could be made because the target machine actively refused it [tcp://127.0.0.1:6379]
means your Laravel application is trying to connect to Redis on 127.0.0.1:6379, but it can't reach the Redis server. Here are the most common reasons and solutions:
1. Redis Server is Not Running
Make sure Redis is installed and running on your machine.
For Windows (if using Redis via WSL or natively): Open a terminal and run:
redis-server
For macOS (Homebrew):
brew services start redis
For Linux:
sudo systemctl start redis
or
redis-server
2. Redis is Running on a Different Host or Port
Check your .env file for these lines:
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
Make sure the host and port match where your Redis server is running.
3. Firewall or Security Software Blocking Connection
If you have a firewall or antivirus, make sure it allows connections to port 6379.
4. Redis Extension/Package Not Installed
Make sure you have the PHP Redis extension or predis/predis package installed.
For PHP Redis extension:
php -m | grep redis
If not installed:
pecl install redis
and enable it in your php.ini.
For Predis:
composer require predis/predis
and set in config/database.php:
'client' => env('REDIS_CLIENT', 'predis'),
5. Code Correction
Also, your loop should start from 1 (since you set keys for users 1, 2, and 3):
for ($i = 1; $i <= 3; $i++) {
echo Redis::get('user:' . $i . ':first_name');
}
Summary Checklist
- Start Redis server.
- Check
.envfor correct host/port. - Ensure no firewall is blocking port 6379.
- Install PHP Redis extension or Predis.
- Fix your loop index.
Example: Starting Redis and checking connection
# Start Redis server
redis-server
# Test connection (from terminal)
redis-cli ping
# Should return: PONG
If you follow these steps, the error should be resolved!