Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot authenticate into mongo, "auth fails"

Tags:

mongodb

I've created an admin user for mongo using these directions:

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

From the mongo client it looks like I can authenticate:

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

But I can't connect any other way. For example:

mongo -u admin -p SECRETPASSWORD

gives the error:

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?

like image 429
justkevin Avatar asked Aug 13 '13 18:08

justkevin


People also ask

What does authentication fail mean?

If you receive this error message, that means that the username and/or password that you have entered is incorrect. The error message states “Authentication failed! Try again.” You may have locked your account after too many attempts and your account will need to be reset. Contact the Help Desk if this is the case.

What is the default authentication mechanism in MongoDB?

SCRAM-SHA-256 is the default authentication method for MongoDB starting in MongoDB 4.0. SCRAM-SHA-256 is a salted challenge-response authentication mechanism (SCRAM) that uses your username and password, encrypted with the SHA-256 algorithm, to authenticate your user.


1 Answers

Authentication is managed at a database level. When you try to connect to the system using a database, mongo actually checks for the credentials you provide in the collection <database>.system.users. So, basically when you are trying to connect to "test", it looks for the credentials in test.system.users and returns an error because it cannot find them (as they are stored in admin.system.users). Having the right to read and write from all db doesn't mean you can directly connect to them.

You have to connect to the database holding the credentials first. Try:

mongo admin -u admin -p SECRETPASSWORD 

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

like image 125
gilo Avatar answered Oct 25 '22 03:10

gilo