[*] URGENT! Issue connecting to mongoDB Replica Set through Node v18.18.0

Hiii!, Im trying to connect to my mongoDB service that is currently a replica set of a single primary server, I need this in order to use changeStreams.
exampleMongo
The problem is that I get this “::1 ECONNREFUSED” , but I am not using the “localhost” syntax. Besides, I have another Python FastAPI running at the same time that connects to the MongoDB with no problems. So I dont know why Node cant stablish a connection. I have been days trying to connect but I can’t. Any help will be appreciated.
Error trace:

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    [cause]: Error: connect ECONNREFUSED ::1:27017
        at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
      errno: -4078,
      code: 'ECONNREFUSED',
      syscall: 'connect',
      address: '::1',
      port: 27017
    }

Thank You, all !!!

Hi @Pedro_Castro1

It looks like the replSet got initialised using an ipv6 address for localhost [::1]

You can reconfigure the replicaset member to be defined as localhost:27017 or 127.0.0.1:27017 .

Another option is you can add &directConnection=true to the connection string to continue without reconfiguring the replica set.


https://www.mongodb.com/docs/manual/tutorial/change-hostnames-in-a-replica-set/#change-hostnames-in-a-replica-set

I tried the “directConnection” param and it works Im really thankful @chris . But I have a question when I initialize the replSet I run this command.

Start-Process -FilePath "C:\Program Files\MongoDB\Server\7.0\bin\mongod.exe" -ArgumentList "--port 27017 --dbpath C:\path\DataBases\mongo\db0 --replSet rs0 --bind_ip localhost" -NoNewWindow

And when I connect with this url in MongoCompass it connects throup the ipv4 address like this


Can you explain to me why?
Thank you again bro

When connecting to a replicaset the hosts in the connection strings are a seed list.

The client discovers the rest of the hosts in the replicaset from the first seed node it can connect to and will then connect to the primary.

The hostnames in the replicaset configuration are the ones that will be used after the initial discovery. These are the hostname provided in rs.initiate() or rs.add().

1 Like

thanks for the explanation, really grateful! :grinning:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.