Skip to content

Database connection consistently fails after version 8.6.4 #15351

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

Open
2 tasks done
domfarolino opened this issue Apr 13, 2025 · 6 comments
Open
2 tasks done

Database connection consistently fails after version 8.6.4 #15351

domfarolino opened this issue Apr 13, 2025 · 6 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@domfarolino
Copy link

Prerequisites

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

Mongoose version

8.6.4

Node.js version

19.3.0

MongoDB server version

8.0

Typescript version (if applicable)

N/A

Description

This is a follow-up to #15015, specifically #15015 (comment). I've made a minimal, reduced test case to illustrate the issue I shared with the OP poster of that issue, namely that I cannot get a database connection to succeed after v8.6.4: https://github.com/domfarolino/mongoose-test-server.

Steps to Reproduce

Clone the https://github.com/domfarolino/mongoose-test-server repository and run:

npm i
npm start

The mongoose version is 8.13.2, but with anything after 8.6.4 you'll see the following in the console:

> [email protected] start
> node app.js

Creating server on port 8000

...then after a long timeout, you'll see:

Click here for the full error
connection error:
/path/to/mongoose-test-server/node_modules/mongoose/lib/connection.js:1165
    err = new ServerSelectionError();
          ^

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://www.mongodb.com/docs/atlas/security-whitelist/
    at _handleConnectionErrors (/path/to/mongoose-test-server/node_modules/mongoose/lib/connection.js:1165:11)
    at NativeConnection.openUri (/path/to/mongoose-test-server/node_modules/mongoose/lib/connection.js:1096:11) {
  errorLabelSet: Set(0) {},
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-k1dppis-shard-00-00.5fjrn1r.mongodb.net:27017' => ServerDescription {
        address: 'ac-k1dppis-shard-00-00.5fjrn1r.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        minRoundTripTime: 0,
        lastUpdateTime: 4056868607,
        lastWriteDate: 0,
        error: MongoNetworkError: Client network socket disconnected before secure TLS connection was established
            at TLSSocket.<anonymous> (/path/to/mongoose-test-server/node_modules/mongodb/lib/cmap/connect.js:285:44)
            at Object.onceWrapper (node:events:628:26)
            at TLSSocket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          errorLabelSet: Set(1) { 'ResetPool' },
          beforeHandshake: false,
          [cause]: Error: Client network socket disconnected before secure TLS connection was established
              at connResetException (node:internal/errors:718:14)
              at TLSSocket.onConnectEnd (node:_tls_wrap:1600:19)
              at TLSSocket.emit (node:events:525:35)
              at endReadableNT (node:internal/streams/readable:1359:12)
              at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
            code: 'ECONNRESET',
            path: undefined,
            host: 'ac-k1dppis-shard-00-00.5fjrn1r.mongodb.net',
            port: 27017,
            localAddress: undefined
          }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        maxMessageSizeBytes: null,
        maxWriteBatchSize: null,
        maxBsonObjectSize: null,
        primary: null,
        me: null,
        '$clusterTime': null,
        iscryptd: false
      },
      'ac-k1dppis-shard-00-01.5fjrn1r.mongodb.net:27017' => ServerDescription {
        address: 'ac-k1dppis-shard-00-01.5fjrn1r.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        minRoundTripTime: 0,
        lastUpdateTime: 4056868608,
        lastWriteDate: 0,
        error: MongoNetworkError: Client network socket disconnected before secure TLS connection was established
            at TLSSocket.<anonymous> (/path/to/mongoose-test-server/node_modules/mongodb/lib/cmap/connect.js:285:44)
            at Object.onceWrapper (node:events:628:26)
            at TLSSocket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          errorLabelSet: Set(1) { 'ResetPool' },
          beforeHandshake: false,
          [cause]: Error: Client network socket disconnected before secure TLS connection was established
              at connResetException (node:internal/errors:718:14)
              at TLSSocket.onConnectEnd (node:_tls_wrap:1600:19)
              at TLSSocket.emit (node:events:525:35)
              at endReadableNT (node:internal/streams/readable:1359:12)
              at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
            code: 'ECONNRESET',
            path: undefined,
            host: 'ac-k1dppis-shard-00-01.5fjrn1r.mongodb.net',
            port: 27017,
            localAddress: undefined
          }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        maxMessageSizeBytes: null,
        maxWriteBatchSize: null,
        maxBsonObjectSize: null,
        primary: null,
        me: null,
        '$clusterTime': null,
        iscryptd: false
      },
      'ac-k1dppis-shard-00-02.5fjrn1r.mongodb.net:27017' => ServerDescription {
        address: 'ac-k1dppis-shard-00-02.5fjrn1r.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        minRoundTripTime: 0,
        lastUpdateTime: 4056868647,
        lastWriteDate: 0,
        error: MongoNetworkError: Client network socket disconnected before secure TLS connection was established
            at TLSSocket.<anonymous> (/path/to/mongoose-test-server/node_modules/mongodb/lib/cmap/connect.js:285:44)
            at Object.onceWrapper (node:events:628:26)
            at TLSSocket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          errorLabelSet: Set(1) { 'ResetPool' },
          beforeHandshake: false,
          [cause]: Error: Client network socket disconnected before secure TLS connection was established
              at connResetException (node:internal/errors:718:14)
              at TLSSocket.onConnectEnd (node:_tls_wrap:1600:19)
              at TLSSocket.emit (node:events:525:35)
              at endReadableNT (node:internal/streams/readable:1359:12)
              at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
            code: 'ECONNRESET',
            path: undefined,
            host: 'ac-k1dppis-shard-00-02.5fjrn1r.mongodb.net',
            port: 27017,
            localAddress: undefined
          }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        maxMessageSizeBytes: null,
        maxWriteBatchSize: null,
        maxBsonObjectSize: null,
        primary: null,
        me: null,
        '$clusterTime': null,
        iscryptd: false
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-ux3okq-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}

Node.js v19.3.0

Expected Behavior

If you change the mongoose version to 8.6.4 or anything earlier, you'll see the following:

> [email protected] start
> node app.js

Creating server on port 8000
Connected to mongoose!
@vkarpov15
Copy link
Collaborator

I get the same error message with Mongoose 8.6.3 and 8.5.0, for example:

$ npm list
[email protected] /Users/val/Workspace/MongoDB/troubleshoot-mongoose/mongoose-test-server
├── [email protected]
└── [email protected]

$ npm start

> [email protected] start
> node app.js

Creating server on port 8000
connection error:
/Users/val/Workspace/MongoDB/troubleshoot-mongoose/mongoose-test-server/node_modules/mongoose/lib/connection.js:897
    err = new ServerSelectionError();
          ^

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://www.mongodb.com/docs/atlas/security-whitelist/
    at _handleConnectionErrors (/Users/val/Workspace/MongoDB/troubleshoot-mongoose/mongoose-test-server/node_modules/mongoose/lib/connection.js:897:11)
    at NativeConnection.openUri (/Users/val/Workspace/MongoDB/troubleshoot-mongoose/mongoose-test-server/node_modules/mongoose/lib/connection.js:848:11) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-k1dppis-shard-00-00.5fjrn1r.mongodb.net:27017' => ServerDescription {
        address: 'ac-k1dppis-shard-00-00.5fjrn1r.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        minRoundTripTime: 0,
        lastUpdateTime: 1826677761,
        lastWriteDate: 0,
        error: MongoNetworkError: C00FAD5DF87F0000:error:0A000438:SSL routines:ssl3_read_bytes:tlsv1 alert internal error:../deps/openssl/openssl/ssl/record/rec_layer_s3.c:1586:SSL alert number 80

Are you sure you're connecting from an IP that's in your Atlas whitelist? The error message I'm getting is indicative of whitelisting issues: https://www.mongodb.com/community/forums/t/tls-handshake-alert-number-80-issue-between-aws-ecs-instance-and-atlas/248642

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Apr 16, 2025
@domfarolino
Copy link
Author

When I set the server up in Atlas, I forgot to add the 0.0.0.0/0 whitelist for universal access, good catch. I've added that, and now I'm back to observing the success < 8.6.4 and failure >=8.6.4 can you please try again and see if you now observe mongoose successfully connecting to the database < 8.6.4?

@vkarpov15
Copy link
Collaborator

What error are you seeing? Running your server I now get the following output with Mongoose 8.13.2:

$ npm start

> [email protected] start
> node app.js

Creating server on port 8000
Connected to mongoose!
^C

@domfarolino
Copy link
Author

I'm seeing the error that I posted in the OP under "Click here for the full error" when you click that details disclosure:

connection error:
/path/to/mongoose-test-server/node_modules/mongoose/lib/connection.js:1165
    err = new ServerSelectionError();
          ^

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://www.mongodb.com/docs/atlas/security-whitelist/
    at _handleConnectionErrors (/path/to/mongoose-test-server/node_modules/mongoose/lib/connection.js:1165:11)
    at NativeConnection.openUri (/path/to/mongoose-test-server/node_modules/mongoose/lib/connection.js:1096:11) {
  errorLabelSet: Set(0) {},
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-k1dppis-shard-00-00.5fjrn1r.mongodb.net:27017' => ServerDescription {
        address: 'ac-k1dppis-shard-00-00.5fjrn1r.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        minRoundTripTime: 0,
        lastUpdateTime: 4056868607,
        lastWriteDate: 0,
        error: MongoNetworkError: Client network socket disconnected before secure TLS connection was established
            at TLSSocket.<anonymous> (/path/to/mongoose-test-server/node_modules/mongodb/lib/cmap/connect.js:285:44)
            at Object.onceWrapper (node:events:628:26)
            at TLSSocket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          errorLabelSet: Set(1) { 'ResetPool' },
          beforeHandshake: false,
          [cause]: Error: Client network socket disconnected before secure TLS connection was established
              at connResetException (node:internal/errors:718:14)
              at TLSSocket.onConnectEnd (node:_tls_wrap:1600:19)
              at TLSSocket.emit (node:events:525:35)
              at endReadableNT (node:internal/streams/readable:1359:12)
              at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
            code: 'ECONNRESET',
            path: undefined,
            host: 'ac-k1dppis-shard-00-00.5fjrn1r.mongodb.net',
            port: 27017,
            localAddress: undefined
          }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        maxMessageSizeBytes: null,
        maxWriteBatchSize: null,
        maxBsonObjectSize: null,
        primary: null,
        me: null,
        '$clusterTime': null,
        iscryptd: false
      },
      'ac-k1dppis-shard-00-01.5fjrn1r.mongodb.net:27017' => ServerDescription {
        address: 'ac-k1dppis-shard-00-01.5fjrn1r.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        minRoundTripTime: 0,
        lastUpdateTime: 4056868608,
        lastWriteDate: 0,
        error: MongoNetworkError: Client network socket disconnected before secure TLS connection was established
            at TLSSocket.<anonymous> (/path/to/mongoose-test-server/node_modules/mongodb/lib/cmap/connect.js:285:44)
            at Object.onceWrapper (node:events:628:26)
            at TLSSocket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          errorLabelSet: Set(1) { 'ResetPool' },
          beforeHandshake: false,
          [cause]: Error: Client network socket disconnected before secure TLS connection was established
              at connResetException (node:internal/errors:718:14)
              at TLSSocket.onConnectEnd (node:_tls_wrap:1600:19)
              at TLSSocket.emit (node:events:525:35)
              at endReadableNT (node:internal/streams/readable:1359:12)
              at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
            code: 'ECONNRESET',
            path: undefined,
            host: 'ac-k1dppis-shard-00-01.5fjrn1r.mongodb.net',
            port: 27017,
            localAddress: undefined
          }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        maxMessageSizeBytes: null,
        maxWriteBatchSize: null,
        maxBsonObjectSize: null,
        primary: null,
        me: null,
        '$clusterTime': null,
        iscryptd: false
      },
      'ac-k1dppis-shard-00-02.5fjrn1r.mongodb.net:27017' => ServerDescription {
        address: 'ac-k1dppis-shard-00-02.5fjrn1r.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        minRoundTripTime: 0,
        lastUpdateTime: 4056868647,
        lastWriteDate: 0,
        error: MongoNetworkError: Client network socket disconnected before secure TLS connection was established
            at TLSSocket.<anonymous> (/path/to/mongoose-test-server/node_modules/mongodb/lib/cmap/connect.js:285:44)
            at Object.onceWrapper (node:events:628:26)
            at TLSSocket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          errorLabelSet: Set(1) { 'ResetPool' },
          beforeHandshake: false,
          [cause]: Error: Client network socket disconnected before secure TLS connection was established
              at connResetException (node:internal/errors:718:14)
              at TLSSocket.onConnectEnd (node:_tls_wrap:1600:19)
              at TLSSocket.emit (node:events:525:35)
              at endReadableNT (node:internal/streams/readable:1359:12)
              at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
            code: 'ECONNRESET',
            path: undefined,
            host: 'ac-k1dppis-shard-00-02.5fjrn1r.mongodb.net',
            port: 27017,
            localAddress: undefined
          }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        maxMessageSizeBytes: null,
        maxWriteBatchSize: null,
        maxBsonObjectSize: null,
        primary: null,
        me: null,
        '$clusterTime': null,
        iscryptd: false
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-ux3okq-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}

Node.js v19.3.0

@DianaBianca
Copy link

I got similar error here when I updated mongoose version from "mongoose": "^8.4.1" to "mongoose": "^8.9.5", the connection with database is not working anymore

My Node version is: v20.19.1

Error:

[Nest] 153  - 04/28/2025, 6:08:45 PM   ERROR [MongooseModule] Unable to connect to the database. Retrying (7)...
MongoServerError: Unsupported mechanism [ -301 ]

@vkarpov15
Copy link
Collaborator

@domfarolino what version of Node.js are you using?

@DianaBianca are you using DocumentDB? If so take a look at #15018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

3 participants