Problem description
When running the command printShardingSizes, on a sharded cluster with authentication enabled, a "not authorized on <DB> to execute command
{ dataSize: \"sample.samplecol1\"}..." error will appear.
mongos> printShardingSizes() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("5685130fc895ab9401e7cc50") } shards: { "_id" : "shard02", ... ... ... databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "sampledb", "partitioned" : true, "primary" : "shard01" } sampledb.samplecoll chunks: { "a" : { "$minKey" : 1 } } -->> { "a" : 1 } on : shard02 { "errmsg" : "not authorized on sampledb to execute command { dataSize: \"sampledb.samplecoll\", keyPattern: { a: 1.0 }, min: { a: MinKey }, max: { a: 1.0 } }", "code" : 13 }
Hypothesis
The command wraps the dataSize command and executes it directly on the primaries of the shards, from the mongos, by connecting through the shell (without authentication). In the snippet of the code wrapped by the printShardingSizes command, the "mydb" variable attempts to connect directly to the primaries of the shards. As the authentication is enabled on the cluster level, the command itself fails on a "not authorized" error.
var mydb = shards[chunk.shard].getDB(db._id) var out = mydb.runCommand({dataSize: coll._id, keyPattern: coll.key, min: chunk.min, max: chunk.max });
I have tested this scenario several times, on versions 2.6.11, 3.0.6 and 3.0.8. all with the same results.
Workaround
mongos> use sampledb
mongos> db.runCommand({dataSize:"sampledb.samplecoll",keyPattern:{a:1},min:{ a: MinKey },max: { a: MaxKey }})