mongodb用户管理2

.继 上篇 – mongodb 3 用户账户管理1

  1. 用户密码修改:db.changeUserPassword()

用法:

db.changeUserPassword("", "")

例如:
修改前

> db.auth("test1-1","test")
1

密码更新:

> db.changeUserPassword("test1-1", "admin")

验证失败,说明更新成功

> db.auth("test1-1","test")
Error: Authentication failed.
0

新密码验证成功

> db.auth("test1-1","admin")db.auth("test1-1","admin")
1

6.删除用户db.removeUser()/db.dropUser()

2.6版本之后用 db.dropUser(),但db.removeUser()功能保留

用法:

db.removeUser(<username>)  

db.dropUser("reportUser1", {w: "majority", wtimeout: 5000})

例如:

> db.removeUser("test")
WARNING: db.removeUser has been deprecated, please use db.dropUser instead
true

> db.dropUser("test")db.dropUser("test")
true
> 

removeuser功能保留,但会抛出一个告警说明

7.删除用户组db.dropAllUsers
如:db.dropAllUsers( {w: “majority”, wtimeout: 5000} )

8.添加用户角色db.grantRolesToUser
用法

db.grantRolesToUser( "<username>", [ <roles> ], { <writeConcern> } )

可以直接指定权限
如:

"readWrite"

也可以指定特定db的权限

{ role: "", db: "" }

例如:
修改前:

{
        "_id" : "mogotest.test",
        "user" : "test",
        "db" : "mogotest",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "mogotest"
                }
        ]
}

新增readWrite权限,新增的默认是当前库的权限

db.grantRolesToUser(
   "test",
   [ "readWrite"],
   { w: "majority" , wtimeout: 4000 }
)

修改后

{
        "_id" : "mogotest.test",
        "user" : "test",
        "db" : "mogotest",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "mogotest"
                },
                {
                        "role" : "read",
                        "db" : "mogotest"
                }
        ]
}

新增加库:

db.grantRolesToUser(
   "test",
   [ { role: "read", db: "mogotest2" } ],
   { w: "majority" , wtimeout: 4000 }
)

修改后:

{
        "_id" : "mogotest.test",
        "user" : "test",
        "db" : "mogotest",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "mogotest2"
                },
                {
                        "role" : "readWrite",
                        "db" : "mogotest"
                },
                {
                        "role" : "read",
                        "db" : "mogotest"
                }
        ]
}

9.删除用户的角色

db.revokeRolesFromUser()

可以从当前数据库中删除一个用户的一个或者多个角色权限

用法:

db.revokeRolesFromUser( "<username>", [ <roles> ], { <writeConcern> } )

可以直接指定

"readWrite"

也可以使用:

{ role: "", db: "" }

例如:
修改前:

{
        "_id" : "mogotest.test",
        "user" : "test",
        "db" : "mogotest",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "mogotest2"
                },
                {
                        "role" : "readWrite",
                        "db" : "mogotest"
                },
                {
                        "role" : "read",
                        "db" : "mogotest"
                }
        ]
}

删除readwrite权限

db.revokeRolesFromUser( "test",
                        ["readWrite" ],
                        { w: "majority" }
                      )

删除后结果

{
        "_id" : "mogotest.test",
        "user" : "test",
        "db" : "mogotest",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "mogotest2"
                },
                {
                        "role" : "read",
                        "db" : "mogotest"
                }
        ]
}

删除多个权限

db.revokeRolesFromUser( "test",
                        [ 
                          { role: "read", db: "mogotest" },
                          { role: "read", db: "mogotest2" }
                        ],
                        { w: "majority" }
                      )  

删除后:

{
        "_id" : "mogotest.test",
        "user" : "test",
        "db" : "mogotest",
        "roles" : [ ]
}

10.获取用户的信息db.getUser(“”)

db.getUser(username, args)

args 可以是:
1.showPrivileges boolean类型,ture显示用户权限,默认是false
2.showCredentials boolean类型,ture显示用户密码的hash,默认是false

用法:
· 直接获取用户的信息
db.getUser(“Admin01”)

> db.getUser("Admin01")db.getUser("Admin01")
{
        "_id" : "mogotest.Admin01",
        "user" : "Admin01",
        "db" : "mogotest",
        "customData" : {
                "employeeId" : 12345
        },
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "mogotest2"
                },
                {
                        "role" : "readWrite",
                        "db" : "mogotest"
                }
        ]
}  

· 显示用户信息,并显示详细的用户权限
db.getUser(“Admin01”,{“showPrivileges”: true})

 db.getUser("Admin01",{"showPrivileges": true})   
{
        "_id" : "mogotest.Admin01",
        "user" : "Admin01",
        "db" : "mogotest",
        "customData" : {
                "employeeId" : 12345
        },
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "mogotest2"
                },
                {
                        "role" : "readWrite",
                        "db" : "mogotest"
                }
        ],
        "inheritedRoles" : [
                {
                        "role" : "readWrite",
                        "db" : "mogotest"
                },
                {
                        "role" : "readWrite",
                        "db" : "mogotest2"
                }
        ],
        "inheritedPrivileges" : [
                {
                        "resource" : {
                                "db" : "mogotest2",
                                "collection" : ""
                        },
                        "actions" : [
                                "collStats",
                                "convertToCapped",
                                "createCollection",
                                "createIndex",
                                "dbHash",
                                "dbStats",
                                "dropCollection",
                                "dropIndex",
                                "emptycapped",
                                "find",
                                "insert",
                                "killCursors",
                                "listCollections",
                                "listIndexes",
                                "planCacheRead",
                                "remove",
                                "renameCollectionSameDB",
                                "update"
                        ]
                },
       ................
}

db.getUser(“Admin01”,{“showCredentials”: true})

> db.getUser("Admin01",{"showCredentials": true})
{
        "_id" : "mogotest.Admin01",
        "user" : "Admin01",
        "db" : "mogotest",
        "credentials" : {
                "SCRAM-SHA-1" : {
                        "iterationCount" : 10000,
                        "salt" : "FipGHpmf/N6C+SgSuWY+cA==",
                        "storedKey" : "2ubvXDom5lefeE8M8uRz2gmmveA=",
                        "serverKey" : "LYUFsP5cvVQXIVTD45uxvQiA7As="
                }
        },
        "customData" : {
                "employeeId" : 12345
        },
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "mogotest2"
                },
                {
                        "role" : "readWrite",
                        "db" : "mogotest"
                }
        ]
} 

11.获取用户组信息db.getUsers()

你可能感兴趣的:(mongodb)