davy_yg's avatar
Level 27

Redis - No connection could be made because the target machine actively refused it

No connection could be made because the target machine actively refused it [tcp://127.0.0.1:6379]

	<?php

	namespace App\Http\Controllers;

	use Illuminate\Http\Request;
	use Illuminate\Support\Facades\Redis;

	class RedisController extends Controller
	{
		//
		public function index()
		{
    	Redis::set('user:1:first_name', 'Mike');
    	Redis::set('user:2:first_name', 'John');
    	Redis::set('user:3:first_name', 'Kate');

    	for ($i=0; $i < 3; $i++){
        	echo Redis::get('user:'. $i .':first_name');
    	}
	}

	}

Any idea why?

0 likes
1 reply
LaryAI's avatar
Level 58

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

  1. Start Redis server.
  2. Check .env for correct host/port.
  3. Ensure no firewall is blocking port 6379.
  4. Install PHP Redis extension or Predis.
  5. 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!

Please or to participate in this conversation.