MongoDB on mLab authentication fails

Viewed 17790

Created a new mLab account and created a database as per the steps here http://docs.mlab.com/#create-sub . Trying to connect to the database using mongo shell and mongoose Node.js module but I see the 'Authentication Failed' errorin both cases.

In Mongo shell the command is, I have double checked the credentials

mongo ds012345.mlab.com:56789/dbname -u dbuser -p dbpassword

Error:

MongoError: authentication fail
at Function.MongoError.create (E:\Gatsby\notmongoose\node_modules\mongodb-core\lib\error.js:31:11)

Using Mongoose

var mongoose = require('mongoose')
, Admin = mongoose.mongo.Admin;

var uri = '<correct mongo uri here>';

var connection = mongoose.createConnection(uri,
{    
User: '<uname>',
Password: '<pwd>'
});
connection.on('open', function() {

console.log('connection established!!!');

new Admin(connection.db).listDatabases(function(err, result) {
    console.log('listDatabases succeeded');
    console.log(err);
    console.log(result); 
});

});

Error:: MongoConnect Error MongoError:authentication fail

Is there anything else that needs to be done on the mLab console or anything else that I might be doing wrong or missing?

6 Answers
 var mongoose = require('mongoose');

 var mongoDB = "mongodb://user:123456@ds114989.mlab.com:13936/brary";

 mongoose.connect(mongoDB, {
    useMongoClient: true
 });

 var db = mongoose.connection;

 db.on('error', console.error.bind(console, 'MongoDB connection error:'));

Note: You need to create a user for your database on Mlab. This is not your account login details on Mlab. Here my is replaced with user and dbpassword replaced with 123456 If your password contains a special character like @, # u need to escape it by enclosing 'dbuser':'dbpassword'

Connecting from Robomongo / Robo 3T? make sure the auth mechanism is SCRAM-SHA-1

Auth Mechanism

are your sure you use your database user name and password? because in most cases people use their account user name.

select your db and click on users then you can find your db user name

Related