最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
MongoDB数据库用户角色和权限管理代码解析
时间:2022-06-29 10:29:00 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下MongoDB数据库用户角色和权限管理代码解析,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
查看数据库
使用终端命令行输入 mongo 登陆 mongodb 之后切换到 admin 库,并认证后可查看所有数据库,操作如下所示:
[[email protected] ~]# mongo MongoDB shell version v4.4.0 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("1ea1-4343-9523-167a101973a9") } MongoDB server version: 4.4.0 > use admin > db.auth("admin","InaM6Aip#2JBlWwY") 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB
说明:1 表示认证成功,0 表示认证失败,认证失败后查看数据库无任何返回。
创建数据库及用户
创建一个 renwoledb 数据库并授权 renwole 用户为该库的 dbOwner 角色。另外、MongoDB数据库实行注册制,数据库内无内容时,无法查看到新建的数据库,操作如下:
> use renwoledb
> db.createUser(
  {
   user:"renwole",
   pwd:"renwolecom",
   roles:[{role:"dbOwner",db:"renwoledb"}]
  }
)
此时已完成了一库一账号的创建。如果创建用户提示无权限,请先使用超级管理员登录之后切换到对应的数据库再创建即可,如下所示:
MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7be9-4c30-ad2e-2a5b58127ab7") }
MongoDB server version: 4.4.0
> use renwoledb
switched to db renwoledb
> db.createUser(
   {
    user:"renwole",
    pwd:"renwolecom",
    roles:[{role:"dbOwner",db:"renwoledb"}]
   }
 )
uncaught exception: Error: couldn't add user: command createUser requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1343:11
@(shell):1:1
> use admin
switched to db admin
> db.auth("root","renwolecompassword")
1
> use renwoledb
switched to db renwoledb
> db.createUser(
   {
    user:"renwole",
    pwd:"renwolecom",
    roles:[{role:"dbOwner",db:"renwoledb"}]
   }
 )
Successfully added user: {
	"user" : "renwole",
	"roles" : [
		{
			"role" : "dbOwner",
			"db" : "renwoledb"
		}
	]
}
添加 root 用户,拥有整个 MongoDB 最高权限,建议取消认证模式后,先进入到 admin 库,再添加 root 用户权限
> use admin
> db.createUser({user: "root",pwd: "renwolecom",roles: [ { role: "root", db: "admin" } ]})
密码修改
修改某个账号的数据库密码需要进入到该数据库,认证后再修改,否则报错,操作如下:
> use renwoledb
> db.changeUserPassword("renwole", "renwolecompwdnew")
> db.auth("renwole","renwolecompwdnew")
1
删除用户及数据库
删除用户(必须切换到admin使用最高权限删除某个用户角色)
> db.system.users.remove({user:"renwole"});
WriteResult({ "nRemoved" : 1 })
删除所有用户(必须具备超级管理权限才能删除)
> db.system.users.remove({})
删除数据库(必须切换到指定的数据库,然后再删除)
> use renwoledb
switched to db renwoledb
> db.dropDatabase()
{ "ok" : 1 }
>                                        				                
                    相关文章
- 暗喻幻想布丽吉塔设施完工时间说明 10-31
- 三国志8重制版居民情感作用介绍说明 10-31
- 三国志8重制版游戏灾害效果介绍说明 10-31
- 三国志8重制版武将不同状态区别说明 10-31
- 三国志8重制版武将阶级提升方法分享 10-31
- 三国志8重制版武将不同阶级作用说明 10-31
 
             
                                 
                                 
                                 
                                 
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                        