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 · 6 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

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

4 participants