Error Message: MongoError: bad auth Authentication

2020-06-30 07:30发布

问题:

Very new to MongoDB and Node.js. I'm trying to connect to my mongoDB server via the connection string given to me by mongo:

"mongodb+srv://david:password@cluster0-re3gq.mongodb.net/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? Thanks!

回答1:

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.



回答2:

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.



回答3:

This Happens because entered password is wrong.Because you are entering login password not cluster one.

Solution ===> step 1. click Database Access From left Side Navigation of mongodb atlas page.

step 2. select your username and and click to edit from right side.

step 3. click to change password.

step 4. click update user.

During change password keep password only alphabetical because special characters need encoding.

that's all now you can connect.

Happy Coding.



回答4:

Works great!!