MongoDB shell 命令行身份验证失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23721866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB shell command line authentication fails
提问by Michal Artazov
I set up replica set with authentication. I used thistutorial. 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.
使用相同的凭据。我在文档或 SO 上找不到任何有用的东西。
回答by James Wahlin
The in-shell authentication performed in your example is against the admindatabase. 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:
在您的示例中执行的壳内身份验证针对admin数据库。上面发布的命令行没有指定数据库,因此是针对默认数据库test 进行身份验证的。试试这个,通过命令行对 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'
回答by SauerTrout
Perhaps different now because of an update since this question was originally answered, but authentication only succeeds when wrapping username/password in double quotes. e.g.,
由于最初回答此问题后的更新,现在可能有所不同,但只有在将用户名/密码用双引号括起来时,身份验证才会成功。例如,
mongo admin -u "myName" -p "myPassword"