How to correct Mongodb atlas error(mongooseServerSelection Error your IP is not whitelist at atlas couldn’t connect to any server) |Projectshop

To correct the “mongooseServerSelection Error: Your IP is not whitelisted at Atlas, couldn’t connect to any server” error in MongoDB Atlas, follow these step-by-step instructions:

1. Login to MongoDB Atlas Dashboard:

Log in to your MongoDB Atlas account.

2. Whitelist Your IP Address:

  • Navigate to the “Network Access” section in the MongoDB Atlas dashboard.
  • Click on the “Add IP Address” button.
  • Add your current IP address to the whitelist. This allows your current IP address to connect to your MongoDB Atlas cluster.

3. Update Connection String:

  • Once your IP address is whitelisted, update your MongoDB connection string in your application code.
  • The connection string should include the username, password, cluster address, and database name.
  • Ensure that the connection string is correctly updated with the new whitelist IP address.

    4. Test Connection:

    • Run your application code again.
    • Ensure that your application can now connect to the MongoDB Atlas cluster without encountering the “mongooseServerSelection Error” message.

    Here’s an example of how you can update your MongoDB connection code in a Node.js application using Mongoose:

    JavaScript
    const mongoose = require('mongoose');
    
    // MongoDB connection options
    const options = {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
      useFindAndModify: false,
      // Add your MongoDB Atlas connection string here
      // Format: mongodb+srv://<username>:<password>@<cluster-address>/<database-name>
      // Example: mongodb+srv://myusername:mypassword@cluster0.abcd.mongodb.net/mydatabase
      uri: 'mongodb+srv://myusername:mypassword@cluster0.abcd.mongodb.net/mydatabase'
    };
    
    // Connect to MongoDB Atlas
    mongoose.connect(options.uri, options)
      .then(() => console.log('Connected to MongoDB Atlas'))
      .catch((error) => console.error('Error connecting to MongoDB Atlas:', error));

    Ensure that you replace<username>‘,’ <password>‘, ‘<cluster-address>‘, and<database-name>placeholders with your actual MongoDB Atlas credentials.

    Following these steps should resolve the “mongooseServerSelection Error” and allow your application to connect to MongoDB Atlas successfully. If you encounter any further issues, feel free to reach out for assistance.

    Leave a Comment

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

    Shopping Cart