31

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:12345@ds047742.mongolab.com: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)
mkuniyil
  • 375
  • 1
  • 4
  • 11
  • db username and password is correct only – mkuniyil Jun 18 '15 at 20:28
  • seems like mongolab is not providing the connection . I tried to ping ds047742.mongolab.com, but it says Unable to connect to remote host – mkuniyil Jun 18 '15 at 20:47
  • hi, you can check out our troubleshooting docs here: http://docs.mongolab.com/connecting/#help. feel free to email us if you have any questions or can't get started. – Chris Chang Jun 19 '15 at 20:05
  • thanks @Chris Chang, that really helps – mkuniyil Jun 20 '15 at 18:22
  • Might be related to this: http://stackoverflow.com/questions/30659213/fail-to-connect-mongolab-with-mongodbshell/31079325#31079325 – dug Jun 26 '15 at 17:55

10 Answers10

42

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>

Joseph
  • 5,793
  • 4
  • 34
  • 41
  • 4
    "Make sure you are using the database username and password not the account username and password from Mlab" this saved my life – Doruk Karınca Apr 11 '16 at 09:34
  • 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 >. – Rick Calder Jul 30 '16 at 13:14
  • "Make sure you are using the database username" ahh! thanks man :) – A.R. Nirjhor May 22 '17 at 05:35
  • In addition, your username and password have to be URL-encoded if they contain [special characters](https://www.w3schools.com/tags/ref_urlencode.asp) – kevin.groat Apr 11 '18 at 15:24
  • add db user was what I missed. you rule – Jony-Y Jun 14 '18 at 09:01
25

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:password@ds31341.mongolab.com:32132/mydb', ["mycollection"], {authMechanism: 'ScramSHA1'});
JaKXz
  • 1,665
  • 1
  • 14
  • 34
gtsouk
  • 5,208
  • 1
  • 28
  • 35
  • 2
    Are the parameters different for `mongoose.connect`? – thetallweeks Oct 05 '15 at 16:50
  • @thetallweeks No, if you update the your local mongo installation, the app will connect with the same parameters. – gtsouk Oct 06 '15 at 17:15
  • 1
    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. – MyrionSC2 Oct 06 '15 at 19:50
9

For me the solution was:

$ npm install --save --save-exact mongoose@4.1.9

According to: Heroku app crashes after MongoDB updated to 3.0

Community
  • 1
  • 1
Oded Regev
  • 4,065
  • 2
  • 38
  • 50
6

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

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:kahney@ds747384.mlab.com:13402/ecommerce?authSource=ecommerce&w=1';

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

This saved my life

sannimichaelse
  • 436
  • 5
  • 12
1

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 !

Al-Maamari Tareq
  • 2,172
  • 2
  • 12
  • 18
0

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 mongoose@5.0.10 --save ....But replace the version with the most recent release,

Mushirih
  • 451
  • 5
  • 13
0

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

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);
Jero Dungog
  • 379
  • 1
  • 10
0

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.