Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB shell command line authentication fails

Tags:

I set up replica set with authentication. I used this tutorial. I set up keyfile, admin user and other users. It all works fine - anonymous access is disabled and I can login

$ mongo MongoDB shell version: 2.6.1 connecting to: test Error while trying to show server startup warnings: not authorized on admin to execute     command { getLog: "startupWarnings" } rs0:PRIMARY> use admin switched to db admin rs0:PRIMARY> db.auth("USERNAME","PASSWORD") 1 rs0:PRIMARY>  

using credentials of users I created.

But I can't login from command line

$ mongo -u 'USERNAME' -p 'PASSWORD' MongoDB shell version: 2.6.1 connecting to: test 2014-05-18T14:23:47.324+0200 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at   src/mongo/shell/db.js:1210 exception: login failed 

using the same credentials. I haven't find anything helpful in the documentation or here on SO.

like image 293
Michal Artazov Avatar asked May 18 '14 12:05

Michal Artazov


People also ask

How do I authenticate in MongoDB shell?

To authenticate as a user, you must provide a username, password, and the authentication database associated with that user. To authenticate using the mongo shell, either: Connect first to the MongoDB or mongos instance. Run the authenticate command or the db.

Why is my MongoDB not connecting?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.


1 Answers

The in-shell authentication performed in your example is against the admin database. The command line posted above does not specify a database and is therefore authenticating against the default database which is test. Try this instead to authenticate via command line against the admin db:

mongo admin -u 'USERNAME' -p 'PASSWORD' 

if the server is not on the local host then you can use this:

mongo your_host_name:your_port/admin -u 'USERNAME' -p 'PASSWORD' 
like image 172
James Wahlin Avatar answered Oct 20 '22 14:10

James Wahlin