问题
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)
回答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
- Navigate to
Users
Add Database User
- Choose your username and password
Now you can test this on the shell with
mongo ds061374.mlab.com:61374/yourdb -u <dbuser> -p <dbpassword>
回答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:password@ds31341.mongolab.com:32132/mydb', ["mycollection"], {authMechanism: 'ScramSHA1'});
回答3:
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
回答4:
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"
回答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:kahney@ds747384.mlab.com:13402/ecommerce?authSource=ecommerce&w=1';
https://github.com/Automattic/mongoose/issues/4587
This saved my life
回答6:
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 !
回答7:
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,
回答8:
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.
回答9:
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);
回答10:
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.
来源:https://stackoverflow.com/questions/30924859/unable-to-connect-to-mongolab-getting-mongoerror-auth-failed