Auth failed, code 18 when connecting to MongoLab database

mongodb remote connection auth failed
openshift mongodb authentication failed
command failed with error 18: 'authentication failed
mongoose server selection error authentication failed
e query (js) error: authentication failed.
mongoose authentication failed
mongoerror: authentication failed
mongodb auth mechanism not specified'', code: 2, codename: badvalue

I'm trying to connect to a MongoLab database but keep getting the following error on connection:

{ [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }

The code I'm using to connect is:

var mongoose = require("mongoose");


mongoose.connection.on('error', function (err) {

When I connect using the shell, I have no problems whatsoever. What am I doing wrong?

I have encountered similar problem when connecting the mongo db using mongoose. After exploring a while I found mongoLab is using SCRAM-SHA-1 authentication. Refer to the question below I tried to upgrade my mongoose to V4.1.11, and then it works for me

Authentication in mongoose using SCRAM-SHA-1

[MongoError: auth failed] when connecting to database, connection error: { [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: '​auth failed', code: 18 }. Here is my connection code: I'm using mongoose 3.8.4, mongo 3.0.6 and node 0.10.33. I didn't see anyone else with  After searching many hours I need to create this issue.Cause I didn't optimal solution.I rent mongodb as a service from one company and they created user which is not using "admin" database for authentication database.For this reason I changed mongoose connection as descripted in this issue.

Had this error myself, turns out I did two things incorrectly (thanks Idos):

  1. Used the username instead of the database one.
  2. Tried to connect to a mongo 3.4 database using a 2.6 shell provided through Ubuntu's repositories. mongo --version to check.

Follow the instructions from this MongoDB page to add their keys and repositories to your APT sources in order to upgrade and keep your MongoDB installation updated going forward.

Auth failed, code 18 when connecting to MongoLab database, I'm trying to connect to a MongoLab database but keep getting the following error on connection: { [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg:  I have an application that connects to a MongoDB database called discussions. I've created a user Mongo shell: > use discussions switched to db discussions > db.auth("discussions","XXXXXXX

I faced the same issue while I try to import data from the locale to server.

Those 2 parameters can be important, it worked after I put them:

--authenticationMechanism 'MONGODB-CR' 
--authenticationDatabase "admin"

Be careful about the auth mechanism, can be a different one. Check this part of documentation:

Mongoerror: authentication failed (code 18) when connecting to , I'm trying to connect to a MongoDB database with a username and password using Mongoose in Node.js. All the docs say that the connection string should look  This straight-forward approach involves the following 3 sequential steps: Start your MongoDB server without authentication. Create the system user administrator. Restart your MongoDB, this time with authentication enabled. You can now connect to your server as that user administrator.

Connect to MongoDB using with different authentication database , ', ok: 0, code: 18, errmsg: 'Authentication failed.' } So I got stuck when I am authenticate with using customAuthDB. How can I fix this ? Optional. The authentication database to use if the connection string includes username:password@ authentication credentials but the authSource option is unspecified. If both authSource and defaultauthdb are unspecified, the client will attempt to authenticate the specified user to the admin database.?<options> Optional.

Troubleshooting connection issues, Troubleshooting guide when you can't connect to your database or It looks like you are trying to access MongoDB over HTTP on the native driver port If host returns with a “not found” error, check with your network administrator Ensure your authentication database is the correct one for the database user you're using. Hey, Since updating to version 1.0.2 (though 1.0.0 most likely introduced my issue) I'm now receiving "Auth Failed" errors when executing a command. This is the full error: { [MongoError: auth failed] name: 'MongoError', message: 'auth f

Connecting to Your Database, Connect to your mLab database using the mongo shell, a standard driver, or mLab's to: Error: error doing query: failed Fri Jan 18  Now, when i want connect to secondary, with mongo shell authenticataion failed with. Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18, codeName: "AuthenticationFailed" } 0 forced to new node become PRIMARY not work.

  • Can you check if by accident you are using your username and password, instead of the database username and password?
  • Also, are you possibly using an old shell version to connect to mongodb newer version?
  • upgrading mongoose version helped me..thanks :) refer this…