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

Nodejs v18 gives: MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 #12134

Closed
2 tasks done
chaeron opened this issue Jul 21, 2022 · 5 comments
Closed
2 tasks done
Labels
developer-experience This issue improves error messages, debugging, or reporting

Comments

@chaeron
Copy link

chaeron commented Jul 21, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.4.6

Node.js version

81.x

MongoDB server version

6.0

Description

If I'm using NodeJS 16.x everything works fine.

If I upgrade NodeJS to 18.x I get an error trying to connect to MongoDB:

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017

Nothing else different except NodeJS version.

Any ideas why mongoose connections fail with NodeJS 18, but are fine with 16?

Steps to Reproduce

Change to NodeJS 18

Expected Behavior

No response

@chaeron
Copy link
Author

chaeron commented Jul 21, 2022

Connection attempt was to a local MongoDB instance.

Exception/Stack trace was

MongooseServerSelectionError: connection timed out
    at Connection.openUri (/home/andrzej/Dropbox/Shore/TGLN/dev/migration/node_modules/mongoose/lib/connection.js:819:32)
    at /home/andrzej/Dropbox/Shore/TGLN/dev/migration/node_modules/mongoose/lib/index.js:379:10
    at /home/andrzej/Dropbox/Shore/TGLN/dev/migration/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/andrzej/Dropbox/Shore/TGLN/dev/migration/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/home/andrzej/Dropbox/Shore/TGLN/dev/migration/node_modules/mongoose/lib/index.js:1224:10)
    at Mongoose.connect (/home/andrzej/Dropbox/Shore/TGLN/dev/migration/node_modules/mongoose/lib/index.js:378:20)
    at Object.<anonymous> (/home/andrzej/Dropbox/Shore/TGLN/dev/migration/src/preMigrateAll.js:48:10)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      '128.0.0.1:27017' => ServerDescription {
        _hostAddress: HostAddress { isIPv6: false, host: '128.0.0.1', port: 27017 },
        address: '128.0.0.1:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 26963738,
        lastWriteDate: 0,
        error: MongoNetworkTimeoutError: connection timed out
            at connectionFailureError (/home/andrzej/Dropbox/Shore/TGLN/dev/migration/node_modules/mongoose/node_modules/mongodb/lib/cmap/connect.js:384:20)
            at Socket.<anonymous> (/home/andrzej/Dropbox/Shore/TGLN/dev/migration/node_modules/mongoose/node_modules/mongodb/lib/cmap/connect.js:302:22)
            at Object.onceWrapper (node:events:627:28)
            at Socket.emit (node:events:513:28)
            at Socket._onTimeout (node:net:526:8)
            at listOnTimeout (node:internal/timers:564:17)
            at process.processTimers (node:internal/timers:507:7) {
          [Symbol(errorLabels)]: Set(0) {}
        }
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  },
  code: undefined
}

Node.js v18.6.0

@Uzlopak
Copy link
Collaborator

Uzlopak commented Jul 22, 2022

Node18 uses ipv6 natively. So you should not use localhost but use the IP explicitly.

@IslandRhythms IslandRhythms added the developer-experience This issue improves error messages, debugging, or reporting label Jul 22, 2022
@vkarpov15
Copy link
Collaborator

Are you using localhost? Try doing mongodb://127.0.0.1:27017/mydbname instead of mongodb://localhost:27017/mydbname. Apparently Node 18 prefers ipv6 to ipv4 addresses, so it looks like Node is taking ::1 over 127.0.0.1 when Mongoose tries to resolve localhost.

Another alternative would be to configure your MongoDB server to accept ipv6 connections

@KrunalTarale
Copy link

I am facing the same problem and none of the solutions are working

@vkarpov15
Copy link
Collaborator

@KrunalTarale please open a new issue and follow the issue template.

@Automattic Automattic locked and limited conversation to collaborators Feb 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
developer-experience This issue improves error messages, debugging, or reporting
Projects
None yet
Development

No branches or pull requests

5 participants