Node-Redis: 准备检查失败 - 需要进行NOAUTH身份验证。

16

我有一个奇怪的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)
});

但我一直收到以下错误:

throw er; // 未处理的 'error' 事件

ReplyError:准备检查失败:需要进行身份验证 NOAUTH。

为什么会是未处理的?我已经设置了错误处理程序。
为什么会是准备检查失败?我在选项中禁用它了。

4个回答

19

我不确定为什么您的代码会抛出这个错误,但是我在我的本地机器上尝试了这段代码,它可以正常工作。

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 将输出:

已连接

bar

当 Redis 处理命令时,如果发现客户端未经过身份验证,则会抛出“NOAUTH Authentication required”的错误。

我猜测可能是您提供给 createClient 的 redisUrl 存在问题,请尝试进行调试或更改到我的代码方式进行尝试。希望您可以解决它。

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


谢谢您提供关于auth_pass选项的建议,非常有用。至于问题 - 我重新创建了一个带有Redis的实例(我们在其底层使用Flynn和Heroku),现在它可以正常工作了。 - Enthusiastic Developer
你好,我按照你的代码操作,但是仍然出现了相同的错误。但是我发现它在我的数据库中创建了密钥,但是不允许运行我的应用程序。我该怎么办?我的错误信息如下:ReplyError: Ready check failed: NOAUTH Authentication required. at parseError (/home/mauricio/Documents/lisapp/pos_lisa/node_modules/redis-parser/lib/parser.js:193:12) at parseType (/home/mauricio/Documents/lisapp/pos_lisa/node_modules/redis-parser/lib/parser.js:303:14) - maoooricio
1
你是否正确配置了 Redis 服务器?请注意 requirepass 和 protected-mode。 - GuangshengZuo

1
我使用了client.connect().then进行连接,并在redis.createClient选项中使用了urluserpassword。 这对我起作用了 update user and password

版本

node: v18.16.0 和 redis: 4.6.7(在package.json中)

代码

import redis from 'redis'
import { log } from 'console'

const client = redis.createClient({
  url: 'redis://localhost/',
  username: 'default',
  password: 'password',
})

client.connect().then(()=> {
  log('Success!')
})

await client.set(id, 'gfg')
log(await client.get(id))

0

如果您正在使用Docker运行Redis,请检查您的docker-compose文件是否有command: redis-server --requirepass redis

然后检查您的.env文件以确保您正在使用它。 这是问题所在,我通过在.env文件中添加密码来解决了它。


你能详细说明一下吗? - undefined

0
如果您将 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");
  });
});
  • 此函数未处理用户名

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接