0
点赞
收藏
分享

微信扫一扫

【MongoDB】用户与权限管理

浮游图灵 2023-08-31 阅读 17

创建用户
> use tpcc10
switched to db tpcc10
> db.createUser(
... {
... user:"test",
... pwd:"test",
... roles:[{role:"read",db:"tpcc10"}]
... }
... )
Successfully added user: {
        "user" : "test",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "tpcc10"
                }
        ]
}

查询指定用户
> db.getUser("test")
{
        "_id" : "tpcc10.test",
        "userId" : UUID("3cec2b29-c547-4254-b848-143fd0f6ae20"),
        "user" : "test",
        "db" : "tpcc10",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "tpcc10"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}

> db.getUser("test",{showCredentials:true,showPrivileges:true,showAuthenticationRestrictions:true})
{
        "_id" : "tpcc10.test",
        "userId" : UUID("3cec2b29-c547-4254-b848-143fd0f6ae20"),
        "user" : "test",
        "db" : "tpcc10",
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ],
        "credentials" : {
                "SCRAM-SHA-1" : {
                        "iterationCount" : 10000,
                        "salt" : "iqpKRRm1hYNpJ9UTMcuhyg==",
                        "storedKey" : "BVA7XICwos4Mcv7Ge5TvQOdwVkM=",
                        "serverKey" : "QAPw+k7aKZ4iBucWIeeSNY6CLNE="
                },
                "SCRAM-SHA-256" : {
                        "iterationCount" : 15000,
                        "salt" : "N0vduBlKj6H8q7tAhcNFHNeBD47xD9RbmXHR9w==",
                        "storedKey" : "ig3sEk8JLGU40kv6eJD+khgY0bY4JRt2474rEVuGlyI=",
                        "serverKey" : "4pmd8AbCL4wSL9y7MBmTlulLVJtcLKS9GRpm4/zfL0A="
                }
        },
        "roles" : [
                {
                        "role" : "read",
                        "db" : "tpcc10"
                }
        ],
        "inheritedRoles" : [
                {
                        "role" : "read",
                        "db" : "tpcc10"
                }
        ],
        "inheritedPrivileges" : [
                {
                        "resource" : {
                                "db" : "tpcc10",
                                "collection" : ""
                        },
                        "actions" : [
                                "changeStream",
                                "collStats",
                                "dbHash",
                                "dbStats",
                                "find",
                                "killCursors",
                                "listCollections",
                                "listIndexes",
                                "planCacheRead"
                        ]
                },
                {
                        "resource" : {
                                "db" : "tpcc10",
                                "collection" : "system.js"
                        },
                        "actions" : [
                                "changeStream",
                                "collStats",
                                "dbHash",
                                "dbStats",
                                "find",
                                "killCursors",
                                "listCollections",
                                "listIndexes",
                                "planCacheRead"
                        ]
                }
        ],
        "inheritedAuthenticationRestrictions" : [ ],
        "authenticationRestrictions" : [ ]
}

查询全部用户
> db.getUsers()

授予用户权限
> db.grantRolesToUser("test",[{role:"read",db:"soe10"}])
> db.getUser("test")
{
        "_id" : "tpcc10.test",
        "userId" : UUID("3cec2b29-c547-4254-b848-143fd0f6ae20"),
        "user" : "test",
        "db" : "tpcc10",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "tpcc10"
                },
                {
                        "role" : "read",
                        "db" : "soe10"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}

撤销用户权限
> db.revokeRolesFromUser("test",[{role:"read",db:"soe10"}])
> db.getUser("test")
{
        "_id" : "tpcc10.test",
        "userId" : UUID("3cec2b29-c547-4254-b848-143fd0f6ae20"),
        "user" : "test",
        "db" : "tpcc10",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "tpcc10"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}

删除指定用户
> db.dropUser("test")
true

删除全部用户
> db.dropAllUsers()
NumberLong(1)

举报

相关推荐

0 条评论