How To Solve MongoDB MongoTimeoutError: Server selection timed out after 30000 ms

Share This Post

Now a Days Many users facing connection error: MongoTimeoutError: Server selection timed out after 30000 ms Error in mongoDB/node.js. Here Is Our Best And Possible All solution.

What is MongoDB connection timeout?

The connection timeout determines the maximum amount of response time that waits for a connection established by the server.

This value is used when making an initial connection to the MongoDB database.

The default connection timeout value ranges from 1 second to 30 seconds.

For example, if you have the connection timeout set to 30 seconds, your applications will never wait more than 30 seconds for the result.

Solution 1

What is The Version of npm Are You Using ? npm mongojs version is the issue. There is a bug in mongojs 3.3.3 npm. If you have installed mongojs3.3.3 Then You Definetly Facing this error.

Need to follow below steps in terminal.

  1. rm -rf mongojs or npm uninstall mongojs
  2. npm install [email protected]

Solution 2

mongoose.connect returns a promise so you need a catch for the errors. Honestly, Using mongoose.connection is something I don’t do but I’m not an expert. I usually handle the connection success with a callback and the failure with a .catch like so:

mongoose.connect( uri, { useNewUrlParser: true}, () => { console.log("we are connected")}).catch(err => console.log(err));

and leave out the mongoose.connection stuff completely.

by passing the error into the catch block it tells you if something goes wrong during the connection. otherwise, you would just have failing api calls with errors that tell you nothing because you didn’t give it a way to tell you the error.

the callback tells you whenever it connects to the db so that’s why you don’t need that extra three lines of code that is only there to tell you that you are connected.

hope this helps.

Solution 3

Server selection timed out after 30000 ms You May Whitelist your connection IP address. Atlas only allows client connections to the cluster from entries in the project’s whitelist. The project whitelist is distinct from the API whitelist, which restricts API access to specific IP or CIDR addresses.


You can skip this step if Atlas indicates in the Setup Connection Security step that you have already configured a whitelist entry in your cluster. To manage the IP whitelist, see Add Entries to the Whitelist.

If the whitelist is empty, Atlas prompts you to add an IP address to the project’s whitelist. You can either:

Click Add Your Current IP Address to whitelist your current IP address.

Click Add a Different IP Address to add a single IP address or a CIDR-notated range of addresses.

For Atlas clusters deployed on Amazon Web Services (AWS) and using VPC Peering, you can add a Security Group associated with the peer VPC.

You can provide an optional description for the newly added IP address or CIDR range. Click Add IP Address to add the address to the whitelist.

I Hope Above 3 of atleast one solution may help you. thank you. if solution worked then please share this solution.

Leave a Reply

Your email address will not be published. Required fields are marked *