[英]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);
}
})();
這似乎不起作用。 我究竟做錯了什么? 它一直連接到 127.0.0.1:6379 這是默認配置,而不是我傳遞的。 這只發生在 redis 的 nodejs 客戶端上。 go-redis 的 golang 客戶端 redis 工作完美。
只是猜測,但您使用的是哪個節點 Redis 版本? 我在升級自己時遇到了困難。 自 4.xx 版以來,客戶端的配置已更改自 4.xx 版以來,您必須根據Client Configuration使用配置。 因此使用
const client = redis.createClient({
socket: {
host: 'redis-19606.redislabs.com',
port: 19606,
}
});
或使用 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.
您應該保持這種格式的 Redis 連接字符串:
redis://:YOUR_PASSWORD@YOUR_ENDPOINT:YOUR_PORT
redisClient.on("connect", () => {})
然后您可以使用redisClient變量來獲取和設置值。
試試這個new Redis("redis://username:authpassword@127.0.0.1:6380/4");
檢查文檔鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.