代码改变世界

【MongoDB】日志报错not authorized on admin to execute command

2022-09-08 17:27  abce  阅读(2378)  评论(0编辑  收藏  举报

安装mongodb exporter监控之后,mongodb的日志一直报错:

{"t":{"$date":"2022-09-08T10:36:50.708+08:00"},"s":"I",  "c":"ACCESS",   "id":20736,   "ctx":"conn32068","msg":"Checking authorization failed","attr":{"error":{"code":13,"codeName":"Unauthorized","errmsg":"not authorized on admin to execute command { find: \"system.version\", filter: { _id: \"shardIdentity\" }, limit: 1, singleBatch: true, lsid: { id: UUID(\"c0161773-77bc-7da2-828b-ad730c188a55\") }, $clusterTime: { clusterTime: Timestamp(1662607605, 1), signature: { hash: BinData(0, 379A5DE7CGB83EC220EDBC59070EA7BA5178B97C), keyId: 7138260557136744207 } }, $db: \"admin\", $readPreference: { mode: \"primaryPreferred\" } }"}}}

  

后台登录数据库查看

> db.system.version.find( {"_id" : "shardIdentity" } )
Error: error: {
        "ok" : 0,
        "errmsg" : "not authorized on admin to execute command { find: \"system.version\", filter: { _id: \"shardIdentity\" }, lsid: { id: UUID(\"db3f050d-1c97-712d-8ad3-9c9df27d6c67\") }, $clusterTime: { clusterTime: Timestamp(1662603775, 1), signature: { hash: BinData(0, 1B730731E36DB0D3DA5E27D67C55DDF532D0E73C), keyId: 7138260557136744207 } }, $db: \"admin\" }",
        "code" : 13,
        "codeName" : "Unauthorized",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1662603795, 1),
                "signature" : {
                        "hash" : BinData(0,"t6CLGusPe6gT6S7lpzwIF88hfK7="),
                        "keyId" : NumberLong("7138260557136744207")
                }
        },
        "operationTime" : Timestamp(1662603795, 1)
}
>

  

此错误是因为没有授权给监控用户对system.version表执行命令的权限,解决方法如下:

> db.grantRolesToUser ( "my_exporter", [ { role: "__system", db: "admin" } ] )