Database Reference
In-Depth Information
And this code enables you to connect to the second shard and see how many records are stored in it from the
testcollection collection:
$mongo localhost:27024
>use testdb
>db.testcollection.count()
51125
You may see different values for the number of documents in each shard, depending on when exactly you
look at the individual shards. the mongos instance may initially place all the chunks on one shard, but over time it will
rebalance the shard set to evenly distribute data among all the shards by moving chunks around. thus, the number of
records stored in a given shard may change from moment to moment. this satisfies “requirement 1: the ability to distrib-
ute data evenly across all shards.”
Note
Adding a New Shard to the Cluster
Let's assume business is really jumping at TextAndARandomNumber.com . To keep up with the demand, you decide to
add a new shard server to the cluster to spread out the load a little more.
Adding a new shard is easy; all it requires is that you repeat the steps described previously. Begin by creating the
new shard storage server and place it on port 27025, so it does not clash with your existing servers:
$ sudo mkdir -p /db/shard2/data
$ sudo mongod --port 27025 --dbpath /db/shard2/data
Next, you need to add the new shard server to the cluster. You do this by logging into the sharding controller
( mongos ), and then using the admin command addshard :
$mongo localhost:27021
>sh.addShard("localhost:27025")
{ "shardAdded" : "shard0002", "ok" : 1 }
At this point, you can run the listshards command to verify that the shard has been added to the cluster. Doing
so reveals that a new shard server ( shard2 ) is now present in the shards array:
> db.printShardingStatus();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("5240282df4ee9323185c70b2")
}
shards:
{ "_id" : "shard0000", "host" : "<hostname>:27023" }
{ "_id" : "shard0001", "host" : "<hostname>:27024" }
{ "_id" : "shard0002", "host" : "<hostname>:27025" }
 
 
Search WWH ::




Custom Search