Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoError: auth failed mongoose connection string

Tags:

I can connect to the DB through terminal, but getting this error using mongoose and gulp. mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246 MongoError: auth failed

My connection string is:

mongodb://usr:psw@localhost:27017/dbname 

Any idea what it can be?

like image 672
Sunkat Avatar asked May 07 '15 15:05

Sunkat


2 Answers

I installed MEAN from MEAN packaged by Bitnami for windows 7 using the following password: 123456

Syntax for connection string to connect to mongodb with mongoose module

mongoose.connect("mongodb://[usr]:[pwd]@localhost:[port]/[db]",{auth:{authdb:"admin"}}); 

If you don't have {auth:{authdb:"admin"}} in the connection string, you will get the following error: MongoError: Authentication failed.

JS Example: mongo-test/app.js

var mongoose = require('mongoose'); mongoose.connect('mongodb://root:123456@localhost/test',{auth:{authdb:"admin"}}); mongoose.set('debug', true); // turn on debug 
like image 93
Thien Nguyen Avatar answered Sep 20 '22 14:09

Thien Nguyen


just add ?authSource=yourDB&w=1 to end of db url

 mongoose.connect('mongodb://user:password@host/yourDB?authSource=yourDB&w=1') 

this work for me . &w=1 is important

like image 43
ali karimi Avatar answered Sep 22 '22 14:09

ali karimi