Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to mongolab, Getting MongoError: auth failed

I have recently created an account in mongoLab.When I am trying to connect to the database using the below statement.

var mongoose = require('mongoose'); mongoose.connect('mongodb://mk:[email protected]:47742/mkdb'); 

I'm always getting the following error

MongoError: auth failed at Function.MongoError.create (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11) at /Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:793:66 at Callbacks.emit (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:94:3) at null.messageHandler (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:235:23) at Socket.<anonymous> (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:259:22) at Socket.emit (events.js:107:17) at readableAddChunk (_stream_readable.js:163:16) at Socket.Readable.push (_stream_readable.js:126:10) at TCP.onread (net.js:538:20) 
like image 368
mkuniyil Avatar asked Jun 18 '15 20:06

mkuniyil


2 Answers

Make sure you are using the database username and password not the account username and password from Mlab.

In MLab, formerly MongoLab, do the following

  1. Navigate to Users
  2. Add Database User
  3. Choose your username and password

Now you can test this on the shell with mongo ds061374.mlab.com:61374/yourdb -u <dbuser> -p <dbpassword>

like image 139
Joseph Avatar answered Sep 30 '22 21:09

Joseph


Mongolab upgraded their 2.6.x databases to 3.0.x. Unfortunately mongo3 has a different authentication mechanism so old clients are not compatible.

Mongoose is using the native mongo driver so you have to upgrade it. This is usually done by upgrading your local mongo installation.

For those using mongojs, upgrade to the latest version and add the authMechanism:'ScramSHA1' parameter in the options object upon connection:

db = mongojs('mongodb://username:[email protected]:32132/mydb', ["mycollection"], {authMechanism: 'ScramSHA1'}); 
like image 39
gtsouk Avatar answered Sep 30 '22 20:09

gtsouk