suigetsuh17
suigetsuh17

Reputation: 75

Error connecting to Atlas Free Cluster (MongoDB)

TL;DR: Can't connect to Atlas Cluster even after doing exactly what docs said.

Hi, so I read the docs of getting started with Atlas and everything seemed nice & easy. I did follow the steps, created a free cluster, whitelisted my IP, and then tried to connect using their sample app:

const { MongoClient } = require("mongodb");

// Replace the following with your Atlas connection string                                                                                                                                        
const url = "mongodb+srv://<username>:<password>@clustername.mongodb.net/test?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true";
const client = new MongoClient(url);

async function run() {
    try {
        await client.connect();
        console.log("Connected correctly to server");

    } catch (err) {
        console.log(err.stack);
    }
    finally {
        await client.close();
    }
}

run().catch(console.dir);

But the following error occurred when I tried to execute with: node connect.js

PS C:\Users\marjo\Documents\mongoDB Atlas> node connect
(node:11352) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
MongoNetworkError: failed to connect to server [remote-doc-shard-00-02-otc5a.mongodb.net:27017] on first connect [MongoError: bad auth Authentication failed.
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\auth\auth_provider.js:46:25
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\auth\scram.js:240:11
    at _callback (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:349:5)
    at Connection.messageHandler (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:378:5)
    at Connection.emit (events.js:315:20)
    at processMessage (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connection.js:384:10)
    at TLSSocket.<anonymous> (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connection.js:553:15)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:273:9) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError'
}]
    at Pool.<anonymous> (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\topologies\server.js:438:11)
    at Pool.emit (events.js:315:20)
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\pool.js:561:14
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\pool.js:1008:9
    at callback (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:97:5)
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:396:21
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\auth\auth_provider.js:66:11
    at C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\auth\scram.js:240:11
    at _callback (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:349:5)
    at Connection.messageHandler (C:\Users\marjo\Documents\mongoDB Atlas\node_modules\mongodb\lib\core\connection\connect.js:378:5)

I tried changing the connection string with the one from Atlas: (because it was different from the docs by a tiny bit)

const uri = "mongodb+srv://Marjo:<password>@remote-doc-otc5a.mongodb.net/<dbname>?retryWrites=true&w=majority";

But still the same result. My password had a !character so I put %21 instead of it. I also replaced with cluster name (Remote-Doc) and test but it still failed.

I'd appreciate if you could help me!

Upvotes: 0

Views: 564

Answers (2)

valdeci
valdeci

Reputation: 15237

I think that you are having a problem with the parse of your password, maybe it has special characters.

The best way to handle this is to change the way that you are connecting to pass the user and password as options.

You can follow the doc and change your MongoClient conection for something like this:

const mongoclient = new MongoClient(new Server("remote-doc-otc5a.mongodb.net", 27017));

// Listen for when the mongoclient is connected
mongoclient.open(function (err, mongoclient) {

    // Then select a database
    const db = mongoclient.db("dbname");

    // Then you can authorize your self
    db.authenticate('username', 'password', (err, result) => {
        // On authorized result=true
        // Not authorized result=false

        // If authorized you can use the database in the db variable
    });
});

And with mongoose you can do something like this:

mongoose.connect('mongodb+srv://@remote-doc-otc5a.mongodb.net/test?retryWrites=true&w=majority', {
    user: 'USERNAME',
    pass: 'PASSWORD',
    useNewUrlParser: true,
    useUnifiedTopology: true
})

Also, check if you are not using the account password instead of the cluster/database password.

You can follow this tutorial to check if you are using the correct one: MongoDB Atlas Setup - Digital Ocean.

Upvotes: 1

suigetsuh17
suigetsuh17

Reputation: 75

I just changed the Atlas password to a simple one with no special characters, and the connection worked! I feel ashamed now

Upvotes: 0

Related Questions