Unable to connect to Mongodb Atlas using mongoose

Viewed 2895

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.

2 Answers

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.

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

  3. 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"); } });

EDIT 1: After having a chat with 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.

After trying different connection strings for several hours, I finally just copy/pasted the connection string from MongoDB Compass and it works! (first connect, then edit the connection string as it will change)

It looks like this:

mongodb+srv://username:password@fra-atlas-shard.abcde.mongodb.net/test?authSource=admin&replicaSet=atlas-abcde-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true

Related