i've setup sharded mongo db environment on localhost, 3 config servers , 2 sharded mongo instances , single mongos.
once cluster has started up, run following sequence of commands:
sh.addshard( "127.0.0.1:27010") sh.addshard( "127.0.0.1:27011") = {"_id" : 1, "value" : 1} b = {"_id" : 2, "value" : 2} c = {"_id" : 3, "value" : 3} d = {"_id" : 4, "value" : 4} use foobar; db.foo.insert(a); db.foo.insert(b); db.foo.insert(c); db.foo.insert(d);
the enable db sharding , create index etc.
sh.enablesharding("foobar"); db.foo.ensureindex({"value":"hashed"}); sh.shardcollection("foobar.foo", { value: "hashed" } )
the result of above operations successful.
but once do: db.foo.stats()
i see data ends in 1 shard without being distributed. , running
db.printshardingstatus();
produces:
--- sharding status --- sharding version: { "_id" : 1, "version" : 3, "mincompatibleversion" : 3, "currentversion" : 4, "clusterid" : objectid("52170e8a7633066f09e0c9d3") } shards: { "_id" : "shard0000", "host" : "127.0.0.1:27010" } { "_id" : "shard0001", "host" : "127.0.0.1:27011" } databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "foobar", "partitioned" : true, "primary" : "shard0000" } foobar.foo shard key: { "value" : "hashed" } chunks: shard0000 1 { "value" : { "$minkey" : 1 } } -->> { "value" : { "$maxkey" : 1 } } on : shard0000 timestamp(1, 0)
interestingly, though, if start off blank collection , have enable sharding on before adding data it, results different:
db.foo.stats(); { "sharded" : true, "ns" : "foobar.foo", "count" : 4, "numextents" : 2, "size" : 144, "storagesize" : 16384, "totalindexsize" : 32704, "indexsizes" : { "_id_" : 16352, "value_hashed" : 16352 }, "avgobjsize" : 36, "nindexes" : 2, "nchunks" : 4, "shards" : { "shard0000" : { "ns" : "foobar.foo", "count" : 1, "size" : 36, "avgobjsize" : 36, "storagesize" : 8192, "numextents" : 1, "nindexes" : 2, "lastextentsize" : 8192, "paddingfactor" : 1, "systemflags" : 1, "userflags" : 0, "totalindexsize" : 16352, "indexsizes" : { "_id_" : 8176, "value_hashed" : 8176 }, "ok" : 1 }, "shard0001" : { "ns" : "foobar.foo", "count" : 3, "size" : 108, "avgobjsize" : 36, "storagesize" : 8192, "numextents" : 1, "nindexes" : 2, "lastextentsize" : 8192, "paddingfactor" : 1, "systemflags" : 1, "userflags" : 0, "totalindexsize" : 16352, "indexsizes" : { "_id_" : 8176, "value_hashed" : 8176 }, "ok" : 1 } }, "ok" : 1 }
so question whether i'm missing something, if shard existing collection?
currently have such small dataset have 1 chunk of data. mongodb balance data according migration thresholds - impact of balancer kept minimum. try adding more data :) , balancer split data , balance chunks on time.
without data in collection begin each shard allocated range of chunks , why seeing data across shards in second case.