解决db.serverStatus()命名执行时报无权限的问题

转载:解决db.serverStatus()命名执行时报无权限的问题
原文地址:解决db.serverStatus()命名执行时报无权限的问题

1、问题描述

今天在执行db.serverStatus()命令时给出了“ "errmsg" : "not authorized on admin to execute command { serverStatus: 1.0 }",”的错误提示。
通过查询admin的权限已经是dbOwner的权限了,然后又陆续赋予了dbadmin等权限,问题仍旧存在。
最后找到了Mongodb的权限列表,赋予了root权限终于可以把问题给解决了,然后又测试了其他几个权限都不可以,这说明db.serverStatus是服务器级别的命令,需要mongodb的最高权限才能执行。
下面是问题处理的简单流程,分享给大家,以供参考。
2、错误内容

[root@ggnode2 ~]# mongo 10.130.170.112:27017/admin -u admin -p
MongoDB shell version: 3.2.8
Enter password:
connecting to: 10.130.170.112:27017/admin
MongoDB Enterprise >
MongoDB Enterprise > db.serverStatus()
{
        "ok" : 0,
        "errmsg" : "not authorized on admin to execute command { serverStatus: 1.0 }",
        "code" : 13
}

3、错误分析

从得到的错误内容可知,是由于admin没有执行db.serverStatus()命令的权限。

MongoDB Enterprise > use admin
switched to db admin
MongoDB Enterprise > show collections
system.users
system.version
MongoDB Enterprise > db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "BEN7SONYkewFMx3f67FNQw==", "storedKey" : "HjlvcjSpXpSKetcUbJyj350Xgjk=", "serverKey" : "k2WEf2cHGgg9n3tyEMJyuKaRt3U=" } }, "roles" : [{ "role" : "dbOwner", "db" : "admin" } ] }

根据查询结果可知,admin拥有dbOwner角色权限,而执行db.serverStatus()命令需要root角色权限。
4、问题处理:

经过查询表system.users表可知,需要为admin用户赋予root角色才能执行db.serverStatus()命令。

赋予角色权限的操作命令如下:

授予角色权限

MongoDB Enterprise > db.grantRolesToUser( "admin" , [ { role: "root", db: "admin" } ])
MongoDB Enterprise >

取消角色权限

MongoDB Enterprise > db.revokeRolesFromUser( "admin" , [ { role: "root", db: "admin" } ]
MongoDB Enterprise >

5、问题解决

赋予root角色权限之后,再执行db.serverStatus()命令就OK了,执行结果如下:
MongoDB Enterprise > db.serverStatus()
MongoDB shell version: 3.2.8
connecting to: 10.130.170.112:27017/admin
{

"host" : "ggnode2",
    "advisoryHostFQDNs" : [ ],
    "version" : "3.2.8",
    "process" : "mongod",
    "pid" : NumberLong(23155),
    "uptime" : 1470624,
    "uptimeMillis" : NumberLong(1470624043),
    "uptimeEstimate" : 1415522,
    "localTime" : ISODate("2016-11-07T04:13:33.328Z"),
    "asserts" : {
            "regular" : 0,
            "warning" : 0,
            "msg" : 0,
            "user" : 62,
            "rollovers" : 0
    },
    "connections" : {
            "current" : 1,
            "available" : 818,
            "totalCreated" : NumberLong(6025)
    },
    "extra_info" : {
            "note" : "fields vary by platform",
            "heap_usage_bytes" : 60437840,
            "page_faults" : 28
    },
    "globalLock" : {
            "totalTime" : NumberLong("1470624234000"),
            "currentQueue" : {
                    "total" : 0,
                    "readers" : 0,
                    "writers" : 0
            },
            "activeClients" : {
                    "total" : 8,
                    "readers" : 0,
                    "writers" : 0
            }
    },
    "locks" : {

… …

"storage" : {
                    "freelist" : {
                            "search" : {
                                    "bucketExhausted" : NumberLong(0),
                                    "requests" : NumberLong(0),
                                    "scanned" : NumberLong(0)
                            }
                    }
            },
            "ttl" : {
                    "deletedDocuments" : NumberLong(0),
                    "passes" : NumberLong(24508)
            }
    },
    "ok" : 1

}

相关推荐