Laravel连接Redis报错

报错:

错误1:MOVED 14315 172.200.0.1:6393
  • 示例:
Predis \ Response \ ServerException
MOVED 14315 172.200.0.1:6393
  • 问题原因:需要用集群模式连接Redis集群,例如,使用如下配置中的mycluster1连接.
#如果是用 redis-cli -h 172.17.0.1 -p 6379命令连接redis报此错误错,则在命令中加-c参数即可,-c表示用集群模式连接。
redis-cli -c -h 172.17.0.1 -p 6379
错误2:No connections available in the pool
  • 示例:
Predis \ ClientException
No connections available in the pool
  • 问题原因:redis集群连接配置不正确
错误3:Redis connection [mycluster1] not configured.
  • 示例:
InvalidArgumentException
Redis connection [mycluster1] not configured.
  • 问题原因:没有名称为mycluster1的集群配置
错误4:No connections left in the pool for CLUSTER SLOTS
  • 示例:
Predis \ ClientException
No connections left in the pool for `CLUSTER SLOTS`

正确的使用示例:

配置示例:
'redis' => [
    'client' => 'predis',
    'default' => [
        'host' => env('REDIS_HOST', '172.17.0.1'),
        'password' => env('REDIS_PASSWORD', null),//无密码要填null
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],
    'options' => [
        'cluster' => 'redis',
    ],
    'clusters' => [
        //集群1
        'cluster1' => [
            [
                'host' => '172.17.0.1',
                'password' => null,//无密码要填null
                'port' => 6379,
                'database' => 0,
            ],
        ],
        //集群2
        'cluster2' => [
            [
                'host' => '192.168.31.244',
                'password' => null,//无密码要填null
                'port' => 6379,
                'database' => 0,
            ],
        ],
    ],
],
正确的连接示例:
$redis = Redis::connection('cluster1');
$redis->set('username','wdh');
echo $redis->get('username');

其他参考:

1.Laravel redis集群:https : // www . cnblogs . com /yinguohai/p/11329273.html
发布了118 篇原创文章 · 获赞 17 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/one312/article/details/105007020