I'm trying to connect to my cluster on mongoDB Atlas via Mongoose.connect(), but every time i try to connect i get an exception "MongoError: authentication fail" I know MongoDB Atlas is new mongo as a service could it be not supported by mongoose yet?.
-
Can you show the connection string that you're using (but please replace username and password with placeholders). Also, take a look [at this](https://github.com/Automattic/mongoose/issues/4818). – robertklep Apr 13 '17 at 13:54
-
'mongodb://username:
@hcluster0-shard-00-00-he3ln.mongodb.net:27017,hcluster0-shard-00-01-he3ln.mongodb.net:27017,hcluster0-shard-00-02-he3ln.mongodb.net:27017/ – M Hilal Apr 13 '17 at 14:02?ssl=true&replicaSet=hcluster0-shard-0&authSource=admin' -
1Is `
` the actual name of your database? Also, if `password` contains any characters that have a special meaning in URI's (like `@`, `+`, `%`, `/`), you need to encode those. – robertklep Apr 13 '17 at 14:07 -
Have you added your public IP address to the whitelist? – helmy Apr 13 '17 at 14:14
-
Yes it's the actual name(But it's not important I'm using it just for testing, no worries). But I was using mLab before and I didn't have to encode anything. Yes I've added a public IP address to the whitelist. – M Hilal Apr 13 '17 at 14:16
-
Can you verify that you are able to connect using the mongo shell? – helmy Apr 13 '17 at 14:20
-
Possible duplicate of [Mongoose with ReplicaSet on Atlas](https://stackoverflow.com/questions/41213148/mongoose-with-replicaset-on-atlas) – Juan Arias Sep 19 '17 at 02:43
-
use directConnection: true, on connect options – Sibelius Seraphini Jun 29 '22 at 14:37
5 Answers
The answer in this related post is correct. You should:
- not mix options with connection string (if done so)
- make sure your IP you are running on is whitelisted and your network allows connections to Atlas
- make sure the user has sufficient permissions
use the connection string as is provided by atlas and just provide it to
mongoose.connect(uri);

- 6,304
- 7
- 29
- 61
-
1
-
1This works, but special characters need to be typed as literals, [reference](https://docs.atlas.mongodb.com/troubleshoot-connection/#special-characters-in-connection-string-password). I have found out this problem with my password – May 07 '20 at 02:21
MongoError: authentication fails - It means your name or password or dbname is not correct -
uri sample -
const uri =
"mongodb+srv://<username>:<password>@firstcluster.4rc4s.mongodb.net/<dbname>?retryWrites=true&w=majority";
Suppose username is - najim & password is 1234 & dbname is pets (Note - default dbname is test but you can write whatever you want) then my uri will be with above credentails -
const mongoAtlasUri =
"mongodb+srv://najim:1234@firstcluster.4rc4s.mongodb.net/pets?retryWrites=true&w=majority";
to connect with moongoose
try {
// Connect to the MongoDB cluster
mongoose.connect(
mongoAtlasUri,
{ useNewUrlParser: true, useUnifiedTopology: true },
() => console.log(" Mongoose is connected")
);
} catch (e) {
console.log("could not connect");
}

- 191
- 1
- 4
const mongoAtlasUri =
"mongodb+srv://<username>:<password>@firstcluster.4rc4s.mongodb.net/<dbname>?retryWrites=true&w=majority";
try {
// Connect to the MongoDB cluster
mongoose.connect(
mongoAtlasUri,
{ useNewUrlParser: true, useUnifiedTopology: true },
() => console.log(" Mongoose is connected"),
);
} catch (e) {
console.log("could not connect");
}
const dbConnection = mongoose.connection;
dbConnection.on("error", (err) => console.log(`Connection error ${err}`));
dbConnection.once("open", () => console.log("Connected to DB!"));

- 1,032
- 1
- 11
- 32

- 71
- 1
- 1
try {
mongoose.connect( uri, {useNewUrlParser: true, useUnifiedTopology: true}, () =>
console.log("connected"));
}catch (error) {
console.log("could not connect");
}
this works fine , try it

- 61
- 1
- 3
"mongodb+srv://:@cluster0.vvkuk.mongodb.net/" also in the atlas, in security go to network access, there will be small buttons edit and delete click on edit, and in the edit, there will to two options the first option is ADD CURRENT IP ADRESS & the second option will be ALLOW ACCESS FROM ANYWHERE go for the first option & then click confirm

- 1
- 1