mongodb Mongo 用户具有读写任何数据库的权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18823112/
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
Mongo user privilege to read and write to any database
提问by JoeyP
Mongo newb here. I have a mongo installation with four databases and would like to create a user that can read and write to all the databases. I tried:
Mongo 新手在这里。我有一个带有四个数据库的 mongo 安装,并想创建一个可以读写所有数据库的用户。我试过:
use admin;
db.addUser({user: "foo" , pwd: "bar", roles: [ "readWriteAnyDatabase" ]})
but when I try to do
但是当我尝试做
mongo someotherdb -u foo -p
and authenticate with the correct password it gives me an authentication error.
并使用正确的密码进行身份验证,它会给我一个身份验证错误。
I also tried manually adding the users to the other databases by doing this
我还尝试通过这样做手动将用户添加到其他数据库
use someotherdb
db.addUser({user: "foo", roles: ['readWrite'], userSource: "admin"});
and still no dice trying to log in via the mongo shell or using the auth command in the target db.
仍然没有骰子尝试通过 mongo shell 登录或在目标数据库中使用 auth 命令。
Am I doing something wrong? How do you go about making a user who can globally read and write to any db? Do you have to add said user to each db's system.users collection or is that not necessary if they have the "readWriteAnyDatabase" role?
难道我做错了什么?你如何让一个可以全局读写任何数据库的用户?您是否必须将所述用户添加到每个数据库的 system.users 集合中,或者如果他们具有“readWriteAnyDatabase”角色则不需要?
I'm using mongo 2.4 on ubuntu.
我在 ubuntu 上使用 mongo 2.4。
回答by JoeyP
So, I've finally figured this out. Note, this fix only works for Mongo 2.4.
所以,我终于想通了这一点。请注意,此修复程序仅适用于 Mongo 2.4。
In 2.4 you can specify a database to authenticate yourself with in the connection string or on the command line
在 2.4 中,您可以在连接字符串或命令行中指定一个数据库来验证自己的身份
So to create the initial user
所以要创建初始用户
use admin
db.createUser({user: "foo" , pwd: "bar", roles: [ "userAdminAnyDatabase","readWriteAnyDatabase" ]})
and to authenticate use
并验证使用
mongo -u foo --authenticationDatabase admin -p
Now you should be able to do whatever you want to any DB
现在你应该可以对任何数据库做任何你想做的事