Laravel : Redis No connection could be made : [tcp

2020-08-09 10:56发布

I have installed redis with laravel by adding "predis/predis":"~1.0",

Then for testing i added the following code :

public function showRedis($id = 1)
   {
      $user = Redis::get('user:profile:'.$id);
      Xdd($user);
   } 

In app/config/database.php i have :

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

    ],

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

I using virtualhost for the project. Using Xampp with windows.

3条回答
Explosion°爆炸
2楼-- · 2020-08-09 11:25

First make sure Redis is actually listening on that port by opening up powershell and typing netstat -aon | more (this command may need to be enabled in features or installed somehow). If it is listening then check your firewall settings. If if not listening then make sure Redis is started and is configured to listen on that port.

It looks like predis/predis is a client library only. See https://packagist.org/packages/predis/predis.

You need to install the Redis server, but it looks like it is not officially supported on Windows. See http://redis.io/download. Per information on that page, it looks like there is a Win64 port for Redis here https://github.com/MSOpenTech/redis.

If it still doesn't work after that then update your question with the new error you see.

查看更多
姐就是有狂的资本
3楼-- · 2020-08-09 11:29

Ref solution: https://rapidsol.blogspot.com/2018/10/php-fatal-error-uncaught.html

It is showing your server is not accepting connections from outside. You need to provide ip of your redis server.

$client = new Predis\Client('tcp://192.168.1.103:6379');
//$client = new Predis\Client();
$client->set('foo', 'bar');
$value = $client->get('foo');
echo $value; exit;

if problem still come then try below steps.

So you need to edit : $sudo vi /usr/local/etc/redis.conf

and find the line bind 127.0.0.1 ::1 and change it to #bind 127.0.0.1 ::1 and then find line protected-mode yes and then change it to protected-mode no

and then restart the redis server

查看更多
在下西门庆
4楼-- · 2020-08-09 11:46

I had this issue in Ubuntu 18.04

I installed redis in my local system, got solved.

sudo apt-get install redis-server
查看更多
登录 后发表回答