Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms

Tags:

I am trying to create a fullstack app reading the following tutorial:

https://medium.com/javascript-in-plain-english/full-stack-mongodb-react-node-js-express-js-in-one-simple-app-6cc8ed6de274

I followed all steps and then tried to run:

node server.js 

But I got the following error:

MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms at Timeout._onTimeout (C:\RND\fullstack_app\backend\node_modules\mongodb\lib\core\sdam\server_selection.js:308:9) at listOnTimeout (internal/timers.js:531:17) at processTimers (internal/timers.js:475:7) { name: 'MongoTimeoutError', reason: Error: connect ETIMEDOUT 99.80.11.208:27017 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) { name: 'MongoNetworkError', [Symbol(mongoErrorContextSymbol)]: {} }, [Symbol(mongoErrorContextSymbol)]: {} } (node:42892) UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms at Timeout._onTimeout (C:\RND\fullstack_app\backend\node_modules\mongodb\lib\core\sdam\server_selection.js:308:9) at listOnTimeout (internal/timers.js:531:17) at processTimers (internal/timers.js:475:7)

My code at server.js is as follows:

const mongoose = require('mongoose'); const router = express.Router();  // this is our MongoDB database const dbRoute =     'mongodb+srv://user:<password>@cluster0-3zrv8.mongodb.net/test?retryWrites=true&w=majority';  mongoose.Promise = global.Promise;  // connects our back end code with the database mongoose.connect(dbRoute,      {   useNewUrlParser: true,         useUnifiedTopology: true     });  let db = mongoose.connection;  db.once('open', () => console.log('connected to the database')); 

Any suggestions?

like image 891
Arvind Krmar Avatar asked Dec 03 '19 17:12

Arvind Krmar


People also ask

Why am I getting a mongotimeouterror in MongoDB?

Version 3.3.3 of the driver which was published today should add more details to the MongoTimeoutError. It is also recommended to update the driver due to a bugfix. This error is the result of the driver being unable to connect to a server satisfying the read preference of a given operation (or initial connect).

Why do I get a mongotimeouterror on initial CONNECT?

That error is being caused by the driver's inability to do an initial connect to a cluster, which would indicate some sort of configuration (connection string) or network error on your part. With the unified topology this will show up as a MongoTimeoutError with a reason field indicating the network error that caused the timeout on initial connect.

Why can't I connect to the Mongo cluster?

Please check your connection string and network configuration and verify that you are able to connect to the cluster (perhaps with the mongo shell). If you're still experiencing issues, please open a jira ticket so we can continue conversation there, and not confuse people looking at this GitHub issue

How to check if MongoDB is running or not?

-Thanks How would someone check if it is running? there are few ways but i would recommend to use ROBO 3T. Create a connection and then press on test. If MongoDB is running you should be connected but if there is firewall issue or mongoDB not running the connection will not be establish. Show activity on this post.


2 Answers

just go to mongodb atlas admin panel.Go in security tab>Network Access> Then whitelist your IP by adding it

See this image to locate the particular menu

Note:Check your IP on google then add it

like image 69
Kunal Usapkar Avatar answered Oct 03 '22 20:10

Kunal Usapkar


Sometimes this error occurs due to the IP Address given access to in our database.

Mind you, your application can be working well then come up with such an error. Anytime this happens, most times your IP address has changed, which is not on the whitelist of addresses allowed. (IP addresses can be dynamic and are subject to change)

All you have to do is to update the whitelist with your current IP addresses

like image 32
Oluwatimilehin Odubola Avatar answered Oct 03 '22 22:10

Oluwatimilehin Odubola