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?

The Fool
  • 16,715
  • 5
  • 52
  • 86
Sunkat
  • 393
  • 1
  • 3
  • 8

12 Answers12

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
Abdullah Khawer
  • 4,461
  • 4
  • 29
  • 66
Thien Nguyen
  • 928
  • 10
  • 12
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

ali karimi
  • 542
  • 4
  • 13
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
})
Ashwin
  • 7,277
  • 1
  • 48
  • 70
akheron
  • 321
  • 3
  • 5
  • 2
    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. – Andre Aug 21 '20 at 12:58
  • 1
    In my case, ("mongoose": "^6.0.2") "user" need to be changed as "username' – 鄭大大 Aug 29 '21 at 09:21
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.

Leon
  • 5,701
  • 3
  • 38
  • 38
  • `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) – Chaim Eliyah Apr 04 '20 at 04:23
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

})
anniex
  • 326
  • 3
  • 7
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.

Kamagatos
  • 854
  • 9
  • 12
  • 1
    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) – Sunkat May 08 '15 at 08:21
  • 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.. – MartianMartian Jul 16 '15 at 11:19
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

Community
  • 1
  • 1
user3531155
  • 439
  • 4
  • 11
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']} ... )

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",

    }
});
Arun
  • 344
  • 2
  • 11
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.

Sandeep PC
  • 797
  • 8
  • 12
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.

Dhruvin modi
  • 613
  • 1
  • 8
  • 13
-1

The connection string will be like

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

MD SHAYON
  • 7,001
  • 45
  • 38