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

Database connection failed after update mongoose from 8.6.4 to 8.7.0 or higher #15015

Open
1 task done
lemagicien00 opened this issue Nov 2, 2024 · 11 comments
Open
1 task done
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity

Comments

@lemagicien00
Copy link

lemagicien00 commented Nov 2, 2024

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.8.0

Node.js version

18.15.0

MongoDB version

7.0.15

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Debian 4.19.0-24-cloud-amd64

Issue

When I update mongoose to version 8.7.0 or higher, I have a database connection error on MongoDb Atlas.
If I go back to mongoose version 8.6.4 everything works fine
The problem is the same from Windows (dev) or Debian Linux (production) on AWS EC2 and on OVH VPS.
The IPs are in the whitelist in mongodb atlas network access.

I did some tests by changing some options (like serverSelectionTimeoutMS) but it didn't change the error.

Log:

Error:  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 (/home/admin/dcare2-cloud/node_modules/mongoose/lib/connection.js:909:11)
    at NativeConnection.openUri (/home/admin/dcare2-cloud/node_modules/mongoose/lib/connection.js:860:11) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'cluster0-shard-00-01.*******************.net:27017' => [ServerDescription],
      'cluster0-shard-00-02.*******************.net:27017' => [ServerDescription],
      'cluster0-shard-00-00.*******************.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-vzvxq2-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}

The code for connection:

// dbUrl is defined like this on env file:
  dbUrl:
    "mongodb+srv://USER:PASSWORD@cluster0.*******************.net/myDB?retryWrites=true&w=majority",

module.exports = {
  connection: async function (callback) {
    const options = {
      sanitizeFilter: true,
      socketTimeoutMS: 300000, //300s (default = 0)
      //serverSelectionTimeoutMS: 10000, //10s (default 30s)
      minPoolSize: 10, //(default 5)
      maxPoolSize: 30, //(default 100)
    };

    mongoose
      .connect(env.dbUrl, options)
      .then(() => {
        console.info("Database connection ok");
        callback(null);
      })
      .catch((error) => {
        console.error("Database connection failed!");
        console.error("Error: ", error);

        callback(error);
      });

    mongoose.connection.once("open", function () {
      console.info("MongoDB event open");
      //console.log("MongoDB connected to [%s]", env.dbUrl);
    });

    mongoose.connection.on("connected", function () {
      console.info("MongoDB event connected");
    });

    mongoose.connection.on("disconnected", function () {
      console.warn("MongoDB event disconnected");
    });

    mongoose.connection.on("reconnected", function () {
      console.info("MongoDB event reconnected");
    });

    mongoose.connection.on("error", function (err) {
      console.error("MongoDB event error: " + err);
    });
  },

  disconnection: async function (callback) {
    mongoose
      .disconnect()
      .then(() => {
        console.info("Database disconnection ok");
        callback(null);
      })
      .catch((error) => {
        console.error("Database disconnection failed!");
        console.error("Error: ", error);

        callback(error);
      });
  },

  getStats: async function () {
    return await mongoose.connection.db.stats();
  },
};

Thank you for your help

@lemagicien00 lemagicien00 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Nov 2, 2024
@vkarpov15
Copy link
Collaborator

Can you please print out what the ServerDescription object is in 'cluster0-shard-00-01..net:27017' => [ServerDescription], ? That may help us see why Mongoose can't connect.

@vkarpov15 vkarpov15 added needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity and removed help wanted labels Nov 4, 2024
@lemagicien00
Copy link
Author

Of course here it is.

error.reason:  TopologyDescription {
  type: 'ReplicaSetNoPrimary',
  servers: Map(3) {
    'cluster0-shard-00-02.******.mongodb.net:27017' => ServerDescription {
      address: 'cluster0-shard-00-02.******.mongodb.net:27017',
      type: 'Unknown',
      hosts: [],
      passives: [],
      arbiters: [],
      tags: {},
      minWireVersion: 0,
      maxWireVersion: 0,
      roundTripTime: -1,
      minRoundTripTime: 0,
      lastUpdateTime: 42365180852,
      lastWriteDate: 0,
      error: [MongoNetworkError],
      topologyVersion: null,
      setName: null,
      setVersion: null,
      electionId: null,
      logicalSessionTimeoutMinutes: null,
      maxMessageSizeBytes: null,
      maxWriteBatchSize: null,
      maxBsonObjectSize: null,
      primary: null,
      me: null,
      '$clusterTime': null
    },
    'cluster0-shard-00-00.******.mongodb.net:27017' => ServerDescription {
      address: 'cluster0-shard-00-00.******mongodb.net:27017',
      type: 'Unknown',
      hosts: [],
      passives: [],
      arbiters: [],
      tags: {},
      minWireVersion: 0,
      maxWireVersion: 0,
      roundTripTime: -1,
      minRoundTripTime: 0,
      lastUpdateTime: 42365180705,
      lastWriteDate: 0,
      error: [MongoNetworkError],
      topologyVersion: null,
      setName: null,
      setVersion: null,
      electionId: null,
      logicalSessionTimeoutMinutes: null,
      maxMessageSizeBytes: null,
      maxWriteBatchSize: null,
      maxBsonObjectSize: null,
      primary: null,
      me: null,
      '$clusterTime': null
    },
    'cluster0-shard-00-01.******.mongodb.net:27017' => ServerDescription {
      address: 'cluster0-shard-00-01.******.mongodb.net:27017',
      type: 'Unknown',
      hosts: [],
      passives: [],
      arbiters: [],
      tags: {},
      minWireVersion: 0,
      maxWireVersion: 0,
      roundTripTime: -1,
      minRoundTripTime: 0,
      lastUpdateTime: 42365180703,
      lastWriteDate: 0,
      error: [MongoNetworkError],
      topologyVersion: null,
      setName: null,
      setVersion: null,
      electionId: null,
      logicalSessionTimeoutMinutes: null,
      maxMessageSizeBytes: null,
      maxWriteBatchSize: null,
      maxBsonObjectSize: null,
      primary: null,
      me: null,
      '$clusterTime': null
    }
  },
  stale: false,
  compatible: true,
  heartbeatFrequencyMS: 10000,
  localThresholdMS: 15,
  setName: 'atlas-******-shard-0',
  maxElectionId: null,
  maxSetVersion: null,
  commonWireVersion: 0,
  logicalSessionTimeoutMinutes: null
}

@mihaylovin
Copy link

I am facing the exact same issue with mongoose 8.8.1

@vkarpov15
Copy link
Collaborator

@lemagicien00 thanks for posting the error. Can you also show what err.reason.servers.get(serverUrlHere).error is? The error you posted just tells us that for some reason the MongoDB Node driver can't connect to any of the servers in your Atlas cluster. The ServerDescription.prototype.error property is a MongoNetworkError that should tell us more.

@lemagicien00
Copy link
Author

lemagicien00 commented Nov 20, 2024

The output given by error.reason.servers.get(serverUrlHere).error is

MongoNetworkError: C0A77A8C407F0000:error:0A000438:SSL routines:ssl3_read_bytes:tlsv1 alert internal error:../deps/openssl/openssl/ssl/record/rec_layer_s3.c:1605:SSL alert number 80

I would like to make a clarification.
The problem persists on a cluster mongodb atlas database in free mode. (mongoose 8.6.4 is ok but not for mongoose 8.7.0 or higher)
But I migrated another cluster from free to dedicated mode and apparently the error is no longer present.

@didaquis
Copy link

After doing some test I can confirm that issue,

It works on Mongoose 8.6.4 but fails on 8.7.0.

So seems an issue introduced on this version: https://github.com/Automattic/mongoose/releases/tag/8.7.0

@vkarpov15
Copy link
Collaborator

@lemagicien00
Copy link
Author

In production I can't migrate at will, I have to wait for the next scheduled maintenance.
But I can do a test on the test server and upgrade to v18.20.5.

I will get back to you after this test.

@BrynkaIT
Copy link

I have a similar issue. I am getting intermittent connection errors with Mongoose 8.7.0 and higher.

I have a multi-tenant design and use the following function to switch db's and register the model(s) required:

dbModel: (databaseName, modelNames = []) => { 
    const db = mongoose.connection.useDb(databaseName, { useCache: true }) 

    let models = {}
    modelNames.forEach(modelName => {
      if (!db.models[modelName]) {
        const model = mongoose.model(modelName)
        models[`${modelName}`] = db.model(model.modelName, model.schema, model.collection.collectionName)
      } else {
        models[`${modelName}`] = db.model(modelName)
      }
    })
    return models
 }

With 8.7.0 the readyState comes in as "0" on the new db. With 8.6.4 it always came in a "1". Not sure if this is connected to why it is suddenly having issues.

The frustrating part is that it seems random and intermittent. Some times the model can fetch without issue. Other times it fails with a "Operation 'model.findOne()' buffering timed out after 10000ms" error.

@Chrysochrome
Copy link

I encountered the same issue. I upgraded Mongoose from ^8.6.1 to ^8.8.3, but the service is unable to connect to the database consistently.

I am using a serverless MongoDB Atlas database, deployed on Azure. The issue can be reproduced locally by attempting to connect to the database using Mongoose. The error log is as follows, and I hope this provides some help:

[2024-11-28T09:04:58.186Z] MongooseError: Operation `users.findOne()` buffering timed out after 10000ms
[2024-11-28T09:04:58.186Z]     at Timeout.<anonymous> (<<my_workspace>>/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:185:23)
[2024-11-28T09:04:58.186Z]     at listOnTimeout (node:internal/timers:581:17)
[2024-11-28T09:04:58.186Z]     at process.processTimers (node:internal/timers:519:7)

@christian-schwaderer
Copy link

Same for me on GCP. Tests pass and when deploying everything is fine for a few minutes. But then all queries fail with buffering timed out after 10000ms

domfarolino added a commit to domfarolino/push-notifications that referenced this issue Dec 9, 2024
We cannot upgrade to 8.7.0 or beyond, because I'm receiving the same connection
issues discussed in Automattic/mongoose#15015. So
further upgrades are paused until that is figured out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity
Projects
None yet
Development

No branches or pull requests

7 participants