const redis = require('redis');
require('dotenv').config();
console.log(process.env.redisHost, ':', process.env.redisPort);
const redisClient = redis.createClient({
host: process.env.redisHost,
port: process.env.redisPort,
password: process.env.redisKey
});
redisClient.connect();
redisClient.on('error', err => console.log('Redis error: ', err.message));
redisClient.on('connect', () => console.log('Connected to redis server'));
module.exports = redisClient;
I tried this sample from redis docs but still I'm getting an error stating:
Redis error: connect ECONNREFUSED 127.0.0.1:6379
I logged the environment host and port variables to the console and I got the remote host ipv4 address, but still the client is trying to connect to localhost instead of remote host (I purposely uninstalled redis from my local device to check if the client is working as it is supposed to). I also confirmed that the remote redis host is working perfectly.
I also tried different methods like : https://cloud.google.com/community/tutorials/nodejs-redis-on-appengine
redis.createClient(port, host, {auth_pass: password});
But still, I got the same error.
I am able to connect to the redis host via commandline:
redis-cli.exe -h XX.XX.XX.XXX -a Password
XX.XX.XX.XXX:6379> set name dhruv
OK
XX.XX.XX.XXX:6379> get name
"dhruv"
XX.XX.XX.XXX:6379> exit
I'm trying to use redis on nodejs for the first time, so don't have a proper idea but I think I am doing everything right. Any solution/workaround will be helpful :D
5 Answers 5
It worked with this code:
const url = `redis://${process.env.redisHost}:${process.env.redisPort}`;
const redisClient = redis.createClient({
url,
password: process.env.redisKey
});
redisClient.connect();
Comments
The configuration object changed in redis v4, you can view the new options here. Below is how I've configured it in my project.
const redisClient = redis.createClient({ socket: { port: process.env.redisPort, host: process.env.redisHost } })
redisClient.connect()
Comments
can you check if in the destination the port is reachable. it maybe the firewall block your access
10 Comments
cluster-ip? Also, for now I don't want to use docker as I am having a problem with my database in docker, so I was looking for a already deployed version of redisIf you want to connect to a remote redis db in an express nodejs app, you need to pass the url like this:
const url = `rediss://default:your_password@your_host:your_port`;
const client = redis.createClient({
url,
});
note that your host should start with something like private-db-redis-nyc3... or db-redis..., if you find redis:// don't include it.
Comments
I was trying to connect to a redis instance running on localhost with the url localhost:port but what made it work for me was using 127.0.0.1:port instead