Failed to connect MongoDB Atlas using SSH

203 Views Asked by At

I'm working on a service which should connect to MongoDB atlas server.
I've enabled all IPs using Atlas's configuration, which means that the SSH server is also in the whitelist, and disabled the firewall in the SSH server.
For some reason, I'm receiving the following error:
MongooseError [MongooseServerSelectionError]: Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.
    at new MongooseServerSelectionError (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\node_modules\mongoose\lib\error\serverSelection.js:24:11)
    at NativeConnection.Connection.openUri (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\node_modules\mongoose\lib\connection.js:823:32)
    at Mongoose.connect (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\node_modules\mongoose\lib\index.js:333:15)
    at Object.<anonymous> (C:\Users\anna james\Dropbox\MERN_SHOPPING_LIST\back\server.js:9:7)
    at Module._compile (internal/modules/cjs/loader.js:1157:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
    at Module.load (internal/modules/cjs/loader.js:1001:32)
    at Function.Module._load (internal/modules/cjs/loader.js:900:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  message: "Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.",
  name: 'MongooseServerSelectionError',

Note: When I try to connect it locally, it works perfectly.

Here's the connection code:

try {
    mongoose.set('useNewUrlParser',true);
    mongoose.set('useCreateIndex',true);  
    const uri = process.env.DB_URI;
    let connectionPromise = await mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
    if (mongoose.connection) {
        logger.info('Connected Successfully to DB');
        await loadMongooseModels();
        logger.info('Mongoose Models Successfully loaded from DB');
        global.connectionPromise = connectionPromise;
    } else { global.connectionPromise = null; 
            logger.warn('not connected to DB') }
    return connectionPromise;
  } catch (error) {
      logger.error('Error connecting to DB ::', error);
0

There are 0 best solutions below