解决Laravel 连接Reids报错问题: `AUTH` failed: ERR Client sent AUTH, but no password is set

Larave 连接 Redis 集群的配置及使用

配置文件:

config/database.php

配置项如下:

'redis' => [
     'client' => 'predis',
      //单个Reis连接(非集群)
     'default' => [
         'host' => env('REDIS_HOST', '127.0.0.1'),
         //如果redis没有密码,则填 null 不要用空字符串'',否则连接失败
         'password' => env('REDIS_PASSWORD', null),
         'port' => env('REDIS_PORT', 6379),
         'database' => 0,
     ],
     'options' => [
         'cluster' => 'redis',
     ],
     //集群连接
     'clusters' => [
         'cluster1' => [
             [
             	 // 我这里是在redis容器(172.200.0.2)内部执行redis-cli --cluster建立的redis集群,并且容器的6379端口映射到了172.17.0.1的端口6379,所以我这里填 172.17.0.1
                 'host' => '172.17.0.1',
                 //如果redis没有密码,则填 null 不要用空字符串'',否则连接失败
                 'password' => null,
                 'port' => 6379,
                 'database' => 0,
             ],
         ],
         //集群2
         'cluster2' => [
             [
                 'host' => '192.168.31.244',
                 //如果redis没有密码,则填 null 不要用空字符串'',否则连接失败
                 'password' => null,
                 'port' => 6379,
                 'database' => 0,
             ],
         ],
     ],
 ],

使用 Redis:

//连接集群2(cluster2):Redis::connection('cluster2');
//连接redis(default):Redis::connection('default');
$redis = Redis::connection('cluster1');
$redis->set('username','wdh');
echo $redis->get('username');

相关参考:

1. laravel框架5.5 连接redis和redis集群:https : // www . cn blogs . com /liaokaichang/p/8874808.html
2. Laravel redis集群:https : // www . cnblogs . com /yinguohai/p/11329273.html
发布了118 篇原创文章 · 获赞 17 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/one312/article/details/105003882
今日推荐