2
votes

My connection code is entered below. I'm trying to connect to the MongoDB atlas free shared cluster and I keep getting an error saying cannot connect to the server in the first time.

    const express = require("express");
    const app = express();
    const morgan = require("morgan");
    const bodyParser = require("body-parser");
    const mongoose = require("mongoose");
    mongoose.Promise = require('bluebird');

    const productRoutes = require("./api/routes/products");
    const orderRoutes = require("./api/routes/orders");

    mongoose.connect(
      "mongodb://username:password6@cluster0-shard-00-00-3xdjv.mongodb.net:27017,cluster0-shard-00-01-3xdjv.mongodb.net:27017,cluster0-shard-00-02-3xdjv.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin",
      {
        useMongoClient: true
      }
    ) .then(() => { // if all is ok we will be here
      return server.start();
    })
    .catch(err => { // we will not be here...
      console.error('App starting error:', err.stack);
      process.exit(1);
    });

Can someone explain why I keep getting this error:

App starting error:

MongoError: failed to connect to server [cluster0-shard-00-00-3xdjv.mongodb.net:27017] on first connect [MongoError: getaddrinfo EAI_AGAIN cluster0-shard-00-00-3xdjv.mongodb.net:27017].....

1
Can you ping the remote server? Can you then connect with RoboMongo/Robo 3T? The error just means it couldn't connect so standard network diagnostics apply... - Nick

1 Answers

0
votes

I had the same problem before I realized that I had set up a network access rule allowing only connections from a certain IP. After deleting that restriction and adding a new rule for allowing all connections from anywhere, the problem was gone

My datasource config is as follows:

  "mongodb-atlas": {
    "url": "mongodb+srv://dbuser:PaSsWOrd@cluster0-oxxxb.gcp.mongodb.net/test?retryWrites=true&w=majority&authSource=admin",
    "name": "mongodb-atlas",
    "connector": "mongodb",
    "database": "admin"
  }