Search code examples
node.jsmongodbmongoose

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?


Solution

  • 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.