mhery
mhery

Reputation: 2248

sh.isBalancerRunning(): false

I am trying to shard a mongodb database like this:

1- Start each member of the shard replica set

mongod --shardsvr --port 27100 --replSet r1 --dbpath <some_path>\shardsvr\shardsvr1

mongod --shardsvr --port 27200 --replSet r2 --dbpath <some_path>\shardsvr\shardsvr2

2- Start each member of the config server replica set

mongod --configsvr --port 27020 --replSet cfg1 --dbpath <some_path>\configsvr\configsvr1

3- Connect to config server replica set

mongo --port 27020

4- Initiate the replica set

conf = {
    _id: "cfg1",
    members: [ 
        {
            _id:0, 
            host: "localhost:27020"
        }
    ]
}

rs.initiate(conf)

5- Start the mongos and specify the --configdb parameter

mongos --configdb cfg1/localhost:27020 --port 28000 

6- Initiate the replica set of each shard

mongo --port 27100
var config = {_id: "r1", members: [{_id:0, host:"localhost:27100"}]}    
rs.initiate(config)
exit    
mongo --port 27200
var config = {_id: "r2", members: [{_id:0, host:"localhost:27200"}]}
rs.initiate(config)
exit

7- Connect to mongos to add shards

mongo --port 28000

sh.addShard("r1/localhost:27100")
sh.addShard("r2/localhost:27200")

8- Add some data

use sharddb

for (i = 10000; i < 30000; i++){
    db.example.insert({
        author: "author" + i,
        post_title: "Blog Post by Author " + i,
        date: new Date()
    });
}

db.example.count()

9- Enable sharding

sh.enableSharding("sharddb")

10- Create the index as part of sh.shardCollection()

db.example.ensureIndex({author : 1}, true)
sh.shardCollection("sharddb.example", {author: 1})

11- Check if balancer is running

sh.isBalancerRunning()

However, in this step, I get a false as response, and I dont know what I did wrong to get this. I followed the steps of this tutorial

Upvotes: 0

Views: 696

Answers (1)

Joe
Joe

Reputation: 28316

With only 20000 documents that are ~100 bytes each, there is probably only 1 chunk.

Check with

use sharddb
db.printShardingStatus()

I repeated the steps you listed above, and got the following result:

{  "_id" : "sharddb",  "primary" : "shard02",  "partitioned" : true }
    sharddb.example
        shard key: { "author" : 1 }
        unique: false
        balancing: true
        chunks:
            shard02 1
        { "author" : { "$minKey" : 1 } } -->> { "author" : { "$maxKey" : 1 } } on : shard02 Timestamp(1, 0)

The mongos will monitor what it has added to each chunk, and notify the config server to consider splitting when it has seen enough data added. Then the balancer will automatically be activated when one shard contains several more chunks than another.

If you insert enough documents to trigger automatic splitting, or manually split the chunk, the balancer will begin doing its thing.

Upvotes: 2

Related Questions