【问题标题】:Node.js and Redis AuthNode.js 和 Redis 身份验证
【发布时间】:2022-01-04 17:06:00
【问题描述】:

我没有获得使用 redis auth 的 redis node.js 文档。

每个例子:

var redis = require("redis"),
client = redis.createClient();

// This command is magical. Client stashes the password and will issue on every connect.
client.auth("somepass");

在我的代码中,我有以下内容:

var redis = require("redis");
r = redis.createClient(6379,'xxx.xxx.xxx.xxx');
r.auth("yyyyyyyy");

app.get('/', function(req, res){
r.set("foo", 'bar');
res.writeHead(200, {'Content-Type': 'image/gif'});
res.end('Hello');
 });

这是我得到的错误:

    Express server listening on port 8070 in development mode

/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:468
                throw callback_err;
                      ^
Error: Auth error: Error: ERR Client sent AUTH, but no password is set
    at Command.callback (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:163:43)
    at RedisClient.return_error (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:464:25)
    at HiredisReplyParser.<anonymous> (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:253:14)
    at HiredisReplyParser.emit (events.js:67:17)
    at HiredisReplyParser.execute (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/lib/parser/hiredis.js:41:18)
    at RedisClient.on_data (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:440:27)
    at Socket.<anonymous> (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:70:14)
    at Socket.emit (events.js:67:17)
    at TCP.onread (net.js:367:14)
[7]+  Killed                  node app.js 8070

那么,正确的身份验证方法是什么?

【问题讨论】:

  • EADDRINUSE 是一个 http 的东西 - 你确定这是 redis 的问题而不是设置你的服务器吗?
  • 我遇到了同样的问题,我发现我在另一台服务器上编辑了配置以要求输入密码,而我在登台服务器上运行的服务器没有requirepass $uP3rs3cretpassW0rd。 ..跛脚!但现在一切正常。

标签: node.js redis


【解决方案1】:

此问题的另一个原因可能是在启动 redis-server 时未指定您的 redis.conf(如果您在其中输入“requirepass”)。

在这种情况下,redis 服务器以默认配置启动(即未启用“requirepass”选项),因此不接受您的 AUTH 命令。

./src/redis-server redis.conf

【讨论】:

    【解决方案2】:

    这是使用auth的正确方法。

    我相信您正在尝试使用旧版本的 Redis 服务器,其协议不受 node_redis 支持(反之亦然)。

    或者,您可能没有连接到您认为受密码保护的实例,或者您没有在目标实例的配置中设置密码。

    我建议你尝试使用 redis-cli 连接到实例并使用 auth 来测试身份验证(即绕过 node.js)。

    【讨论】:

    • 大家好,我用的是redis2.6。我还使用与身份验证一起使用的 py-redis。 Auth 适用于 node.js redis mranney 的所有其他客户端。甚至 tornadoredis 也有效。
    • 它对我来说很好用。您的 node.js 和相关模块(即 npm 列表)的版本是什么?我的是 v0.6.16,带有 redis@0.7.2 和hiredis@0.1.14 ...
    • 节点为 v0.6.18。 redis 是 redis@0.7.2
    【解决方案3】:

    较新版本的 Redis 默认启用保护模式,仅接受环回和 unix 域套接字。

    要从外部连接,请在 redis.conf 中绑定到 bind publicip anotherip etc requirepass foobared。现在使用redis-cli ping进行测试。
    如果没有requirepass 任何人都可以FLUSHALL :)

    启用防火墙以过滤端口访问以进一步保护,另请参阅quickstart security notes.

    您可以(暂时)使用 ssh 隧道,而不是暴露端口: 因为我想要一个简单且加密的隧道,所以在客户端运行:

     ssh -f -N -L 6379:127.0.0.1:6379 user@<serverip>
    

    在启动node index.js 之前,redis 保存在其环回地址服务器端,我在客户端使用端口转发(-f -N 用于后台进程 ps -x)和设置 ssh 密钥。
    如果坚持,您应该查看推荐的spiped

    【讨论】:

      【解决方案4】:

      以下代码使用节点包 redis 创建到 Redis 的连接。

      const redis = require('redis');
      
      const client = redis.createClient({
          host: '<hostname>',
          port: <port>,
          password: '<password>'
      });
      

      【讨论】:

        【解决方案5】:

        我在查看 redis auth 文档时发现其他人遇到了同样的问题

        http://redis.io/commands/auth

        你可能想从那里跟进

        【讨论】:

          【解决方案6】:

          我怀疑你和我有同样的问题。通过将 redis 模块本身作为选项传递给 RedisStore 构造函数,我设法修复了我的问题:

          Redis auth error with Node.js and socket.io

          没有它,RedisStore 模块将无法将您传入的 RedisClient 对象识别为“真正的”RedisClient 对象(因为它们将是来自 不同 redis 的 redis.RedisClient 类) , RedisStore 将重新创建它们并丢失您设置的身份验证。

          【讨论】:

            猜你喜欢
            • 2012-11-06
            • 2014-03-13
            • 2016-01-22
            • 1970-01-01
            • 1970-01-01
            • 2021-09-27
            • 2014-07-25
            • 1970-01-01
            • 2013-03-06
            相关资源
            最近更新 更多