如果本文对你们的开发之路有所帮助,请帮忙点个赞,您的支持是我坚持写博客的动力
扫描文章底部二维码获取电子书和最新面试资料
本系列课程将带着大家以面试题的方式 深入分布式专题之MongoDB。这篇文章带着大家深入MongoDB安全认证
RBAC(基于角色的访问控制):英文名称Role-Based Access Control
| 角色 | 权限名称 |
|---|---|
| 数据库用户角色 | read、readWrite |
| 数据库管理角色 | dbAdmin、dbOwner、userAdmin |
| 集群管理角色 | clusterAdmin、clusterManager、clusterMonitor、hostManager |
| 备份恢复角色 | backup、restore |
| 所有数据库角色 | readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase |
| 超级用户角色 | root |
| 内部角色 | __system |
| 权限名 | 描述 |
|---|---|
| read | 允许用户读取指定数据库 |
| readWrite | 允许用户读写指定数据库 |
| dbAdmin | 允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile |
| dbOwner | 允许用户在指定数据库中执行任意操作,增、删、改、查等 |
| userAdmin | 允许用户向system.users集合写入,可以在指定数据库里创建、删 除和管理用户 |
| clusterAdmin | 只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限 |
| readAnyDatabase | 只在admin数据库中可用,赋予用户所有数据库的读权限 |
| readWriteAnyDatabase | 只在admin数据库中可用,赋予用户所有数据库的读写权限 |
| userAdminAnyDatabase | 只在admin数据库中可用,赋予用户所有数据库的userAdmin权限 |
| dbAdminAnyDatabase | 只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限 |
| root | 只在admin数据库中可用。超级账号,超级权限 |
访问权限, 客户端连接MongoDB 服务端不需要安全认证,可以随意操作存在很大风险 --auth,或配置文件(mongod.conf)加上auth=truebind_ip=1.1.1.1,2.2.2.2,禁止bind_ip使用0.0.0.0或bind_ip_all设置为true查看角色列表
> show roles
{
"role" : "dbAdmin",
"db" : "test",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "dbOwner",
"db" : "test",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "enableSharding",
"db" : "test",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "read",
"db" : "test",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "readWrite",
"db" : "test",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
{
"role" : "userAdmin",
"db" : "test",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}

创建用户
db.createUser({user:“用户名”,pwd:“名称”,roles:[“角色”]})
| 命令参数 | 含义 |
|---|---|
| user | 用户名称 |
| pwd | 密码 |
| roles | 角色列表 |
创建test库,并指定test库的管理员
> use test
switched to db test
> db.createUser({user:"test",pwd:"test",roles:['dbOwner']})
uncaught exception: ReferenceError: dn is not defined :
@(shell):1:1
> use test
switched to db test
> db.createUser({user:"test",pwd:"test",roles:['dbOwner']})
Successfully added user: { "user" : "test", "roles" : [ "dbOwner" ] }
> show users
{
"_id" : "test.test",
"userId" : UUID("d9be5de9-8c28-4b2b-8d88-530be7846b14"),
"user" : "test",
"db" : "test",
"roles" : [
{
"role" : "dbOwner",
"db" : "test"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
>

创建用户
db.dropUser(“用户名”)
db.dropUser(“test”)
> use test
switched to db test
> dn.createUser({user:"test",pwd:"test",roles:['dbOwner']})
uncaught exception: ReferenceError: dn is not defined :
@(shell):1:1
> use test
switched to db test
> db.createUser({user:"test",pwd:"test",roles:['dbOwner']})
Successfully added user: { "user" : "test", "roles" : [ "dbOwner" ] }
> show users
{
"_id" : "test.test",
"userId" : UUID("d9be5de9-8c28-4b2b-8d88-530be7846b14"),
"user" : "test",
"db" : "test",
"roles" : [
{
"role" : "dbOwner",
"db" : "test"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
>

授权用户权限
db.grantRolesToUser(“用户名”, [{
role: “角色code”,
db: “数据库名称”
},…
])
db.grantRolesToUser(“test”, [{
role: “dbAdmin”,
db: “test”
}
])
> use test
switched to db test
> db.createUser({user:"test",pwd:"test",roles:['dbOwner']})
Successfully added user: { "user" : "test", "roles" : [ "dbOwner" ] }
> db.grantRolesToUser("test", [{
... role: "dbAdmin",
... db: "test"
... }
... ])
>

授权账号登录
mongo MONGO_HOST:PORT -u USER -p PWD --authenticationDatabase=DB
| 命令参数 | 含义 |
|---|---|
| MONGO_HOST | Mongo服务端 ip |
| PORT | Mongo服务端 端口 |
| -u | 用户名 |
| -p | 密码 |
| –authenticationDatabase | 授权数据库 |
mongo 127.0.0.1:27017 -u test -p test --authenticationDatabase=test
[root@localhost mongodb]# mongo 127.0.0.1:27017 -u test -p test --authenticationDatabase=test
MongoDB shell version v4.4.14
connecting to: mongodb://127.0.0.1:27017/test?authSource=test&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("bf4865d7-def1-4824-8240-d8687d820a2e") }
MongoDB server version: 4.4.14
---
The server generated these startup warnings when booting:
2022-06-29T02:07:49.470-07:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2022-06-29T02:07:49.471-07:00: You are running this process as the root user, which is not recommended
2022-06-29T02:07:49.471-07:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2022-06-29T02:07:49.471-07:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
2022-06-29T02:07:49.471-07:00: Soft rlimits too low
2022-06-29T02:07:49.471-07:00: currentValue: 1024
2022-06-29T02:07:49.471-07:00: recommendedMinimum: 64000
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
>

初次见面,也不知道送你们啥。干脆就送几百本电子书和最新面试资料,祝你们找到更好的工作,扫描下面二维码获取