MongoDB connection error: MongoTimeoutError: Server selection timed out after 30000 ms
Solution 1
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
Solution 2
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
Solution 3
I wasted whole day on this because the whitelist suggestion was not my issue (I'm running in docker compose with --bind_ip
option set properly and I can see the Connection accepted
in the mongo logs each time my client tried to connection).
It turns out, I simply needed to add this to the end of my connection string in the code:
?directConnection=true
connection string:
mongodb://myUserId:myPassword@mongodb/myDatabase?directConnection=true
I hope mongodb documents this better because I only stumbled across it from looking at the mongosh logs when I connected using that client.
Solution 4
Ensure that the "<" and ">" are removed when you replace the user and password fields. This worked for me
Solution 5
I got the same error. These are the steps I followed for resolve it
- Log into your Mongo Atlas account
- Go to the security tab -> Network Access -> Then whitelist your IP
- Then go to the security tab -> Database Access -> and delete your current user.
- Create a new user by providing new username and password.
- Provide that newly created username and password in the .env file or mongoose.connect method
After these steps it worked as usual. hope this will help you guys.
Related videos on Youtube
Comments
-
Arvind Krmar almost 2 years
I am trying to create a fullstack app reading the following tutorial:
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?
-
Shivam over 4 yearsDid you changed
user:password
to yourusername and password
in connection string? -
Arvind Krmar over 4 years@ShivamSood Yes, I did
-
Arvind Krmar over 4 yearsTo add to the info, I tried to connect using MongoDB compass community but it gave the same error. Can it be some settings at atLas Mongodb?
-
Arvind Krmar over 4 yearsapplication is running well with local MongoDB "mongodb://127.0.0.1/FullStack". Appears connectivity to atlas MongoDB is the issue.
-
Arvind Krmar over 4 yearsI am able to solve it. Firewall was blocking access,the same could be tested with this: portquiz.net:27017
-
-
Arvind Krmar over 4 yearsDid that, but no luck. Rather I allowed all IPs, still did not work
-
Kunal Usapkar over 4 yearsbro, have you set up the DB configuration properly
-
Arvind Krmar over 4 yearsI have created roles for db access: username: arvind Authentication method: SCRAM MongoDBRoles: atlasAdmin@admin Do I need to create something in cluster or 'Data Lake'?
-
Arvind Krmar over 4 yearsit gives the following error when { useUnifiedTopology: true } removed: (node:10020) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
-
Arvind Krmar over 4 yearsI found the solution. The issue was in firewall settings at my network system. portquiz.net:27017 can be used to test if the connection is through. Though i did not change the firewall as that is managed by network admins at my end, but once the firewall corrected its working now. Thanks everybody for suggestions.
-
Arvind Krmar over 4 yearsit was a firewall issue at my end. I corrected it and now its working
-
Arvind Krmar over 4 yearsable to solve it, everything was correct, but the firewall was blocking access to port 27017 same can be tested here:portquiz.net:27017
-
Mike K over 4 yearsI was running into the same issue, however, I frequently develop with my VPN toggled on and off. By turning my VPN on again, I managed to connect to the DB. I'm hoping this issue is temporary, because I can't always have my VPN on.
-
okram almost 4 yearsI would suggest another first step before current first step saying "Log into your Mongo Atlas account". It would make things much clearer.
-
Sanya over 3 yearsWorked for me. Thank you!! I was getting " UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED" earlier. This hack is awesome!
-
s.j about 3 yearsnot seeing any MongoDB server in open services window.
-
BruceJo over 2 yearsOMG. Cost me a day until I found this tip!! Thanks!
-
jkalandarov about 2 yearsWhat if 0.0.0.0 is in ip address which includes my IP address and the problem still persists?
-
alireza about 2 yearsthat worked for me as well if you are using MongoDB in local that's the answer.
-
Alex about 2 yearsThank you. Same for me running Node 17.4.0 - There's probably a way to get Node resolving localhost but makes no difference to me so using 127.0.0.1 was a good quick fix
-
benshabatnoam about 2 yearsI was so happy to see this answer but it didn't help me unfortunautly as I'm using 'SRV' connection string. This is the error I'm getting after trying this solutions: "MongoAPIError: SRV URI does not support directConnection"