19
votes

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?

11
For those who come looking for a solution, stackoverflow.com/a/49957410/13062813 is your answer - Joshua

11 Answers

31
votes

I installed MEAN at https://bitnami.com/stack/mean for windows 7 When install I make password is 123456

Syntax make connect to mongodb with mongoose

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

If have no

{auth:{authdb:"admin"}}

You will get error message "MongoError: auth failed"

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
23
votes

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

14
votes

There is many ways to make it work. This is what worked for me [mongoose v5.9.15] :

mongoose.connect('mongodb://localhost:27017/', {
    auth: {
        user:'root',
        password:'example'
    },
    authSource:"admin",
    useUnifiedTopology: true,
    useNewUrlParser: true
}
8
votes

You might want to do something like this...

var opt = {
    user: config.username,
    pass: config.password,
    auth: {
        authdb: 'admin'
    }
};
var connection = mongoose.createConnection(config.database.host, 'mydatabase', config.database.port, opt);

'authdb' option is the database you created the user under.

8
votes
mongoose.connect("mongodb://[host]/[db]", { auth:{

    authdb: "admin",
    user: [username],
    password: [pw]

}}).then(function(db){

    // do whatever you want

    mongoose.connection.close() // close db

})
5
votes

Do you have a user set up for dbname? By default, no user is required to connect to the database unless you explicitly set one. If you haven't, you should just try to connect to mongodb://localhost:27017/dbname and see if you still get an error.

1
votes

I have found the solution hier, looks like when you create an user from the mongo shell, it makes SCRAM-SHA-1 instead of MongoDB-CR. So the solution to create a new user with MongoDB-CR authentication.

MongoDB-CR Authentication failed

0
votes

just make sure that your database is created. and also if your user is not added in the admin database, then make sure to add it by putting db.createUser( ... {user:'admin',pwd:'admin',roles:['root']} ... )

0
votes

This worked for me for mongod --version = db version v3.6.13

mongoose.connect('mongodb://localhost/expressapi', {
    auth: {
        authdb: "admin",
        user: "root",
        password: "root",

    }
});
0
votes
mongo mongodb://usr:psw@localhost:27017/dbname
  • Password should be alphanumeric only
  • User should be also available in db 'dbname' (Note : Even if user is super admin)

With above changes it connected successfully.

0
votes
mongoose.connect("mongodb://[usr]:[pwd]@localhost:[port]/[db]",{ authSource: 'admin', useNewUrlParser: true, useUnifiedTopology: true });

I was getting same error. Resolved by adding authSource option to connect function solved the issue. see above code.