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

101,890

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

  1. Log into your Mongo Atlas account
  2. Go to the security tab -> Network Access -> Then whitelist your IP
  3. Then go to the security tab -> Database Access -> and delete your current user.
  4. Create a new user by providing new username and password.
  5. 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.

Share:
101,890

Related videos on Youtube

Arvind Krmar
Author by

Arvind Krmar

Updated on July 09, 2022

Comments

  • Arvind Krmar
    Arvind Krmar almost 2 years

    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?

    • Shivam
      Shivam over 4 years
      Did you changed user:password to your username and password in connection string?
    • Arvind Krmar
      Arvind Krmar over 4 years
      @ShivamSood Yes, I did
    • Arvind Krmar
      Arvind Krmar over 4 years
      To 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
      Arvind Krmar over 4 years
      application is running well with local MongoDB "mongodb://127.0.0.1/FullStack". Appears connectivity to atlas MongoDB is the issue.
    • Arvind Krmar
      Arvind Krmar over 4 years
      I am able to solve it. Firewall was blocking access,the same could be tested with this: portquiz.net:27017
  • Arvind Krmar
    Arvind Krmar over 4 years
    Did that, but no luck. Rather I allowed all IPs, still did not work
  • Kunal Usapkar
    Kunal Usapkar over 4 years
    bro, have you set up the DB configuration properly
  • Arvind Krmar
    Arvind Krmar over 4 years
    I 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
    Arvind Krmar over 4 years
    it 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
    Arvind Krmar over 4 years
    I 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
    Arvind Krmar over 4 years
    it was a firewall issue at my end. I corrected it and now its working
  • Arvind Krmar
    Arvind Krmar over 4 years
    able 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
    Mike K over 4 years
    I 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
    okram almost 4 years
    I would suggest another first step before current first step saying "Log into your Mongo Atlas account". It would make things much clearer.
  • Sanya
    Sanya over 3 years
    Worked for me. Thank you!! I was getting " UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED" earlier. This hack is awesome!
  • s.j
    s.j about 3 years
    not seeing any MongoDB server in open services window.
  • BruceJo
    BruceJo over 2 years
    OMG. Cost me a day until I found this tip!! Thanks!
  • jkalandarov
    jkalandarov about 2 years
    What if 0.0.0.0 is in ip address which includes my IP address and the problem still persists?
  • alireza
    alireza about 2 years
    that worked for me as well if you are using MongoDB in local that's the answer.
  • Alex
    Alex about 2 years
    Thank 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
    benshabatnoam about 2 years
    I 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"