设置日志级别而不是root的权限级别

时间:2017-01-19 00:09:56

标签: mongodb

我的用户角色为userAdminAnyDatabase和readWriteAnyDatabase。但这似乎不足以为我的数据库设置日志级别。那么我需要设置什么权限而不必让我的用户root?

这是我得到的错误:

[thread1] Error: setLogLevel failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { setParameter: 1.0, logComponentVerbosity: { verbosity: 3.0 } }",
    "code" : 13
}

1 个答案:

答案 0 :(得分:0)

TL; DR 在使用管理命令之前,您需要选择admin数据库:

use admin

对于群集资源,您还需要hostManager角色或权限setParameter

详细了解administrative commandadmin databasesetParameter

setLogLevel使用管理命令setParameter

function (logLevel, component) {
    componentNames = [];
    if (typeof component === "string") {
        componentNames = component.split(".");
    } else if (component !== undefined) {
        throw Error("setLogLevel component must be a string:" + tojson(component));
    }
    var vDoc = {verbosity: logLevel};

    // nest vDoc
    for (var key, obj; componentNames.length > 0;) {
        obj = {};
        key = componentNames.pop();
        obj[key] = vDoc;
        vDoc = obj;
    }
    var res = this.adminCommand({setParameter: 1, logComponentVerbosity: vDoc});
    if (!res.ok)
        throw _getErrorWithCode(res, "setLogLevel failed:" + tojson(res));
    return res;
}