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",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,"writers" : 0 } },"locks" : { … … "storage" : { "freelist" : { "search" : { "bucketExhausted" : NumberLong(0),"requests" : NumberLong(0),"scanned" : NumberLong(0) } } },"ttl" : { "deletedDocuments" : NumberLong(0),"passes" : NumberLong(24508) } },"ok" : 1 }
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對我們的支援。