skip to Main Content

i’m learning MongoDB and i’m sorry to bother you but i’m getting this error:

MongoServerSelectionError: connection <monitor> to xx.xxx.xxx.xxx:27017 closed
at Timeout._onTimeout (C:...node_modulesmongodblibsdamtopology.js:305:38)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
  'ac-c9obg9r-shard-00-00.onq7cwz.mongodb.net:27017' => [ServerDescription],
  'ac-c9obg9r-shard-00-02.onq7cwz.mongodb.net:27017' => [ServerDescription],
  'ac-c9obg9r-shard-00-01.onq7cwz.mongodb.net:27017' => [ServerDescription]
},


stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'atlas-up12ch-shard-0',
logicalSessionTimeoutMinutes: undefined
},

  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

I have tried to enable the port 27017 and resetting the ip in the network access tab (was white listed already), but no luck, error persists. Reinstalled the modules I used, and nothing.
My code was working yesterday, but after a Windows update i can’t connect (that’s why i thought it was the port).
The digits I replaced xx.xxx.xxx.xxx:27017 are not my ip number, i don’t know if that helps.

If u have any ideas, I apreciate your input.

3

Answers


  1. MongoServerSelectionError: connection <monitor> to xx.xxx.xxx.xxx:27017 closed 
        at Timeout._onTimeout (C:...node_modulesmongodblibsdamtopology.js:305:38)
        at listOnTimeout (node:internal/timers:559:17)
        at processTimers (node:internal/timers:502:7)
    

    I had this same error while trying to connect to MongoDB with a new Node server.

    See if you have changed your network connection to a diferent network than the one you have whitelisted in MongoDB.
    If so, whitelist the current IP adress in mongoDB.
    (PS: I see you have already tried resetting the IP address in the Network Access tab, but check again. This was how i fixed it.)

    Also, check and see if the .env file variables are correctly declared with MongoDB link and also change the password and database name.

    Still if the issue is not solved, I would suggest you to delete the old cluster and create a new one in MongoDB. Also re-initialise node and the packages.

    Hope this solves your problem.

    Login or Signup to reply.
  2. For me this error was occurring because my connection string was wrong.To be very specific – I copied the sample connection string from a course I was learning and just replaced the username and password with my credentials. So, the credentials were right but not the rest of the connection string.

    Just for the sake of understanding. Please see below :

    mongodb+srv://myusername:[email protected]/?retryWrites=true&w=majority"

    myusername and mypassword are correct i.e belong to the cluster in my atlas account but the rest of the string is wrong as I copied it from somewhere instead of copying it from my own MongoDB atlas account.

    So please make sure to double check if your entire connection string is correct.

    Login or Signup to reply.
  3. import { MongoClient } from 'mongodb'
    const uri = process.env.MONGODB_URI
    const options = {
      useNewUrlParser: true, 
      useUnifiedTopology: true, 
    }
    
    client = new MongoClient(uri, options)
    clientPromise = client.connect();
    
    export default clientPromise
    

    Inside your .env you could insert something like this:

    MONGODB_URI=mongodb+srv://username:[email protected]/DatabaseName?retryWrites=true&w=majority
    

    Code snippet for connnecting mongodb will be available at https://cloud.mongodb.com/, navigate to your cluster and click connect then click Connect your application. enter image description here

    Finally copy code snippet whatever you get at your time of mongodb version after choosing include full driver code example and implement it into your application:
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search