踩坑 AUTH password called without any password

时间:2025-03-02 09:09:13

问题描述

哨兵模式部署 redis 服务,本地使用 redis-py 客户端连接,抛异常:

: No master found for 'mymaster' 

或者

: AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?

已确定 ”mymaster“ 配置没问题。

原因

原因很简单,报错信息翻译过来就是:默认用户不需要认证密码,确定你的配置是正确的吗?刚开始一直以为是客户端连接时没有提供密码,实际上是本来不需要密码,但是我们连接时给人家提供了。

password = "redis_pwd"
host = "localhost"

sentinel = Sentinel([
        (host, 26379),
        (host, 26380),
        (host, 26381),
  ], sentinel_kwargs={'password': password}, password=password)

conn = sentinel.master_for("mymaster")
conn.set('mykey', 'myvalue')
result = conn.get('mykey')

print(result)

参数:sentinel_kwargs 用来配置哨兵的密码。password 用来配置 redis 密码。

解决办法

也很简单。确认下部署redis 时都给谁配置密码了。我遇到的就是 redis 有密码,哨兵没有配置密码。

上述代码稍稍改下就 work 了

password = "redis_pwd"
host = "localhost"

sentinel = Sentinel([
        (host, 26379),
        (host, 26380),
        (host, 26381),
  ],password=password)

conn = sentinel.master_for("mymaster")
conn.set('mykey', 'myvalue')
result = conn.get('mykey')

print(result)

坑死!