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
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
mongo mongodb://usr:psw@localhost:27017/dbname
With above changes it connected successfully.
mongoose.connect("mongodb://[host]/[db]", { auth:{
authdb: "admin",
user: [username],
password: [pw]
}}).then(function(db){
// do whatever you want
mongoose.connection.close() // close db
})
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.
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.