25

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?

1

13 Answers 13

34

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
Sign up to request clarification or add additional context in comments.

1 Comment

I added the {auth:{authdb:"admin"}} but still getting "MongoError: auth failed"
28

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

4 Comments

yes, this worked for me. Quite strange that from pymongo, it doesn't need the authSource parameter.
mongoose.connect('mongodb://admin:password@localhost/some_db?authSource=admin&w=1',{})
what is the purpose of w=1?
@RyanGriggs The w option requests acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags.
16

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
})

2 Comments

Had this issue when switching from the mongo node driver to mongoose. Used the same connection string, but I kept on getting the auth error. I added authSource='admin' and problem was solved.
In my case, ("mongoose": "^6.0.2") "user" need to be changed as "username'
9

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.

1 Comment

Error [MongooseError]: Mongoose 5.x no longer supports ``mongoose.connect(host, dbname, port)`` or ``mongoose.createConnection(host, dbname, port)``. See http://mongoosejs.com/docs/connections.html for supported connection syntax (yes I realize 5 years have passed... info for visitors)
8
mongoose.connect("mongodb://[host]/[db]", { auth:{

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

}}).then(function(db){

    // do whatever you want

    mongoose.connection.close() // close db

})

1 Comment

This should be the default way in the documentation. All other ways lead to error and confusion. Congratulations!
5

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.

2 Comments

I have a user and a password, otherwise the data will be available to everybody. I tried also with the root user, but the problem exist. I am using mongoose, just interested if anybody else issued similar problem...and solved)
same issue here. i noticed it only has problem when i make a second or higher number db link, not to the first one i made..
1

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

1 Comment

hey, thanks for the answer. I will try it. But we have switched to sqlite.
0

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']} ... )

Comments

0

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

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

    }
});

Comments

0
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.

Comments

0
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.

Comments

-1

The connection string will be like

mongodb://username:password@localhost:27017/yourdbname?authSource=admin

Comments

-1

I had the same problem, and in my case, the answer was as simple as removing the angle brackets "<"and ">" around . I had been trying: my_login_id:<my_password>, when it should have been my_login_id:my_password.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.