好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MongoDB数据库用户角色和权限管理详解

查看数据库

使用终端命令行输入 mongo 登陆 mongodb 之后切换到 admin 库,并认证后可查看所有数据库,操作如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

[root@renwole.com ~] # 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数据库实行注册制,数据库内无内容时,无法查看到新建的数据库,操作如下:

?

1

2

3

4

5

6

7

8

> use renwoledb

> db.createUser(

   {

    user: "renwole" ,

    pwd : "renwolecom" ,

    roles:[{role: "dbOwner" ,db: "renwoledb" }]

   }

)

此时已完成了一库一账号的创建。如果创建用户提示无权限,请先使用超级管理员登录之后切换到对应的数据库再创建即可,如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

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 用户权限

?

1

2

> use admin

> db.createUser({user: "root" , pwd : "renwolecom" ,roles: [ { role: "root" , db: "admin" } ]})

密码修改

修改某个账号的数据库密码需要进入到该数据库,认证后再修改,否则报错,操作如下:

?

1

2

3

4

> use renwoledb

> db.changeUserPassword( "renwole" , "renwolecompwdnew" )

> db.auth( "renwole" , "renwolecompwdnew" )

1

删除用户及数据库

删除用户(必须切换到admin使用最高权限删除某个用户角色)

?

1

2

> db.system. users .remove({user: "renwole" });

WriteResult({ "nRemoved" : 1 })

删除所有用户(必须具备超级管理权限才能删除)

?

1

> db.system. users .remove({})

删除数据库(必须切换到指定的数据库,然后再删除)

?

1

2

3

4

5

> use renwoledb

switched to db renwoledb

> db.dropDatabase()

{ "ok" : 1 }

>

总结

到此这篇关于MongoDB数据库用户角色和 权限管理 的文章就介绍到这了,更多相关MongoDB用户角色和权限管理内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://renwole.com/archives/2340

查看更多关于MongoDB数据库用户角色和权限管理详解的详细内容...

  阅读:29次