问题
Created a new mLab account and created a database as per the steps here http://docs.mlab.com/#create-sub . Trying to connect to the database using mongo shell and mongoose Node.js module but I see the 'Authentication Failed' errorin both cases.
In Mongo shell the command is, I have double checked the credentials
mongo ds012345.mlab.com:56789/dbname -u dbuser -p dbpassword
Error:
MongoError: authentication fail
at Function.MongoError.create (E:\Gatsby\notmongoose\node_modules\mongodb-core\lib\error.js:31:11)
Using Mongoose
var mongoose = require('mongoose')
, Admin = mongoose.mongo.Admin;
var uri = '<correct mongo uri here>';
var connection = mongoose.createConnection(uri,
{
User: '<uname>',
Password: '<pwd>'
});
connection.on('open', function() {
console.log('connection established!!!');
new Admin(connection.db).listDatabases(function(err, result) {
console.log('listDatabases succeeded');
console.log(err);
console.log(result);
});
});
Error:: MongoConnect Error MongoError:authentication fail
Is there anything else that needs to be done on the mLab console or anything else that I might be doing wrong or missing?
回答1:
You needs to create a new user for the database in mLab account.and login with that created user on mlab account.then click on specific database and click on users tab, see in image below:
Now fill the form and create user for database. See in image below :
Now, in the code use the following string for connection:
var mongoose = require('mongoose');
var mongoDB = "mongodb://<username>:<password>@ds241489.mlab.com:41489/<DB Name>";
mongoose.connect(mongoDB, {
useMongoClient: true
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
回答2:
I run into this same issue and in my case it was a problem of client version compatibility, as explained here.
My mLab mongo database was version 3.4.9 but the client command-line version that I had, which came from the official Ubunty Xenial repos, was version 2.6.10.
This errored out with "authentication failed" even though the credentials being used (database credentials, not mlab user credentials) were correct.
回答3:
You are probably using you mLab credentials for dbuser
and dbpassword
.
You should create a new user for the database in using mLab's web interface and try to login with that user.
Here is how you can create a user for a database in mLab:
- Select Databse
- Switch to Users tab
- Click
add database user
回答4:
var mongoose = require('mongoose');
var mongoDB = "mongodb://user:123456@ds114989.mlab.com:13936/brary";
mongoose.connect(mongoDB, {
useMongoClient: true
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
Note: You need to create a user for your database on Mlab. This is not your account login details on Mlab. Here my is replaced with user and dbpassword replaced with 123456 If your password contains a special character like @, # u need to escape it by enclosing 'dbuser':'dbpassword'
回答5:
Connecting from Robomongo / Robo 3T? make sure the auth mechanism is SCRAM-SHA-1
回答6:
are your sure you use your database user name and password? because in most cases people use their account user name.
select your db and click on users then you can find your db user name
来源:https://stackoverflow.com/questions/45627563/mongodb-on-mlab-authentication-fails