MongoDB connect ECONNREFUSED timeout in nodejs

541 views Asked by At

I'm trying to work on my web-app's nodejs backend which simply handles express.js API requests and deals with my mongodb database access, however after recently switching to linux full time I seem to be unable to connect to the database from my code:

/home/Vawlpe/repos/vawlpe.github.io/server/node_modules/mongodb/src/sdam/topology.ts:606
        const timeoutError = new MongoServerSelectionError(
                             ^
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (/home/Vawlpe/repos/vawlpe.github.io/server/node_modules/mongodb/src/sdam/topology.ts:606:30)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

I made sure mongodb is running on the right port, I don't have any firewall configured, and I can connect to it using mongosh just fine, but the JS API seems to have trouble, any clue what i could be missing? Code:

  //--------DB----------
  const db_port : number = 27017;
  const db_connnect_string : string = `mongodb://localhost:${db_port}`;
  const db : MongoClient = new MongoClient(db_connnect_string);

  console.log(`Connecting to DB @ ${db_connnect_string}...`);

  db.connect().catch((err) => {
    if (err) throw err;
  }).then(() => {
    console.log(`Successfully Connected to DB @ \x1b[34m${db_connnect_string}\x1b[0m`);
  })

nmap -p- localhost output:

$ nmap -p- localhost
Starting Nmap 7.92 ( https://nmap.org ) at 2022-05-26 13:21 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00020s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 65532 closed tcp ports (conn-refused)
PORT      STATE SERVICE
3000/tcp  open  ppp
6463/tcp  open  unknown
27017/tcp open  mongod

Nmap done: 1 IP address (1 host up) scanned in 1.20 seconds

I have a feeling this might have something to do with nodejs resolving localhost to ::1 instead of 127.0.0.1 but both should be pointing to the same thing so I don't see the problem..

Edit: yes, changing the connection string from localhost to 127.0.0.1 does fix the issue, I guess I should edit /etc/hosts and see what's wrong with my ::1 mapping

Edit 2: turns out ::1 is the ipv6 loopback address, equivalent to ipv4's 127.0.0.1, I have mongod running on ipv4, but the mongodb js api or maybe nodejs itself seems to prefer resolving localhost trough ipv6, looking into how to fix that cuz I'd prefer not having to write 127.0.0.1 instead of localhost in my code lol

1

There are 1 answers

0
Vawlpe On

Turns out the easiest solution was simply to run mongod on ipv6, so I just edited /etc/mongodb.conf as follows:

# network interfaces
net:
  ipv6: true
  port: 27017
  bindIp: ::1