使用 Redis.createClient() 连接远程 Redis 服务器
要连接到远程服务器的 Redis,需要在 redis.createClient() 方法中传递适当的连接参数。以下是一个示例:
const redis = require('redis');
const client = redis.createClient({
host: 'remote-server-ip-address',
port: 6379,
password: 'redis-password'
});
client.on('connect', () => {
console.log('Connected to Redis');
});
client.on('error', (err) => {
console.log('Error:', err);
});
在上面的示例中,我们传递了远程服务器的 IP 地址,端口号和密码作为连接参数。如果连接成功,将输出'Connected to Redis',否则将输出错误信息。
原文地址: https://www.cveoy.top/t/topic/mv5p 著作权归作者所有。请勿转载和采集!