MongoDB 提供各种特性,如身份验证、访问控制、加密,来确保MongoDB的部署。
Manger Users and Roles
创建管具有管理用户角色的用户
1 2 3 4 5 6 7 8
| use admin db.createUser( { user: "myUserAdmin", pwd: "abc123", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
|
配置config文件开启鉴权 (mongodb v3.4)
1 2 3 4 5 6 7 8
| vim /etc/mongod.conf
# security: authorization: enabled
# systemctl restart mongod
|
重新链接数据库 mongo -u "admin" -p "pwd" --authenticationDatabase "admin"
用户操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| ## 创建用户
db.createUser( { user: "cfl", pwd: "cfl", roles: [ { role: "readWrite", db: "cms" } ] } )
db.revokeRolesFromUser('cmsAdmin',[{role:'dbAdmin',db:'cms'}]) db.grantRolesToUser('cmsAdmin',[{role:'dbAdmin',db:'cms'}]) db.changeUserPassword('cmsAdmin','cmsAdmin456') db.dropAllUsers() db.dropUser('username') db.getUser('username') db.getUsers()
|
mongodb 常用内键角色
- root 最高角色能够操作所有的库和备份(备份权限要是3.4版本添加的)
- read 读权限
- readWrite 写权限
- dbAdmin 数据库管理员
- dbOwner 具有 read,readWrite,dbAdmin,userAdmin 的所有特权
- userAdmin 用户管理角色
- backup 备份
- restore 还原
- readAnyDatabase
- readWriteAnyDatabase
- userAdminAnyDatabase
- dbAdminAnyDatabase