Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

command line authentication of mongo fails

Tags:

mongodb

I am running mongo 2.2.2 on osx.

When I do the following authentication is going fine:

$ mongo
>> use admin
>> db.auth("uname", "password")

log:

Thu Mar  7 13:51:08 [initandlisten] connection accepted from 127.0.0.1:63474 #10 (4 connections now open)
Thu Mar  7 13:51:08 [conn10]  authenticate db: admin { authenticate: 1, nonce: "123", user: "uname", key: "456" }

However when I try to authenticate directly from the commandline:

$ mongo admin -u uname -p password

I get the following error:

Thu Mar  7 14:25:52 [initandlisten] connection accepted from 127.0.0.1:63939 #12 (5 connections now open)
Thu Mar  7 14:25:52 [conn12]  authenticate db: admin { authenticate: 1, nonce: "789", user: "uname", key: "147" }
Thu Mar  7 14:25:52 [conn12] auth: key mismatch uname, ns:admin
Thu Mar  7 14:25:52 [conn12] end connection 127.0.0.1:63939 (4 connections now open)

Does anyone know what causes this?

like image 343
RickyA Avatar asked Mar 07 '13 13:03

RickyA


People also ask

Why mongo command not working?

mongo is not recognized as an internal or external command This error usually occurs because the system can not find the executable that you are asking to launch so to launch the file you have to provide the full path and after that, it will run without any issues.

How use MongoDB command line?

To open up the MongoDB shell, run the mongo command from your server prompt. By default, the mongo command opens a shell connected to a locally-installed MongoDB instance running on port 27017 . Try running the mongo command with no additional parameters: mongo.

How do I start MongoDB from command prompt?

Open up your command prompt and type mongod to start the MongoDB server.


3 Answers

A password containing special characters, especially the dollar sign, has to be put in single quotes to protect them from the command shell:

$ mongo admin -u uname -p 'password'
like image 136
ronasta Avatar answered Oct 20 '22 13:10

ronasta


You have to use the --authenticationDatabase to indicate mongodb where to find the user you have created. For example:

mongo admin -u uname -p 'password' --authenticationDatabase admin
like image 38
user6155548 Avatar answered Oct 20 '22 12:10

user6155548


This is the way to access an authenticate MongoDB database from terminal

mongo -u user_name -p "your_password" host_name/database_name

Ex:

mongo -u hasib -p "123456" localhost/my_db
like image 38
Hasib Kamal Chowdhury Avatar answered Oct 20 '22 11:10

Hasib Kamal Chowdhury