问题
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?
回答1:
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'
回答2:
You have to user the --authenticationDatabase
to indicate mongodb where to find the user you have created. For example:
mongo admin -u uname -p 'password' --authenticationDatabase admin
回答3:
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
回答4:
To Login to MongoDB Via Command Line do the following
$ mongo admin -u 'username' -p 'Password'
来源:https://stackoverflow.com/questions/15272521/command-line-authentication-of-mongo-fails