2017-08-14 322 views
0

我有一个奇怪的Redis行为:节点的Redis:准备检查失败 - NOAUTH需要验证

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; // Unhandled 'error' event

ReplyError: Ready check failed: NOAUTH Authentication required.

为什么未处理?我错误处理程序
为什么准备检查失败?我已禁用它在选项

回答

0

我不知道为什么你的代码会抛出这个错误。但我在我的本地机器上试用这个代码,它运行良好。

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()) 
}) 

和运行节点client.js将输出:

connected

bar

NOAUTH需要验证通过时redis的处理命令时,它发现客户端没有通过认证,因此与它抱怨引起的。

我想也许你给createClient的redisUrl有一些问题,试着去调试它,或者改成我的代码的方式来尝试。希望你能解决它。

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

+0

感谢您使用auth_pass选项的建议。这很有用。至于这个问题 - 我用redis重新创建了一个实例(我们在它的引擎盖下使用了flynn和heroku)现在它起作用了 –