Search code examples
mongodbmongoosemongodb-atlas

Unable to connect to Mongodb Atlas using mongoose


I am trying to connect to a cluster created in Mongodb Atlas using mongoose in node js and I am facing below issues when doing so.

  1. When I use the connection string that is given in the Mongo db atlasmongodb+srv://lm_dev_app:<password>@lmdev-q5biw.mongodb.net/test?retryWrites=true&w=majorityI get below error

    { Error: queryTxt EBADNAME lmdev-q5biw.mongodb.net at QueryReqWrap.onresolve [as oncomplete] (dns.js:196:19) errno: 'EBADNAME', code: 'EBADNAME', syscall: 'queryTxt', hostname: 'lmdev-q5biw.mongodb.net'}

I cannot use this connection string in Mongodb Compass as well as I am getting the same error there.

  1. If I try to connect using mongodb://lm_dev_app:<password>@lmdev-shard-00-01-q5biw.mongodb.net/test i get below error

    MongooseServerSelectionError: connection to 54.66.221.230:27017 closed

However I am able to connect to each node using Mongodb Compass which eliminates the possibility of my ipaddress not being whitelisted.

Here is the sample code that I am using

const mongoosePromise = mongoose.connect("mongodb://lm_dev_app:<password>@lmdev-shard-00-01-q5biw.mongodb.net/test", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    replicaSet: "LMDEV"
}, (err) => {
    if (err) {
        console.log(err);
    } else {
        console.log("Successful");
    }
});

Any thoughts on what is happening here.


Solution

  • There are couple of things that I need to highlight here.

    1. The default connection string that is shown in MongoDB Atlas seems to be wrong. It shows you:
    mongodb+srv://<username>:<password>@<cluster_url>/test?retryWrites=true&w=majority
    

    But I used mongodb://<username>:<password>@<node_url>:27017/ to make it work.
    You can also use mongodb://<username>:<password>@<node_url>:27017/admin.

    1. Pass ssl:true in the options that we are passing.

    2. Finally one of the 3 options can be used to connect to the database:

    a.

    const mongoosePromise = mongoose.connect("mongodb://lm_dev_app:<password>@lmdev-shard-00-01-q5biw.mongodb.net:27017/", {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            authSource:"admin",
            ssl: true,
        }, (err) => {
            if (err) {
                console.log(err);
            } else {
                console.log("Successful");
            }
        });
    

    b.

    const mongoosePromise = mongoose.connect("mongodb://lm_dev_app:<password>@lmdev-shard-00-01-q5biw.mongodb.net:27017/", {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            authSource:"admin",
            ssl: true,
        }, (err) => {
            if (err) {
                console.log(err);
            } else {
                console.log("Successful");
            }
        });
    

    c.

    const mongoosePromise = mongoose.connect("mongodb://lm_dev_app:<password>@lmdev-shard-00-01-q5biw.mongodb.net:27017/admin", {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            ssl: true,
        }, (err) => {
            if (err) {
                console.log(err);
            } else {
                console.log("Successful");
            }
        });
    

    After having a chat with the Atlas support team I was told that issue in point 1 is due to DNS resolution issue with my service provider. So I have changed my DNS settings to point to a public DNS server.