1、启动mongodb
mongod --config mongodb.conf
2、切换数据库
user admin
3、认证
db.auth('root','root')
4、查看用户
show users
5、查看数据库
show dbs;
6、查看集合
show collections
7、用户操作
用户管理
添加用户
通过mongo shell终端操作,用户保存在admin数据库system.user集合中
添加普通用户
切换到需要添加用户的db
use xxxx
执行添加
1
2
3
4
5
6
7
8
9
10
db.createUser(
{
user:"username",
pwd:"password",
roles:[
{role:"read", db:"xxxx"},
{role:"readWrite", db:"test"}
]
}
)
添加超级用户
切换到admin数据库
use admin
执行添加
1
2
3
4
5
6
7
8
9
db.createUser(
{
user:"username",
pwd:"password",
roles:[
{role:"root", db:"admin"}
]
}
)
删除用户
切换到用户授权的db
use xx
执行删除操作
db.dropUser("username")
更新用户
切换到用户授权的db
use xx
执行更新
字段会覆盖原来的内容
1
2
3
4
5
6
db.updateUser("username",{
pwd:"new password",
customData:{
"title":"PHP developer"
}
})
更新用户密码
use xx
db.changeUserPassword("username","newpassword")
查看用户信息
use admin
db.getUser("username")
删除用户角色
use xx
1
2
3
4
5
6
db.revokeRolesFromUser(
"usename",
[
{ role: "readWrite", db: "accounts" }
]
)
添加用户角色
use xx
1
2
3
4
5
6
db.grantRolesToUser(
"reportsUser",
[
{ role: "read", db: "accounts" }
]
)
角色管理
自定义角色
自定义角色保存在admin数据库system.roles集合中
切换到admin数据库
use admin
执行添加
1
2
3
4
5
6
7
8
9
10
db.createRole(
{
role: "manageOpRole",
privileges: [
{ resource: { cluster: true }, actions: [ "killop", "inprog" ] },
{ resource: { db: "", collection: "" }, actions: [ "killCursors" ] }
],
roles: []
}
)
查看角色信息
use admin
db.getRole("rolename",{showPrivileges:true})
删除角色
use admin
db.dropRole("rolename")
系统内置用户角色
大部分内置的角色对所有数据库共用,少部分仅对admin生效
数据库用户类
read
非系统集合有查询权限
readWrite
非系统集合有查询和修改权限
数据库管理类
dbAdmin
数据库管理相关,比如索引管理,schema管理,统计收集等,不包括用户和角色管理
dbOwner
提供数据库管理,读写权限,用户和角色管理相关功能
userAdmin
提供数据库用户和角色管理相关功能
集群管理类
clusterAdmin
提供最大集群管理权限
clusterManager
提供集群管理和监控权限
clusterMonitor
提供对监控工具只读权限
hostManager
提供监控和管理severs权限
备份和恢复类
backup
提供数据库备份权限
restore
提供数据恢复权限
All-Database类
readAnyDatabase
提供读取所有数据库的权限除了local和config数据库之外
readWriteAnyDatabase
和readAnyDatabase一样,除了增加了写权限
userAdminAnyDatabase
管理用户所有数据库权限,单个数据库权限和userAdmin角色一样
dbAdminAnyDatabase
提供所有用户管理权限,除了local,config
超级用户类
root
数据库所有权限
内部角色
__system
提供数据库所有对象任何操作的权限,不能分配给用户,非常危险
参考资料
【1】认证识别
https://docs.mongodb.com/manual/core/authentication/
【2】openssl 证书操作命令
http://blog.youkuaiyun.com/madding/article/details/26717963
【3】维基百科X.509介绍
https://en.wikipedia.org/wiki/X.509
【4】mongodb - security-x.509
https://docs.mongodb.com/manual/core/security-x.509/
【5】Use x.509 Certificates to Authenticate Clients
https://docs.mongodb.com/manual/tutorial/configure-x509-client-authentication/
【6】Enterprise Authentication Mechanisms - ldap and Kerberos
https://docs.mongodb.com/manual/core/authentication-mechanisms-enterprise/
【7】mongodb添加用户
https://docs.mongodb.com/manual/tutorial/create-users/
【8】更改mongodb用户密码和自定义数据
https://docs.mongodb.com/manual/tutorial/change-own-password-and-custom-data/
【9】mongodb数组
http://www.cnblogs.com/ljhdo/p/5428037.html
【10】管理mongodb用户和角色
https://docs.mongodb.com/manual/tutorial/manage-users-and-roles/
【11】security-users
https://docs.mongodb.com/manual/core/security-users/
【12】Internal Authentication
https://docs.mongodb.com/manual/core/security-internal-authentication/
【13】Built-In Roles
https://docs.mongodb.com/manual/core/security-built-in-roles/
【14】reference:built-in-roles and built-in-actions
https://docs.mongodb.com/manual/reference/built-in-roles/
【15】SCRAM-SHA1认证方式介绍
https://docs.mongodb.com/manual/core/security-scram-sha-1/#authentication-scram-sha-1