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
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']} ... )
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.
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
This worked for me for mongod --version = db version v3.6.13
mongoose.connect('mongodb://localhost/expressapi', {
auth: {
authdb: "admin",
user: "root",
password: "root",
}
});
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
}
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