node-redis
node-redis copied to clipboard
createClient trying to connect with localhost even though URL is for docker's redis instance
Description
I am trying to create a client with URL redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}
, and sending REDIS_HOST=rediss
and REDIS_PORT=6379
, but still it is trying to connect with localhost
redis is running in docker, tested the connection, its working also have other instance of
ioredis
using same connection, it is able to connect
Node.js Version
v19.6.0
Redis Server Version
7.0.5
Node Redis Version
4.6.4
Platform
Linux
Logs
web | Error: connect ECONNREFUSED 127.0.0.1:6379
web | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
web | errno: -111,
web | code: 'ECONNREFUSED',
web | syscall: 'connect',
web | address: '127.0.0.1',
web | port: 6379
web | }
Make sure to pass the url like this:
createClient({ url: '...' });
I have the same error when I try to connect from AWS lambda using discrete parameters, like host, port, and DB. When I use the URL, it works.
Make sure to pass the url like this:
createClient({ url: '...' });
Yeah, doing that exactly
@anatoly314 make sure to use this format for discrete parameters:
createClient({
socket: {
host: '127.0.0.1',
port: 6379
},
database: 123,
});
see this file for more information.
@sk1122 I just ran this code locally
import { createClient } from 'redis';
const client = createClient({
url: 'redis://127.0.0.2:6378',
});
client.on('error', err => console.error(err));
await client.connect();
and here is the error I get:
Error: connect ECONNREFUSED 127.0.0.2:6378
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.2',
port: 6378
}
can you please try running the same code and share the error you get?
@anatoly314 make sure to use this format for discrete parameters:
createClient({ socket: { host: '127.0.0.1', port: 6379 }, database: 123, });
see this file for more information.
@sk1122 I just ran this code locally
import { createClient } from 'redis'; const client = createClient({ url: 'redis://127.0.0.2:6378', }); client.on('error', err => console.error(err)); await client.connect();
and here is the error I get:
Error: connect ECONNREFUSED 127.0.0.2:6378 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.2', port: 6378 }
can you please try running the same code and share the error you get?
I solved this using ioredis
library, still don't why this error is happening
@sk1122 I won't be able to fix it without reproducing the error first... If you can run the script above and share the error you get it'll be very helpful :)
I think that the issue is that the client is not defaulting to the REDIS_HOST
and REDIS_PORT
env variables, you have to pass them to the client manually. @sk1122 is that it?
We are running into a similar issue here, using url or discrete options causes this to appear just after connect()
is called.
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
Node.js v18.16.0
process.env.REDIS_URL
is defined as redis://:password@hostname:port
and not set to 127.0.0.1:6379 at all
class QueueWorker {
constructor() {
const url = process.env.REDIS_URL ?? "redis://127.0.0.1:6379";
const parsedUrl = new URL(url);
const options = {
socket: {
port: Number.parseInt(parsedUrl.port, 10),
host: parsedUrl.hostname,
family: 4
},
username: parsedUrl.username,
password: parsedUrl.password
};
console.log(options);
this.redisClient = redis.createClient(options);
this.pubSubClient = redis.createClient(options);
this.redisClient.on("error", console.log);
this.pubSubClient.on("error", console.log);
}
async start() {
await this.redisClient.connect();
await this.pubSubClient.connect();
}
}
const worker = new QueueWorker();
await worker.start();
In the same set-up I had to swap url
for socket.host
and socket.port
, but also had to switch from localhost
to 127.0.0.1
.
I am experiencing similar issues when using REDIS_URL
in a docker environment.
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
^
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
Node.js v20.5.1
Same here. No matter what I would try, I run into the same issue but only when I set the redis store for session management. The client is at the last version.
redis.config.js `
let redisClients = {};
function redisInit({redisCachedInstance = requiredParam(redisCachedInstance), redisClients} = {}) {
redisClients['cachedInstance'] = redisCachedInstance;
redisEventListeners({redisClient: redisCachedInstance});
return {
redisCachedInstance,
redisClients
};
}
/* virtualized scenario with Docker */
if (process.env.APP_RUNTIME === 'virtual') {
/** @type {string} */
let urlstring = `redis://default:${process.env.REDIS_HOST_PASSWORD}@redis:6379/10`;
const options = urlstring ? {url: urlstring} : {};
let redisConnectorVirtual = redisInit({redisCachedInstance: redis.createClient(options), redisClients});
// redisClient = redis.createClient(options);
module.exports = redisConnectorVirtual;
} `
app.js
`
// REDIS
const {redisCachedInstance, redisClients} = require('./redis.config');
if (redisCachedInstance) {
await redisCachedInstance.connect();
// console.log(`[app.js] Clienții conectați sunt: ${JSON.stringify(redisClients)}`);
} else {
throw new Error(`[app.js] Nu am client de Redis pe care să-l activez. Modulul nu este încărcat.`);
}
await redisCachedInstance.auth(process.env.REDIS_HOST_PASSWORD);
let sessionMiddleware = session({
name: process.env.APP_NAME,
secret: process.env.COOKIE_ENCODING,
genid: function(req) {
return crypto.randomUUID({disableEntropyCache : true}); // pentru ID-urile de sessiune, folosește UUID-uri
},
store: new RedisStore({client: redisCachedInstance, prefix: `${process.env.APP_NAME}:`}),
unref: true,
proxy: true,
resave: false,
saveUninitialized: true,
logErrors: true,
cookie: {
httpOnly: true,
maxAge: (1 * 24 * 3600 * 1000),
sameSite: 'lax' // https://www.npmjs.com/package/express-session#cookiesamesite
}
});
`
has this problem been solved? How ?
I seems ok as add 'redis://' for url ,like this client = createClient({ url: 'redis://x.x.x.x:6379', });
Hi there, someone here has solve the problem? :)
Problem is still going on
static async StartCache():Promise<RedisCache> {
const c = createClient({
socket: {
host: 'external_redis_instance',
port:12896
},
url: 'external_redis_instance',
password: 'password'
});
c.on('error', err => console.log('Redis Client Error', err));
c.on('connect', err => console.log('Redis Client CONNECTED', err));
await c.connect();
return new RedisCache(c);
}
edis Client Error AggregateError at internalConnectMultiple (node:net:1116:18) at afterConnectMultiple (node:net:1683:7) { code: 'ECONNREFUSED', [errors]: [ Error: connect ECONNREFUSED ::1:12896 at createConnectionError (node:net:1646:14) at afterConnectMultiple (node:net:1676:16) { errno: -61, code: 'ECONNREFUSED', syscall: 'connect', address: '::1', port: 12896 }, Error: connect ECONNREFUSED 127.0.0.1:12896 at createConnectionError (node:net:1646:14) at afterConnectMultiple (node:net:1676:16) { errno: -61, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 12896 } ] }
@sk1122 Hi, I solved problem like this:
.createClient({
url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`,
})
.on("error", (err) => console.log("Redis Client Error", err));
Same issue!
await createClient({
socket: {
host: "name-of-service",
port: 6379,
},
}).connect();
Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1605:16) [ERROR] 16:37:26 Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379