简体   繁体   English

如何将nodejs redis客户端连接到redis云服务器?

[英]How to connect nodejs redis client to redis cloud server?

const redis = require('redis');

const client = redis.createClient({
  host: 'redis-19606.redislabs.com',
  port: 19606,
  password: 'password'
});

client.on('ready', () => {
  console.log('redis is connected');
});

client.on('error', (err) => {
  console.log('redis is disconnected: ', err);
});

(async () => {
  try {
    await client.connect();
  } catch (error) {
    console.error('error while connecting redis', error);
  }
})();

This somehow does not seem to work.这似乎不起作用。 What am I doing wrong?我究竟做错了什么? It keeps connecting to 127.0.0.1:6379 which is the default config instead of what I am passing.它一直连接到 127.0.0.1:6379 这是默认配置,而不是我传递的。 This is only happening with nodejs client of redis.这只发生在 redis 的 nodejs 客户端上。 go-redis the golang client for redis is working flawlessly. go-redis 的 golang 客户端 redis 工作完美。

Just a guess, but which Node Redis version do you use?只是猜测,但您使用的是哪个节点 Redis 版本? I had difficulties upgrading myself.我在升级自己时遇到了困难。 The configuration of the client has changed since version 4.xx Since version 4.xx you have to use a confiuration according to Client Configuration .自 4.xx 版以来,客户端的配置已更改自 4.xx 版以来,您必须根据Client Configuration使用配置。 Therefore use因此使用

const client = redis.createClient({
  socket: {
    host: 'redis-19606.redislabs.com',
    port: 19606,
  }
});

or use a URL或使用 URL

const client = redis.createClient({
  url: "redis://redis-19606.redislabs.com:19606"
});

Your client configuration matches the Node Redis version 3.xx but not 4.xx Please see Redis NPM v3.1.2 and Redis NPM v4.0.1 for details. Your client configuration matches the Node Redis version 3.xx but not 4.xx Please see Redis NPM v3.1.2 and Redis NPM v4.0.1 for details.

you should maintain this format of Redis connecting string:您应该保持这种格式的 Redis 连接字符串:

redis://:YOUR_PASSWORD@YOUR_ENDPOINT:YOUR_PORT
  1. What version of redis are you using?你用的是什么版本的 redis?
  2. Do not explicitly conect to redis by writing "client.connect()" but instead use this after setting host, port and password:不要通过编写“client.connect()”明确连接到 redis,而是在设置主机、端口和密码后使用它:
     redisClient.on("connect", () => {})

and then you can use redisClient variable to get and set values.然后您可以使用redisClient变量来获取和设置值。

  1. Also if you're trying to connect to a redislabs server then I think your host name might be incorrect and double check your host from redislabs.此外,如果您尝试连接到 redislabs 服务器,那么我认为您的主机名可能不正确,并从 redislabs 仔细检查您的主机。

Try this new Redis("redis://username:authpassword@127.0.0.1:6380/4");试试这个new Redis("redis://username:authpassword@127.0.0.1:6380/4");

Check the documentation Link检查文档链接

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

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