Cannot connect to MongoDB on Atlats

What is the current behavior?
The connection to a MongoDB hosted on Mongo Atlas suddenly throws out an error. I have been searching on the Internet (Stackoverflow, …) for a day about this bug, but haven’t been able to figure out why it happens. Here are the detailed configs and error:

  • Connections from any IPs are allowed

  • Connection code

    import mongoose from 'mongoose'
    try {
        await mongoose.connect(process.env.MONGO_URI)
        console.log('Connected to MongoDB')
    } catch (err) {
        console.error(err)
    }
    
  • Connection string’s format

    process.env.MONGO_URI = mongodb+srv://<username>:<password>@<cluster>.ig6hm.mongodb.net/<dbname>?retryWrites=true&w=majority
    

mongodb+srv://devdb-admin:<password_is_hide_here>@devcluster.ig6hm.mongodb.net/auth?retryWrites=true&w=majority


- Error
  ```shell
  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://docs.atlas.mongodb.com/security-whitelist/
      at NativeConnection.Connection.openUri (/home/node/app/node_modules/mongoose/lib/connection.js:796:32)
      at /home/node/app/node_modules/mongoose/lib/index.js:328:10
      at /home/node/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
      at new Promise (<anonymous>)
      at promiseOrCallback (/home/node/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
      at Mongoose._promiseOrCallback (/home/node/app/node_modules/mongoose/lib/index.js:1149:10)
      at Mongoose.connect (/home/node/app/node_modules/mongoose/lib/index.js:327:20)
      at /home/node/app/src/index.ts:33:20
      at step (/home/node/app/src/index.ts:33:23)
      at Object.next (/home/node/app/src/index.ts:14:53) {
  reason: TopologyDescription {
      type: 'ReplicaSetNoPrimary',
      servers: Map(3) {
      'devcluster-shard-00-01.asdf1234.mongodb.net:27017' => [ServerDescription],
      'devcluster-shard-00-00.asdf1234.mongodb.net:27017' => [ServerDescription],
      'devcluster-shard-00-02.asdf1234.mongodb.net:27017' => [ServerDescription]
      },
      stale: false,
      compatible: true,
      heartbeatFrequencyMS: 10000,
      localThresholdMS: 15,
      setName: 'atlas-11mhge-shard-0',
      logicalSessionTimeoutMinutes: undefined
  }
  ```

Here is the content of the `err.reason.servers`:

```shell
Map(3) {
'devcluster-shard-00-00.ig6hm.mongodb.net:27017' => ServerDescription {
  _hostAddress: HostAddress {
    isIPv6: false,
    host: 'devcluster-shard-00-00.ig6hm.mongodb.net',
    port: 27017
  },
  address: 'devcluster-shard-00-00.ig6hm.mongodb.net:27017',
  type: 'Unknown',
  hosts: [],
  passives: [],
  arbiters: [],
  tags: {},
  minWireVersion: 0,
  maxWireVersion: 0,
  roundTripTime: -1,
  lastUpdateTime: 1164506954,
  lastWriteDate: 0,
  error: MongoNetworkError: certificate is not yet valid
      at connectionFailureError (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:390:14)   
      at TLSSocket.<anonymous> (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:358:16)    
      at Object.onceWrapper (node:events:514:26)
      at TLSSocket.emit (node:events:394:28)
      at emitErrorNT (node:internal/streams/destroy:157:8)
      at emitErrorCloseNT (node:internal/streams/destroy:122:3)
      at processTicksAndRejections (node:internal/process/task_queues:83:21)
},
'devcluster-shard-00-01.ig6hm.mongodb.net:27017' => ServerDescription {
  _hostAddress: HostAddress {
    isIPv6: false,
    host: 'devcluster-shard-00-01.ig6hm.mongodb.net',
    port: 27017
  },
  address: 'devcluster-shard-00-01.ig6hm.mongodb.net:27017',
  type: 'Unknown',
  hosts: [],
  passives: [],
  arbiters: [],
  tags: {},
  minWireVersion: 0,
  maxWireVersion: 0,
  roundTripTime: -1,
  lastUpdateTime: 1164506989,
  lastWriteDate: 0,
  error: MongoNetworkError: certificate is not yet valid
      at connectionFailureError (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:390:14)   
      at TLSSocket.<anonymous> (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:358:16)    
      at Object.onceWrapper (node:events:514:26)
      at TLSSocket.emit (node:events:394:28)
      at emitErrorNT (node:internal/streams/destroy:157:8)
      at emitErrorCloseNT (node:internal/streams/destroy:122:3)
      at processTicksAndRejections (node:internal/process/task_queues:83:21)
},
'devcluster-shard-00-02.ig6hm.mongodb.net:27017' => ServerDescription {
  _hostAddress: HostAddress {
    isIPv6: false,
    host: 'devcluster-shard-00-02.ig6hm.mongodb.net',
    port: 27017
  },
  address: 'devcluster-shard-00-02.ig6hm.mongodb.net:27017',
  type: 'Unknown',
  hosts: [],
  passives: [],
  arbiters: [],
  tags: {},
  minWireVersion: 0,
  maxWireVersion: 0,
  roundTripTime: -1,
  lastUpdateTime: 1164507365,
  lastWriteDate: 0,
  error: MongoNetworkError: certificate is not yet valid
      at connectionFailureError (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:390:14)   
      at TLSSocket.<anonymous> (/home/node/app/node_modules/mongoose/node_modules/mongodb/src/cmap/connect.ts:358:16)    
      at Object.onceWrapper (node:events:514:26)
      at TLSSocket.emit (node:events:394:28)
      at emitErrorNT (node:internal/streams/destroy:157:8)
      at emitErrorCloseNT (node:internal/streams/destroy:122:3)
      at processTicksAndRejections (node:internal/process/task_queues:83:21)
}
}

Are you able to connect to this Atlas cluster using the mongo shell from the command line?

No, I tried but wasn’t able to connect to the DB on Atlas using MongoDB Shell.

>mongosh "mongodb+srv://devcluster.ig6hm.mongodb.net/auth" --username devdb-admin
Enter password: ****************
Current Mongosh Log ID: 6159072f46e048ec94c0c683
Connecting to:          mongodb+srv://devcluster.ig6hm.mongodb.net/auth
Error: querySrv EREFUSED _mongodb._tcp.devcluster.ig6hm.mongodb.net

But I was able to connect to the MongoDB on Atlas using MongoDB Compass with the same username and password (however, I need to downgrade the connection to a form of mongodb:// instead of mongodb+srv:// even though my Compass's version is 1.28.4 which should be able to connect to the DB using a connection string with the format of mongodb+srv://). Another strange thing is that the connection was successful for the last 2 months and suddenly it becomes unsuccessful while no changes were added to the source code and no changes were made to the configuration of the DB Cluster or users and network access on the Atlas as well.

What are the versions of Node.js, Mongoose, MongoDB and TypeScript you are using?

  • Nodejs: v16.8.0
  • Mongoose: 6.0.8
  • MongoDB (Atlas): 4.4.9
  • TypeScript: 4.3.5

Please ensure that you’ve opened up a support case so that we can get you the assistance you need here (you can open up the chat in the lower right in the Atlas UI)

1 Like

@Andrew_Davidson Thanks for your reply. The issue was fixed after I waited for a couple of days - without changing any configurations of MongoDB on Atlas or source code. I still don’t get the reasons why the connection error occurred, but at least now it works again.

I had the same problem after resetting my router. You have to set your IP again.