Cannot authenticate into mongo, "auth fails"
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?
Solution 1:
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/
Solution 2:
I also received this error, what I needed was to specify the database where the user authentication data was stored:
mongo -u admin -p SECRETPASSWORD --authenticationDatabase admin
Update Nov 18 2017:
mongo admin -u admin -p
is a better solution. Mongo will prompt you for your password, this way you won't put your cleartext password into the shell history which is just terrible security practice.
Solution 3:
You may need to upgrade your mongo shell. I had version 2.4.9 of the mongo shell locally, and I got this error trying to connect to a mongo 3 database. Upgrading the shell version to 3 solved the problem.