浅析MongoDB用户管理
1. 创建一个超级用户
use admin db.createUser( { user: "adminUserName", pwd: "userPassword", roles: [ { roles: "userAdminAnyDatabase", db: "admin" } ] } )
超级用户的role有两种,userAdmin或者userAdminAnyDatabase(比前一种多加了对所有数据库的访问)。
db是指定数据库的名字,admin是管理数据库。
2. 用新创建的用户登录
mongo --host xxx -u adminUserName -p userPassword --authenticationDatabase admin
3. 查看当前用户的权限
db.runCommand( { usersInfo:"userName", showPrivileges:true } )
4. 创建一般用户,也是用createUser
use db01 db.createUser( { user:"oneUser", pwd:"12345", roles:[ {role:"read",db:"db01"}, {role:"read",db:"db02"}, {role:"read",db:"db03"} ] } )
5. 创建一个不受访问限制的超级用户
use admin db.createUser( { user:"superuser", pwd:"pwd", roles:["root"] } )
6. 修改密码
use admin db.changeUserPassword("username", "xxx")
7. 查看用户信息
db.runCommand({usersInfo:"userName"})
8. 修改密码和用户信息
db.runCommand( { updateUser:"username", pwd:"xxx", customData:{title:"xxx"} } )
注:
1. 和用户管理相关的操作基本都要在admin数据库下运行,要先use admin;
2. 如果在某个单一的数据库下,那只能对当前数据库的权限进行操作;
3. db.addUser是老版本的操作,现在版本也还能继续使用,创建出来的user是带有root role的超级管理员。
相关文章
MongoDB的备份(mongodump)与恢复(mongorestore)
在使用MongoDB时,数据备份与恢复是非常重要的一环,以防止数据丢失或意外删除,本文就来介绍一下MongoDB的备份(mongodump)与恢复(mongorestore),感兴趣的可以了解一下2023-12-12Mongodb启动报错完美解决方案:about to fork child pr
在使用命令行启动 MongoDB 的时候报错:about to fork child process, waiting until server is ready for connections.forked process: 50411,造成这个报错的原因是 “MongoDB” 服务没有正常的关闭,在终端连接非正常断开后,再次执行 MongoDB 的时候报错2023-04-04MongoDB聚合$listSampledQueries实例操作
使用$listSampledQueries可以返回指定集合或所有集合的采样查询,analyzeShardKey命令使用采样查询来计算分片密钥的读写分布指标,这篇文章主要介绍了MongoDB聚合$listSampledQueries,需要的朋友可以参考下2024-02-02
最新评论