use admin
show users
db.system.users.find()
db.system.users.pretty()
db.runCommand({userInfo:"用户名"})
3. 修改用户密码
use amdin
db.changeUserPassword("用户名","新密码")# 修改用户密码及用户信息
db.runCommand({updateUser:"用户名",pwd:"新密码",customData:{age:22}})
3. db.runCommand创建用户与授权
可通过db.runCommand命令行来创建/修改用户与授予相关权限的操作
1. 创建用户
use admin
db.runCommand({"createUser":"yuan","pwd":"yuan","customData":{},"roles":[{"role":"__queryableBackup","db":"admin"},{"role":"__system","db":"admin"},{"role":"backup","db":"admin"},{"role":"clusterAdmin","db":"admin"},{"role":"clusterManager","db":"admin"},{"role":"clusterMonitor","db":"admin"},{"role":"dbAdmin","db":"admin"},{"role":"dbAdminAnyDatabase","db":"admin"},{"role":"dbOwner","db":"admin"},{"role":"enableSharding","db":"admin"},{"role":"hostManager","db":"admin"},{"role":"read","db":"admin"},{"role":"readAnyDatabase","db":"admin"},{"role":"readWrite","db":"admin"},{"role":"readWriteAnyDatabase","db":"admin"},{"role":"restore","db":"admin"},{"role":"root","db":"admin"},{"role":"userAdmin","db":"admin"},{"role":"userAdminAnyDatabase","db":"admin"}]});
2. 更改用户权限
use admin
db.runCommand({"updateUser":"yuan","customData":{},"roles":[{"role":"readWrite","db":"yuan"},{"role":"__queryableBackup","db":"admin"},{"role":"__system","db":"admin"},{"role":"backup","db":"admin"},{"role":"clusterAdmin","db":"admin"},{"role":"clusterManager","db":"admin"},{"role":"clusterMonitor","db":"admin"},{"role":"dbAdmin","db":"admin"},{"role":"dbAdminAnyDatabase","db":"admin"},{"role":"dbOwner","db":"admin"},{"role":"enableSharding","db":"admin"},{"role":"hostManager","db":"admin"},{"role":"read","db":"admin"},{"role":"readAnyDatabase","db":"admin"},{"role":"readWrite","db":"admin"},{"role":"readWriteAnyDatabase","db":"admin"},{"role":"restore","db":"admin"},{"role":"root","db":"admin"},{"role":"userAdmin","db":"admin"},{"role":"userAdminAnyDatabase","db":"admin"}]});