当前位置:网站首页>MongoDB的用户管理总结
MongoDB的用户管理总结
2022-07-07 11:17:00 【cui_yonghua】
基础篇(能解决工作中80%的问题):
进阶篇:
其它:
一. mongodb用户
1.1 用户管理接口
要添加用户,可使用MongoDB提供的db.createUser()方法。 添加用户时,可以为用户分配角色以授予权限。
注意:在数据库中创建的第一个用户应该是具有管理其他用户的权限的用户管理员。
还可以更新现有用户,例如更改密码并授予或撤销角色。
1.2 验证数据库
添加用户时,可以在特定数据库中创建用户。该数据库是用户的认证的数据库。
用户可以跨不同数据库拥有权限; 即用户的权限不限于认证数据库。 通过分配给其他数据库中的用户角色,在一个数据库中创建的用户可以拥有对其他数据库的操作权限。
用户名和认证数据库作为该用户的唯一标识符。 也就是说,如果两个用户具有相同的名称,但是在不同的数据库中创建,则它们是两个不同的用户。 如果您打算拥有具有多个数据库权限的单个用户,请在适用的数据库中创建具有角色的单个用户,而不是在不同数据库中多次创建用户。
1.3 认证用户
要验证用户,也可以在连接到 mongod 或 mongos 实例时使用命令行身份验证选项(例如:-u,-p,–authenticationDatabase)先连接到 mongod 或 mongos 实例,然后针对身份验证数据库运行 authenticate 命令或db.auth()方法。
要进行身份验证,客户端必须对用户的身份验证数据库进行身份验证。
例如,如果使用 mongo shell作为客户端,则可以使用–authenticationDatabase选项为用户指定身份验证数据库。
二、配置账号和密码
2.1 开启认证
MongoDB 默认安装完成以后,只允许本地连接,同时不需要使用任何账号密码就可以直接连接MongoDB,这样就容易被黑,让支付一些比特币,所以为了避免这些不必要的麻烦,所以我们需要给Mongo设置一个账号密码;
2.2 创建管理员用户
> use admin
switched to db admin
> db.createUser({
user:"admin",pwd:"password",roles:["root"]})
Successfully added user: {
"user" : "admin", "roles" : [ "root" ] }
2.3 认证登录
> db.auth("admin", "password")
2.4 MongoDB role 类型
数据库用户角色(Database User Roles)
read:授予User只读数据的权限
readWrite:授予User读写数据的权限
数据库管理角色(Database Administration Roles):
dbAdmin:在当前dB中执行管理操作
dbOwner:在当前DB中执行任意操作
userAdmin:在当前DB中管理User
备份和还原角色(Backup and Restoration Roles):
backup
restore
跨库角色(All-Database Roles):
readAnyDatabase:授予在所有数据库上读取数据的权限
readWriteAnyDatabase:授予在所有数据库上读写数据的权限
userAdminAnyDatabase:授予在所有数据库上管理User的权限
dbAdminAnyDatabase:授予管理所有数据库的权限
集群管理角色(Cluster Administration Roles):
clusterAdmin:授予管理集群的最高权限
clusterManager:授予管理和监控集群的权限,A user with this role can access the config and local databases, which are used in sharding and replication, respectively.
clusterMonitor:授予监控集群的权限,对监控工具具有readonly的权限
hostManager:管理Server
2.5 添加数据库用户
> use flowpp
switched to db flowpp
> db.createUser({
user: "flowpp", pwd: "flopww", roles: [{
role: "dbOwner", db: "flowpp" }]}) # 创建用户flowpp,设置密码flopww,设置角色dbOwner
2.6 查看系统用户
> use admin
switched to db admin
> db.system.users.find() # 显示当前系统用户
{
"_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000, "salt" : "9jXmylyRAK22TZmzv1Thig==", "storedKey" : "z76cVrBjX/CTFmn5RujtU+dz7Nw=", "serverKey" : "JQGonM84iDMI1nIXW7FdyOE55ig=" } }, "roles" : [ {
"role" : "root", "db" : "admin" } ] }
{
"_id" : "flowpp.flowpp", "user" : "flowpp", "db" : "flowpp", "credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000, "salt" : "KvocqWZA9E2tXBHpKpdAeQ==", "storedKey" : "50Kxc3LEgCSVN1z16S8g4A6jVp8=", "serverKey" : "0RSnsxd/7Yzmqro/YOHf/kfbHCk=" } }, "roles" : [ {
"role" : "dbOwner", "db" : "flowpp" } ] }
2.7 删除用户
# 删除用户的时候需要切换到用户管理的数据库才可以删除;
1.切换admin ,删除用户flowpp ,删除失败> use admin
switched to db admin
> db.dropUser("flowpp")
false2.切换flowpp ,删除用户flowpp,删除成功
> use flowpp
switched to db flowpp
> db.dropUser("flowpp")
true
边栏推荐
- What if the xshell evaluation period has expired
- 滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- Image pixel read / write operation
- 初学XML
- 非分区表转换成分区表以及注意事项
- Cinnamon 任务栏网速
- How to continue after handling chain interruption / sub chain error removed from scheduling
- 日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
- 认养一头牛冲刺A股:拟募资18.5亿 徐晓波持股近40%
猜你喜欢

Day22 deadlock, thread communication, singleton mode

Leetcode brush question: binary tree 24 (the nearest common ancestor of binary tree)

Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images

2022 practice questions and mock examination of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge)

TPG x AIDU|AI领军人才招募计划进行中!

MATLAB中polarscatter函数使用

Differences between MySQL storage engine MyISAM and InnoDB

飞桨EasyDL实操范例:工业零件划痕自动识别

Adopt a cow to sprint A shares: it plans to raise 1.85 billion yuan, and Xu Xiaobo holds nearly 40%

ICLR 2022 | pre training language model based on anti self attention mechanism
随机推荐
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
regular expression
Conversion from non partitioned table to partitioned table and precautions
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
Adopt a cow to sprint A shares: it plans to raise 1.85 billion yuan, and Xu Xiaobo holds nearly 40%
处理链中断后如何继续/子链出错removed from scheduling
Layer pop-up layer closing problem
PCAP学习笔记二:pcap4j源码笔记
DrawerLayout禁止侧滑显示
10 张图打开 CPU 缓存一致性的大门
Go language learning notes - structure
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
Grep of three swordsmen in text processing
The difference between cache and buffer
如何让electorn打开的新窗口在window任务栏上面
Awk of three swordsmen in text processing
MySQL master-slave replication
关于 appium 启动 app 后闪退的问题 - (已解决)
自定义线程池拒绝策略
PACP学习笔记一:使用 PCAP 编程