Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createClient trying to connect with localhost even though URL is for docker's redis instance #2416

Open
sk1122 opened this issue Feb 18, 2023 · 17 comments
Labels

Comments

@sk1122
Copy link

sk1122 commented Feb 18, 2023

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       | }
@sk1122 sk1122 added the Bug label Feb 18, 2023
@leibale
Copy link
Contributor

leibale commented Feb 18, 2023

Make sure to pass the url like this:

createClient({ url: '...' });

@anatoly314
Copy link

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.

@sk1122
Copy link
Author

sk1122 commented Feb 20, 2023

Make sure to pass the url like this:

createClient({ url: '...' });

Yeah, doing that exactly

@leibale
Copy link
Contributor

leibale commented Feb 20, 2023

@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?

@sk1122
Copy link
Author

sk1122 commented Feb 20, 2023

@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

@leibale
Copy link
Contributor

leibale commented Feb 20, 2023

@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 :)

@leibale
Copy link
Contributor

leibale commented Mar 2, 2023

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?

@mbeerta-factset
Copy link

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();

@stalwartstu
Copy link

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.

@tansanDOTeth
Copy link

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

@kosson
Copy link

kosson commented Sep 26, 2023

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
        }
    });

`

@ghost
Copy link

ghost commented Jan 24, 2024

has this problem been solved? How ?

@JamesMonges
Copy link

I seems ok as add 'redis://' for url ,like this
client = createClient({
url: 'redis://x.x.x.x:6379',
});

@federicoaaguirre
Copy link

Hi there, someone here has solve the problem? :)

@trilamsr
Copy link

trilamsr commented May 6, 2024

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
}
]
}

@StevanZivadinovic
Copy link

StevanZivadinovic commented Jul 9, 2024

@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));

@mt-ks
Copy link

mt-ks commented Jul 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests