【问题标题】:Node-Redis: ready check failed - NOAUTH Authentication requiredNode-Redis:就绪检查失败 - 需要 NOAUTH 身份验证
【发布时间】:2018-01-22 04:15:05
【问题描述】:

我有一个奇怪的 redis 行为:

const redis = require('redis');
const { REDIS_URL: redisUrl, REDIS_PASSWORD: redisPassword } = process.env;

const client = redis.createClient(redisUrl, {
  no_ready_check: true,
  auth_pass: redisPassword
});

client.on('connect', () => {
  redisPassword && client.auth(redisPassword);
});

client.on('error', err => {
  global.console.log(err.message)
});

但我一直收到以下错误:

投掷者; // 未处理的“错误”事件

ReplyError:就绪检查失败:需要 NOAUTH 身份验证。

为什么未处理?我设置 onerror 处理程序
为什么就绪检查失败?我在选项中禁用

【问题讨论】:

    标签: node.js redis node-redis


    【解决方案1】:

    我不确定你的代码为什么会抛出这个错误。但是我在本地机器上尝试了这段代码,效果很好。

    const redis = require('redis');
    const redisPassword = "password" ; 
    const client = redis.createClient({
              host : '127.0.0.1',  
              no_ready_check: true,
              auth_pass: redisPassword,                                                                                                                                                           
    });                               
                                      
    client.on('connect', () => {   
              global.console.log("connected");
    });                               
                                      
    client.on('error', err => {       
              global.console.log(err.message)
    });                               
                                      
    client.set("foo", 'bar');         
                                      
    client.get("foo", function (err, reply) {
            global.console.log(reply.toString())
    })
    

    运行 node client.js 会输出:

    已连接

    酒吧

    NOAUTH Authentication required 是由于 redis process 命令时发现客户端未通过身份验证,因此抱怨它。

    我猜你给 createClient 的 redisUrl 可能有问题,尝试调试它或更改为我的代码的方式尝试。希望你能解决它。

    还有一件事:client.auth(redisPassword) 不是必需的,因为如果您设置 auth_pass 或密码选项,redis 客户端将在任何命令之前自动向服务器发送 auth 命令。

    【讨论】:

    • 感谢您提供有关 auth_pass 选项的建议。这很有用。至于问题 - 我用 redis 重新创建了一个实例(我们在它的引擎盖下使用 flynn 和 heroku)现在它可以工作了
    • 您好,我遵循您的相同代码,但它仍然向我发送相同的错误。但我意识到它在我的数据库中创建了密钥,但它不允许运行我的应用程序。我能做些什么? Mi错误如下:ReplyError:就绪检查失败:NOAUTH需要身份验证。在 parseError (/home/mauricio/Documents/lisapp/pos_lisa/node_modules/redis-parser/lib/parser.js:193:12) 在 parseType (/home/mauricio/Documents/lisapp/pos_lisa/node_modules/redis-parser/ lib/parser.js:303:14)
    • 你的redis服务器配置对了吗? requirepass 和保护模式
    【解决方案2】:

    如果您将 redis uri 保存为字符串。您需要将其分解为对象。对于ioredis,您可以使用函数

    export function decomposeRedisUrl(url) {
      const [[, , password, host, port]] = [...(url.matchAll(/redis:\/\/(([^@]*)@)?(.*?):(\d*)/g))];
      return { password, host, port };
    }
    

    这个功能有测试:

    it("redis url should be decomposed correctly with password", () => {
      expect(decomposeRedisUrl("redis://pass@host.com:9183")).to.eql({
        password: "pass",
        host: "host.com",
        port: "9183",
      });
    });
    
    it("redis url should be decomposed correctly without password", () => {
      expect(decomposeRedisUrl("redis://localhost:6379")).to.eql({
        password: undefined,
        host: "localhost",
        port: "6379",
      });
    });
    

    和用法

    import Redis from "ioredis";
    
    async function getKeysFromRedisUrl(url) {
      const rc = new Redis(decomposeRedisUrl(url));
      const keys = await rc.keys("*");
      rc.disconnect();
      return keys;
    }
    
    describe("Redis can connect", () => {
      it("with cloud", async () => {
        expect(await getKeysFromRedisUrl("redis://pass@host.com:9183")).to.be.an("array");
      });
      it("with local redis instance", async () => {
        expect(await getKeysFromRedisUrl("redis://localhost:6379")).to.be.an("array");
      });
    });
    
    • 此函数未处理用户名

    【讨论】:

      猜你喜欢
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 2017-02-18
      • 2019-06-11
      • 2020-02-22
      • 2016-01-19
      • 1970-01-01
      相关资源
      最近更新 更多