Unable to connect to mongolab, Getting MongoError: auth failed
Asked Answered
N

10

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:[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)
Numerator answered 18/6, 2015 at 20:8 Comment(4)
db username and password is correct onlyNumerator
seems like mongolab is not providing the connection . I tried to ping ds047742.mongolab.com, but it says Unable to connect to remote hostNumerator
hi, you can check out our troubleshooting docs here: docs.mongolab.com/connecting/#help. feel free to email us if you have any questions or can't get started.Phalansterian
Might be related to this: #30659713Irrepressible
M
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>

Metritis answered 26/3, 2016 at 8:9 Comment(5)
"Make sure you are using the database username and password not the account username and password from Mlab" this saved my lifeSnide
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 >.<Supercharge
"Make sure you are using the database username" ahh! thanks man :)Incised
In addition, your username and password have to be URL-encoded if they contain special charactersGarfieldgarfinkel
add db user was what I missed. you ruleFrozen
W
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:[email protected]:32132/mydb', ["mycollection"], {authMechanism: 'ScramSHA1'});
Walkling answered 22/7, 2015 at 11:40 Comment(3)
Are the parameters different for mongoose.connect?Mcneill
@Mcneill No, if you update the your local mongo installation, the app will connect with the same parameters.Walkling
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.Odyssey
P
9

For me the solution was:

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

According to: Heroku app crashes after MongoDB updated to 3.0

Peekaboo answered 21/3, 2016 at 9:57 Comment(0)
T
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"
Territorialize answered 17/2, 2018 at 14:41 Comment(0)
R
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:[email protected]:13402/ecommerce?authSource=ecommerce&w=1';

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

This saved my life

Riedel answered 4/8, 2018 at 15:32 Comment(0)
M
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 !

Manageable answered 19/6, 2015 at 2:45 Comment(0)
M
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 [email protected] --save ....But replace the version with the most recent release,

Mombasa answered 18/3, 2018 at 7:54 Comment(0)
S
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.

Sourdine answered 4/7, 2018 at 8:27 Comment(0)
G
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);
Gospel answered 30/8, 2018 at 8:49 Comment(0)
S
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.

Stidham answered 1/9, 2018 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.