mongodb创建local库用户_MongoDB我应该将用户放在admin数据库还是本地数据库中

Is there any best practice in MongoDB as to where I should put my database users?

I've just set up a local MongoDB server and I've added users in the admin database and granted them access to "their" (by their I mean the only database the users have access to) database.

Do you think it would be better to just put the users in "their" database and not in the admin database?

解决方案

Regardless of the user's authentication database, Mongo always stores user information in admin.

MongoDB stores all user information, including name, password, and the user's authentication database, in the system.users collection in the admin database.

When you create a user and grant that user access to a single database (aka their authentication database) then that information can only be stored in the admin database.

So, it's not really a question of "best practice"; storing user details in admin is MongoDB's choice, as implemented by their user management commands.

Update in response to this comment:

Ok, so the users are always located in the admin db, but I may also add "duplicates" to the other dbs? Maybe the question should be whether there any advantage in adding users to the other "non admin" dbs?

If you intend to have a single user with access to multiple databases then create a single user with roles in each of those databases rather than creating that user multiple times i.e. once in each of those databases. For example:

use admin;

db.createUser({user:'userName', pwd:'passwordValue', roles:[

{role:'readWrite', db:'DatabaseA'},

{role:'readWrite', db:'DatabaseB'}

]});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值