Unable to connect to mongolab, Getting MongoError: auth failed

47.7k views Asked by At

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

There are 10 answers

0
Al-Maamari Tareq On

1- make sure the db is up and running. 2- dont forget to create the db user to have access credentials.

Wish that will help you !

0
sannimichaelse On

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

3
gtsouk On

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'});
0
Jero Dungog On

If your password has special characters it would be best to check the url encoding value of the special character present here: url encoding list

But I highly suggest you verify your data being sent first before attempting to connect. One way to verify it is to console.log the data being sent. Example:

console.log(process.env.MONGO_ATLAS_PW);
0
umang-malhotra On

You can try connecting via mshell, I had this similar problem while connecting using mongoose even when I had given it database user name and it's correct password.

just type following command in the terminal

mongo ds239412.mlab.com:39412/videoplayer11 -u dbuser -p dbpassword (command will be different for you, see here).

and remove the code in your model file where you were connecting via mongoose.

Worked for me. Happy faces.

0
Pooja S Arkasali On

Make sure you are using proper db username and password.

If you are trying to connect to db through your code and your username and password has any speacial characters like '@','$' etc , make sure you encode your URI using encodeURIComponent() funtion

example : "localhost://pooja:"+encodeURIComponent('pooja@123')+"/trymynewdb" , then use the enocded uri to connect to db.

0
Mushirih On

I was getting this error while using an older version of mongoose(version 3.8.10).After upgrading to the latest release(version 5.0.10) the error dissapeared and a connection was made.

Just run npm install [email protected] --save ....But replace the version with the most recent release,

0
Rumesh Madushanka On

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"
0
Oded Regev On

For me the solution was:

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

According to: Heroku app crashes after MongoDB updated to 3.0

5
Joseph On

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>