Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Message: MongoError: bad auth Authentication failed through URI string

I'm trying to connect to my mongoDB server via the connection string given to me by mongo:

"mongodb+srv://david:[email protected]/test?retryWrites=true"

In my code I am calling the connection through mongoose like this (obviously putting in my password):

const mongoose = require('mongoose');
const db = 'mongodb+srv://david:<password>@cluster0-re3gq.mongodb.net/test?retryWrites=true'
mongoose
    .connect(db, { 
        useNewUrlParser: true,
        useCreateIndex: true
      })
    .then(() => console.log('MongoDB connected...'))
    .catch(err => console.log(err));

When I run the code I am getting the following error

"MongoError: bad auth Authentication failed."

Any ideas of what that could mean?

like image 727
Djaenike Avatar asked Apr 15 '19 18:04

Djaenike


3 Answers

I had the same problem, and in my case, the answer was as simple as removing the angle brackets "<"and ">" around <password>. I had been trying: my_login_id:<my_password>, when it should have been my_login_id:my_password.

like image 156
Crowdpleasr Avatar answered Oct 08 '22 06:10

Crowdpleasr


I think you're confused with the mongodb account password and user password. You should use user password, not account password. That was the reason of my case.

like image 22
Alexander Avatar answered Oct 08 '22 08:10

Alexander


It happens because your provided password in connection string is wrong and most probably you have mistaken cluster password with your login password, in simple words while connecting with Atlas Cluster we can't use our account password by which we login to the Atlas website. In both case we can reset our cluster password and solve this issue.

To Solve The Issue Follow Below Given Steps

Step 1:- Click Database Access From left Side Navigation of MongoDB Atlas page.

Step 2:- Select your username and and click on the edit button from right side.

Step 3:- Click to change password.

Step 4:- Click update user.

While changing password try to keep password only alphabetical because special characters need encoding. See The Image For Your Better Understanding

that's all now you can connect.

like image 67
Pawan Bishnoi Avatar answered Oct 08 '22 06:10

Pawan Bishnoi