[英]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
redisClient.on("connect", () => {})
and then you can use redisClient variable to get and set values.然后您可以使用redisClient变量来获取和设置值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.