如何在mongo中向用户添加shardCollection操作角色?

时间:2017-01-30 10:10:09

标签: mongodb

我正在尝试允许 readWriteAnyDatabase 访问mongo的用户能够使用shardCollection。

{
    "role" : "readWriteShard",
    "roles" : [
        "readWriteAnyDatabase",
        "clusterManager" // tried with and without this role

    ],
    "privileges" : [
        {
            "resource" : {
                "cluster" : true
            },
            "actions" : [
                "enableSharding"
            ]
        }
    ]
}

我创建了这样的用户之后:

{
"user" : "usershard",
"roles" : [
    //this is useless here, at it's inherited by readWriteShard
    {
        "role" : "readWriteAnyDatabase",
        "db" : "admin"
    },
    {
        "role" : "readWriteShard",
        "db" : "admin"
    }
]

}

一切都好。但我仍然得到这个错误:

  

未授权管理员执行命令

1 个答案:

答案 0 :(得分:0)

您似乎没有管理数据库的权限来运行该命令。您需要在admin数据库上拥有createUser和grantRole权限。