Cannot authenticate to mango, "auth failed"

I created an admin user for Mongo using the following instructions:

http://docs.mongodb.org/manual/tutorial/add-user-administrator/

From the Mongo client, it seems I can authenticate:

> use admin switched to db admin > db.auth('admin','SECRETPASSWORD'); 1 > 

But I can not connect in another way. For example:

mongo -u admin -p SECRETPASSWORD

gives an error message:

 JavaScript execution failed: Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.js:L228 

I have auth = true in etc/mongod.conf .

What am I missing?

+79
mongodb
Aug 13 '13 at 18:41
source share
12 answers

Authentication is managed at the database level. When you try to connect to the system using the database, mongo actually validates the credentials that you provide in the <database>.system.users . So, basically, when you try to connect to "test", it searches for credentials in test.system.users and returns an error because it cannot find them (since they are stored in admin.system.users ). Having the right to read and write from all databases does not mean that you can directly connect to them.

First you need to connect to the database containing the credentials. Try:

 mongo admin -u admin -p SECRETPASSWORD 

For more information, check this out http://docs.mongodb.org/manual/reference/privilege-documents/

+87
Dec 28 '13 at 0:05
source share

I also got this error, I needed to specify the database in which the user authentication data was stored:

mongo -u admin -p SECRETPASSWORD --authenticationDatabase admin

November 18, 2017 Patch:

 mongo admin -u admin -p 

- The best decision. Mongo will ask you for a password, so you won’t enter the plaintext password into the shell history, which is just a terrible security practice.

+66
Apr 21 '14 at 18:33
source share

You may need to update the mongo shell. I had version 2.4.9 of the mongo shell locally, and I got this error trying to connect to the mongo 3 database. Updating the shell version to 3 solved the problem.

+36
Jun 12 '15 at 15:56
source share

I know this may seem obvious, but I also had to use a single quote around u / n and p / w before it worked

administrator mongo -u "user" -p "password"

+29
May 6 '14 at 19:24
source share

In MongoDB 3.0, it now supports several authentication mechanisms.

  • MongoDB Task and Answer (SCRAM-SHA-1) - Default in Version 3.0
  • MongoDB Challenge and Response (MONGODB-CR) - previous default (<3.0)

If you started with the new 3.0 database with new users, they would be created using SCRAM-SHA-1.

To do this, you need a driver capable of such authentication:

http://docs.mongodb.org/manual/release-notes/3.0-scram/#considerations-scram-sha-1-drivers

If you had an updated 2.x database with existing user data, they would still use MONGODB-CR, and the user authentication database should be updated:

http://docs.mongodb.org/manual/release-notes/3.0-scram/#upgrade-mongodb-cr-to-scram

Now connecting to MongoDB 3.0 with users created using SCRAM-SHA-1, you need to specify the authentication database (via the mongo command line client) and use other mechanisms when using the driver.

$> mongo -u USER -p PASSWORD --authenticationDatabase admin

In this case, the admin database will be used for authentication, which is also the default.

+17
Oct 07 '15 at 16:54
source share

It seems that the problem is that the user created using the method described in the mongo docs does not have permission to connect to the default database (test), even if this user was created with the roles "userAdminAnyDatabase" and "dbAdminAnyDatabase" .

+13
Aug 14 '13 at 14:28
source share

This fixed my problem:

Go to the terminal shell and type mongo .

Then enter use db_name .

Then enter:

  db.createUser( { user: "mongodb", pwd: "dogmeatsubparflavour1337", roles: [ { role: "dbOwner", db: "db_name" } ] } ) 

Also try: db.getUsers()

Quick example:

 const MongoClient = require('mongodb').MongoClient; // MongoDB Connection Info const url = 'mongodb://mongodb:dogmeatsubparflavour1337@stackoverflow.com:27017/?authMechanism=DEFAULT&authSource=db_name'; // Additional options: https://docs.mongodb.com/manual/reference/connection-string/#connection-string-options // Use Connect Method to connect to the Server MongoClient.connect(url) .then((db) => { console.log(db); console.log('Casually connected correctly to server.'); // Be careful with db.close() when working asynchronously db.close(); }) .catch((error) => { console.log(error); }); 
+12
Jun 28 '17 at 10:54 on
source share

This is kind of a specific case, but if anyone gets here with my problem:

In MongoHQ, it will show you a field called "password", but in fact it is just a password hash. You will need to add a new user and save the password elsewhere (because MongoHQ will not show it to you).

+2
Sep 09 '14 at 1:59
source share

The correct way to enter the mongo shell is

mongo localhost: 27017 -u 'uuuuu' -p '> xxxxxx' --authenticationDatabase dbname

+2
Jun 06 '17 at 12:24
source share

Another possibility: when you created the user, you could accidentally use a database other than admin , or the one you wanted. You need to install --authenticationDatabase for the database in which the user was actually created.

mongodb apparently puts you in the default test database when you open the shell, so you will need to write --authenticationDatabase test --authenticationDatabase admin if you accidentally use test with db.createUser(...)

Assuming that you have access to the computer where the mongodb instance is running, y can disable authorization in /etc/mongod.conf (comment out authorization which is protected), and then restart the server and run:

 mongo show users 

And you can get something like this:

 { "_id" : "test.myusername", "user" : "myusername", "db" : "test", "roles" : [ { "role" : "dbOwner", "db" : "mydatabasename" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } 

Note that the db value is test . This is because when I created the user, I did not start use admin or use desiredDatabaseName . Thus, you can delete the user using db.dropUser("myusername") and then create another user in the desired database, for example:

 use desiredDatabaseName db.createUser(...) 

Hope this helps someone who was in my position as a newbie in this business.

+2
Aug 31 '18 at 10:47
source share

You can also try the following: -

 mongo localhost:27017/admin -u admin -p SECRETPASSWORD 

Found it in this post

Here it is obvious that localhost may be a different host, and / admin may be a different database on which authentication has been applied

+1
Nov 01 '17 at 15:15
source share

Check the mongo client version with which we connect to the mongo server.

In my case, the mongo server was version Mongo4.0.0, but my client was version 2.4.9. Upgrade the Mongo version to upgrade the Mongo Cli.

0
Jun 25 '19 at 11:47
source share



All Articles