简体   繁体   English

socket.io-redis不断抛出NOAUTH错误

[英]socket.io-redis keeps throwing NOAUTH error

I am trying to connect to my redis server that password protected but for some reason I keep getting the error: 我正在尝试连接到受密码保护的Redis服务器,但由于某些原因,我不断收到错误消息:

events.js:141 throw er; events.js:141 throw er; // Unhandled 'error' event ^ ReplyError: Ready check failed: NOAUTH Authentication required. //未处理的“错误”事件^ ReplyError:准备检查失败:NOAUTH需要身份验证。 at parseError (/home/ubuntu/TekIT/ITapp/node_modules/redis-parser/lib/parser.js:193:12) at parseType (/home/ubuntu/TekIT/ITapp/node_modules/redis-parser/lib/parser.js:303:14) 在parseType(/ home / ubuntu / TekIT / ITapp / nodeapp / node_modules / redis-parser / lib / parser。 JS:303:14)

I know the password is correct because i tried it in the redis-cli and it works fine. 我知道密码是正确的,因为我在redis-cli中尝试过,并且可以正常工作。 Here is the code below: 这是下面的代码:

var redis = require('redis');

var client = redis.createClient(redisPort, redisHostname, { auth_pass: 'password1' });


var redisSubscriber = redis.createClient(redisPort, redisHostname, { auth_pass: 'password1' });




// Create and use a Socket.IO Redis store
var RedisStore = require('socket.io-redis');
io.set('store', new RedisStore({
    redisPub: client,
    redisSub: redisSubscriber,
    redisClient: client
}));

Does anyone know why I am getting this error? 有人知道我为什么收到此错误吗?

You should initialize socket.io-redis after ready event. 您应该在ready事件之后初始化socket.io-redis

Also you should call to client.auth('password1', ()=>{}) function. 另外,您应该调用client.auth('password1', ()=>{})函数。

Check this part of documentation: 检查文档的此部分:

When connecting to a Redis server that requires authentication, the AUTH command must be sent as the first command after connecting. 连接到需要身份验证的Redis服务器时,必须在连接后将AUTH命令作为第一个命令发送。 This can be tricky to coordinate with reconnections, the ready check, etc. To make this easier, client.auth() stashes password and will send it after each connection, including reconnections. 要与重新连接,就绪检查等配合使用,可能会很棘手。为简化此操作,client.auth()会隐藏密码,并将在每次连接(包括重新连接)后发送密码。 callback is invoked only once, after the response to the very first AUTH command sent. 在发送对第一个AUTH命令的响应后,回调仅被调用一次。 NOTE: Your call to client.auth() should not be inside the ready handler. 注意:您对client.auth()的调用不应在就绪处理程序内。 If you are doing this wrong, client will emit an error that looks something like this Error: Ready check failed: ERR operation not permitted. 如果做错了,客户端将发出类似于以下错误的错误:准备检查失败:不允许进行ERR操作。

Try this code: 试试这个代码:

var redis = require('redis');
const redisPort = 6379
const redisHostname = 'localhost'
const password = 'password1'

var p1 = new Promise((resolve, reject)=>{
    var client = redis.createClient(redisPort, redisHostname, { auth_pass: 'password1' });
    client.auth(password)
    client.on('ready', ()=>{
        resolve(client)
    })
})

var p2 = new Promise((resolve, reject)=>{
    var redisSubscriber = redis.createClient(redisPort, redisHostname, { auth_pass: 'password1' });
    redisSubscriber.auth(password)
    redisSubscriber.on('ready', ()=>{
        resolve(redisSubscriber)
    })
})

Promise.all([p1,p2]).then(([client, redisSubscriber])=>{
    console.log('done', client)
    client.ping((err, data)=>{
        console.log('err, data', err, data)
    })

    // Create and use a Socket.IO Redis store
    var RedisStore = require('socket.io-redis');
    io.set('store', new RedisStore({
        redisPub: client,
        redisSub: redisSubscriber,
        redisClient: client
    }));
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM