mongodb 如何在 2.6 中向 Mongo 添加管理员用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23003391/
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
How do I add an admin user to Mongo in 2.6?
提问by Tony
I upgraded from 2.4 to 2.6 and authentication broke. This tutorialseems pretty straightforward but I keep getting locked out of my own database. My situation is pretty simple, I have a single Mongo server and need one user/pwd combination to connect.
我从 2.4 升级到 2.6 并且身份验证失败了。 本教程看起来很简单,但我一直被锁定在我自己的数据库之外。我的情况很简单,我有一个 Mongo 服务器,需要一个用户/密码组合来连接。
First I connect via the localhost exception as mentioned. Then I create the admin user as suggested:
首先,我通过上面提到的 localhost 异常进行连接。然后我按照建议创建管理员用户:
use admin
db.createUser(
{
user: "myadmin",
pwd: "mysecret",
roles:
[
{
role: "userAdminAnyDatabase",
db: "admin"
}
]
}
)
Now it's time to add new users so to sanity check myself, I logout of the shell. Now when I type "mongo" it fails. That used to work but OK, it's not seeing a username password and I guess the localhost exception isn't there anymore so I follow the instructions outlined here:
现在是添加新用户的时候了,为了自己的完整性检查,我退出了 shell。现在,当我输入“mongo”时,它失败了。这曾经可以工作,但好吧,它没有看到用户名密码,我猜本地主机异常不再存在,所以我按照此处概述的说明进行操作:
mongo --port 27017 -u myadmin -p mysecret --authenticationDatabase admin
And I get:
我得到:
MongoDB shell version: 2.6.0
connecting to: 127.0.0.1:27017/test
Error while trying to show server startup warnings: not authorized on admin to execute command { getLog: "startupWarnings" }
>
Any idea on how to:
关于如何:
Setup Mongo 2.6 so I can easily go in and out of the shell managing the databases (I would think this is the "system user administrator")
Enable a user from a remote client to connect? (Just the mongo side, no help needed with iptables ...)
设置 Mongo 2.6,这样我就可以轻松进出管理数据库的 shell(我认为这是“系统用户管理员”)
允许来自远程客户端的用户进行连接?(只是 mongo 方面,iptables 不需要帮助......)
Thanks!
谢谢!
回答by Tony
Apparently the "system user administrator" isn't enough. Create a root user:
显然,“系统用户管理员”是不够的。创建根用户:
> db.createUser({user:"someadmin",pwd:"secret", roles:[{role:"root",db:"admin"}]})
Then add your database user:
然后添加您的数据库用户:
> use some_db
> db.createUser(
{
user: "mongouser",
pwd: "someothersecret",
roles: ["readWrite"]
}
)
More details on this gist. Comments on gist and better answers on SO welcome - I'm not a sys admin
有关此要点的更多详细信息。欢迎评论要点和更好的答案 - 我不是系统管理员
回答by user3369108
1) The role that you assign the admin user- userAdminAnyDatabase - doesn't have unlimited privileges. It's just a role that is allowed to create and manage users on any database. Apparently, by default it is restricted from executing certain commands that are not directly related to managing database users (such as fetching the startup warnings from the log, querying the server status, etc.).
1) 您分配给管理员用户的角色 - userAdminAnyDatabase - 没有无限权限。它只是一个允许在任何数据库上创建和管理用户的角色。显然,默认情况下,它被限制执行某些与管理数据库用户没有直接关系的命令(例如从日志中获取启动警告,查询服务器状态等)。
You can use the 'root' role instead as Tony suggests. If you are going to use the root account to do setup and management and then just have a few basic read/write privileged accounts talking to the database, this probably makes the most sense.
您可以像 Tony 建议的那样使用“root”角色。如果您打算使用 root 帐户进行设置和管理,然后只有一些基本的读/写特权帐户与数据库通信,这可能是最有意义的。
2) In general, connecting on the client side just requires calling the db.authenticate() function after connecting from your client code. There are different ways to do this depending on the driver/language that you are using for a client. The node.js driver code is pretty typical: http://mongodb.github.io/node-mongodb-native/api-generated/db.html#authenticate
2) 一般来说,在客户端连接只需要在从客户端代码连接后调用 db.authenticate() 函数。根据您为客户端使用的驱动程序/语言,有不同的方法可以做到这一点。node.js 驱动程序代码非常典型:http: //mongodb.github.io/node-mongodb-native/api-generated/db.html#authenticate
回答by Vinay
Even after following @Tony's method I was getting a
即使在遵循@Tony 的方法之后,我也得到了一个
`com.mongodb.CommandFailureException:`
Adding
添加
compile 'org.mongodb:mongo-java-driver:2.13.1'
in Dependency section of BuildConfig.groovy however fixed the issue.
在 BuildConfig.groovy 的 Dependency 部分解决了这个问题。