Database Reference
In-Depth Information
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
If you log in to the new shard storage server you have created on port 27025 and look at
testcollection
, you will
see something interesting:
$mongo localhost:27025
> use testdb
switched to db testdb
> show collections
system.indexes
testcollection
> db.testcollection.count()
4657
> db.testcollection.count()
4758
> db.testcollection.count()
6268
This shows that the number of items in the
testcollection
on your new
shard2
storage server is slowly going up.
What you are seeing is proof that the sharding system is rebalancing the data across the expanded cluster. Over time,
the sharding system will migrate chunks from the
shard0
and
shard1
storage servers to create an even distribution
of data across the three servers that make up the cluster. This process is automatic, and it will happen even if there is
no new data being inserted into the
testcollection
collection. In this case, the
mongos
shard controller is moving
chunks to the new server, and then registering them with the config server.
This is one of the factors to consider when choosing a chunk size. If your
chunkSize
value is very large, you will
get a less even distribution of data across your shards; conversely, the smaller your
chunkSize
value, the more even
the distribution of your data will be.
Removing a Shard from the Cluster
It was great while it lasted, but now assume that
TextAndARandomNumber.com
was a flash in the pan and its sizzle has
fizzled. After a few weeks of frenzied activity, the site's traffic started to fall off, so you had to start to look for ways to
cut your running costs—in other words, that new shard server had to go!
In the next example, you will remove the shard server you added previously. To initiate this process, log in to the
shard controller (
mongos
) and issue the
removeShard
command:
$ mongo localhost:27021
> use admin
switched to db admin
> db.runCommand({removeShard : "localhost:27025"})
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shard0002",
"ok" : 1
}