Unable to connect to mongolab, Getting MongoError: auth failed

45,965

Solution 1

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>

Solution 2

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

Solution 3

For me the solution was:

$ npm install --save --save-exact [email protected]

According to: Heroku app crashes after MongoDB updated to 3.0

Solution 4

Database User create In here we have to know that mLab username and Password are not the username and password for Our database too...there for we have to check whether we have used correct username and password for connection String

we can create Database user account in here---->>

My connection const as follows

const db ="mongodb://<My database username>:<my database password>.mlab.com:39648/videoplayer"

Solution 5

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

e.g

MONGO_URI='mongodb://kahn:[email protected]:13402/ecommerce?authSource=ecommerce&w=1';

https://github.com/Automattic/mongoose/issues/4587

This saved my life

Share:
45,965
mkuniyil
Author by

mkuniyil

Updated on September 02, 2020

Comments

  • mkuniyil
    mkuniyil almost 4 years

    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)
    
  • thetallweeks
    thetallweeks over 8 years
    Are the parameters different for mongoose.connect?
  • gtsouk
    gtsouk over 8 years
    @thetallweeks No, if you update the your local mongo installation, the app will connect with the same parameters.
  • MyrionSC2
    MyrionSC2 over 8 years
    It may also be your node version that is too old. I started my application via the heroku node tutorial, so the version of node I used on the server were 0 point something. It didn't create problems untill the mongolab update. My app works fine now since i've updated node locally and on the server via package.json.
  • Doruk Karınca
    Doruk Karınca about 8 years
    "Make sure you are using the database username and password not the account username and password from Mlab" this saved my life
  • Rick Calder
    Rick Calder almost 8 years
    Frustrating they don't really make this clear. Thanks, saved me much more wasted time fiddling with this. I was convinced it had something to do with me loading the url from a .env file >.<
  • A.R. Nirjhor
    A.R. Nirjhor about 7 years
    "Make sure you are using the database username" ahh! thanks man :)
  • kevin.groat
    kevin.groat about 6 years
    In addition, your username and password have to be URL-encoded if they contain special characters
  • Jony-Y
    Jony-Y about 6 years
    add db user was what I missed. you rule