MongoError: auth failed

MongoError: auth failed

I am trying to connect to mongodb from my web application using Mongooose. However, I get an MongoError: auth failed from mongo when I specify the database I want to connect to. If I do not specify the db, then to connection is successful.

It works when using mongo command line and mongodb management tool like Robomongo. Here is what we normally do.

var dbURI = 'mongodb://user:pwd@localhost:27017/dbname';
mongoose.connect(dbURI);   

And an error will be like -

MongoError: auth failed
at Object.toError...

But I found a solution from MongoDB Node.js Driver. It only needs to add ?authSource=admin in the end of the line.

mongodb://user:pwd@localhost:27017/dbname?authSource=admin
Show Comments
Mastodon