I tried MongoDB horizontal sharding using 3 shards initially and I saw data were divided equally between the shards. I have used "hashed" for the shard key.
Now I have added the 4th shard to this existing setup. But I can see balancer is not moving chunks to the 4th shard so I inserted more than 1millions documents but still data chunks are moving to the 4th shard i.e 4th shard is empty.
Please find the commands that I have executed
Config Servers
mongod --configsvr --port 27019 --replSet rs0 --dbpath data1 --bind_ip localhost
mongod --configsvr --port 27020 --replSet rs0 --dbpath data2 --bind_ip localhost
mongod --configsvr --port 27021 --replSet rs0 --dbpath data3 --bind_ip localhost
mongod --configsvr --replSet rs0 --dbpath data1 --bind_ip localhost
rs.initiate(
{
_id: "rs0",
configsvr: true,
members: [
{ _id : 0, host : "localhost:27019" },
{ _id : 1, host : "localhost:27020" },
{ _id : 2, host : "localhost:27021" }
]
}
)
Three shards
mongod --shardsvr --port 37019 --replSet rp1 --dbpath data1 --bind_ip localhost
mongod --shardsvr --port 37020 --replSet rp1 --dbpath data2 --bind_ip localhost
mongod --shardsvr --port 37021 --replSet rp1 --dbpath data3 --bind_ip localhost
rs.initiate( { _id : "rp1", members: [ { _id: 0, host: "localhost:37019" }, { _id: 1, host: "localhost:37020" }, { _id: 2, host: "localhost:37021" } ] })
mongod --shardsvr --port 47019 --replSet rp2 --dbpath data1 --bind_ip localhost
mongod --shardsvr --port 47020 --replSet rp2 --dbpath data2 --bind_ip localhost
mongod --shardsvr --port 47021 --replSet rp2 --dbpath data3 --bind_ip localhost
rs.initiate( { _id : "rp2", members: [ { _id: 0, host: "localhost:47019" }, { _id: 1, host: "localhost:47020" }, { _id: 2, host: "localhost:47021" } ] })
mongod --shardsvr --port 57019 --replSet rp3 --dbpath data1 --bind_ip localhost
mongod --shardsvr --port 57020 --replSet rp3 --dbpath data2 --bind_ip localhost
mongod --shardsvr --port 57021 --replSet rp3 --dbpath data3 --bind_ip localhost
rs.initiate( { _id : "rp3", members: [ { _id: 0, host: "localhost:57019" }, { _id: 1, host: "localhost:57020" }, { _id: 2, host: "localhost:57021" } ] })
Enabling sharding
mongos --port 40000 --configdb rs0/localhost:27019,localhost:27020,localhost:27021
mongo --port 40000
sh.addShard( "rp1/localhost:37019,localhost:37020,localhost:37021");
sh.addShard( "rp2/localhost:47019,localhost:47020,localhost:47021");
sh.addShard( "rp3/localhost:57019,localhost:57020,localhost:57021");
sh.enableSharding("demodb");
sh.shardCollection("demodb.collection", { timeStamp : "hashed" } )
Now I ran the script to insert data into demodb.collection and I can able to see the data spread across above three shards.
So I added 4th shard using below command
mongod --shardsvr --port 57119 --replSet rp4 --dbpath data1 --bind_ip localhost
mongod --shardsvr --port 57120 --replSet rp4 --dbpath data2 --bind_ip localhost
mongod --shardsvr --port 57121 --replSet rp4 --dbpath data3 --bind_ip localhost
rs.initiate( { _id : "rp4", members: [ { _id: 0, host: "localhost:57119" }, { _id: 1, host: "localhost:57120" }, { _id: 2, host: "localhost:57121" } ] })
mongo --port 40000
sh.addShard( "rp4/localhost:57119,localhost:57120,localhost:57121");
Now I added few more data into demodb.collection thinking that balancer will move the chunks to 4th shard to balance data among all 4 shards. But it didn't worked.
Below the output of Shard status
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("6052f2e74f97982edd469896")
}
shards:
{ "_id" : "rp1", "host" : "rp1/localhost:37019,localhost:37020,localhost:37021", "state" : 1 }
{ "_id" : "rp2", "host" : "rp2/localhost:47019,localhost:47020,localhost:47021", "state" : 1 }
{ "_id" : "rp3", "host" : "rp3/localhost:57019,localhost:57020,localhost:57021", "state" : 1 }
{ "_id" : "rp4", "host" : "rp4/localhost:57119,localhost:57120,localhost:57121", "state" : 1 }
active mongoses:
"4.2.7" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
rp1 256
rp2 256
rp3 256
rp4 256
too many chunks to print, use verbose if you want to force print
{ "_id" : "demodb", "primary" : "rp2", "partitioned" : true, "version" : { "uuid" : UUID("0034d13f-d46f-4248-9bec-021b8949336b"), "lastMod" : 1 } }
demodb.collection
shard key: { "timeStamp" : "hashed" }
unique: false
balancing: true
chunks:
rp1 2
rp2 2
rp3 2
{ "timeStamp" : { "$minKey" : 1 } } -->> { "timeStamp" : NumberLong("-6148914691236517204") } on : rp1 Timestamp(1, 0)
{ "timeStamp" : NumberLong("-6148914691236517204") } -->> { "timeStamp" : NumberLong("-3074457345618258602") } on : rp1 Timestamp(1, 1)
{ "timeStamp" : NumberLong("-3074457345618258602") } -->> { "timeStamp" : NumberLong(0) } on : rp2 Timestamp(1, 2)
{ "timeStamp" : NumberLong(0) } -->> { "timeStamp" : NumberLong("3074457345618258602") } on : rp2 Timestamp(1, 3)
{ "timeStamp" : NumberLong("3074457345618258602") } -->> { "timeStamp" : NumberLong("6148914691236517204") } on : rp3 Timestamp(1, 4)
{ "timeStamp" : NumberLong("6148914691236517204") } -->> { "timeStamp" : { "$maxKey" : 1 } } on : rp3 Timestamp(1, 5)
Please let me know what I am missing here.