saurabh kackar
saurabh kackar

Reputation: 219

No connection could be made because the target machine actively refused it. [tcp://127.0.0.1:6379] on localhost using laravel 5.2

I am using Laravel 5.2 and developing an admin panel and trying to integrate Redis 1.0 in Laravel.

When I am trying to set the variable name along with value it prompting an error:

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

The configurational changes i made it is given below:

'redis' => [
    'cluster' => true,
    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],
]

Below is my Controller code:

<?php

namespace App\Http\Controllers\Administrator;

use Redis;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MyController extends Controller
{
    public function __construct() {}

    public function myProfile(Request $request)
    {
        $redis = Redis::connection();
        $adminName = $request->route('admin_name');
        if ($redis) {
            echo 'connection done';
        } else {
            echo 'connection not done';
        }
        Redis::set('name', $adminName);
        echo $redis->get('name');
    }
}

Please help if anyone have an idea of it.

Upvotes: 22

Views: 45053

Answers (2)

Ramin Axundbeyli
Ramin Axundbeyli

Reputation: 1

  1. Install Redis from here (https://github.com/MicrosoftArchive/redis/releases)

  2. configure config/database.php

  3. Check redis with this code block if working normaly

     try {
     // Create a Redis Instance
     $redis = new \Redis();
    
     // Try to connect to a redis server
     // In this case within the host machine and the default port of redis
     $redis->connect('127.0.0.1', 6379);
    
     // Define some Key
     $redis->set('user', 'sdkcarlos');
    
     // Obtain value
     $user = $redis->get('user');
    
     // Should Output: sdkcarlos
     print($user);
    

    } catch (Exception $ex) { echo $ex->getMessage(); }

Upvotes: 0

Bibhudatta Sahoo
Bibhudatta Sahoo

Reputation: 4904

I think you are not started the Redis server that's why this error is coming.

Just download and install the Redis server For download the sever just redis server install

For windows 64 go here https://github.com/MicrosoftArchive/redis/releases and
=>Download Redis-x64-3.2.100.msi file
=>Run it and install it
=>Then open the Redis folder (it will the installed folder) then run "redis-server.exe" file.
=>Now your Redis server is activated
=>You can check this by run "redis-cli.exe" file and type ping it will be pong.

Upvotes: 66

Related Questions