Database Reference
In-Depth Information
Carefully placing the shard storage instances so that they are correctly distributed among the physical servers
enables you to ensure that your system can tolerate the failure of one or more servers in your cluster. This mirrors
the approach used by RAID disk controllers to distribute data across multiple drives in stripes, enabling RAID
configurations to recover from a failed drive.
Setting Up a Sharding Configuration
To use sharding effectively, it's important that you understand how it works. The next example will walk you through
setting up a test configuration on a single machine. You will configure this example like the simple sharding system
shown in Figure 12-2 , with two differences: this example will keep things simple by using only two shards, and these
shards will be single mongod s rather than full replica sets. Finally, you will learn how to create a sharded collection and
a simple PHP test program that demonstrates how to use this collection.
In this test configuration, you will use the services listed in Table 12-1 .
Table 12-1. Server Instances in the Test Configuration
Service
Daemon Port
Dbpath
Shard Controller mongos
27021
N/A
mongod
/db/config/data
Config Server
27022
mongod
/db/shard1/data
Shard0
27023
mongod
/db/shard2/data
Shard1
27024
Let's begin by setting up the configuration server. Do so by opening a new terminal window and typing the
following code:
$ mkdir -p /db/config/data
$ mongod --port 27022 --dbpath /db/config/data --configsvr
Be sure to leave your terminal window open once you have the config server up and running, or feel free to add
the --fork and --logpath options to your commands. Next, you need to set up the shard controller ( mongos ). To do
so, open a new terminal window and type the following:
$ mongos --configdb localhost:27022 --port 27021 --chunkSize 1
This brings up the shard controller, which should announce that it's listening on port 27021. If you look at the
terminal window for the config server, you should see that the shard server has connected to its config server and
registered itself with it.
In this example, you set the chunk size to its smallest possible size, 1MB. This is not a practical value for real-
world systems, because it means that the chunk storage is smaller than the maximum size of a document (16MB).
However, this is just a demonstration, and the small chunk size allows you to create a lot of chunks to exercise the
sharding setup without also having to load a lot of data. By default, chunkSize is set to 64MB unless otherwise
specified.
Finally, you're ready to bring up the two shard servers. To do so, you will need two fresh terminal windows, one
for each server. Type the following into one window to bring up the first server:
$ mkdir -p /db/shard0/data
$ mongod --port 27023 --dbpath /db/shard0/data
 
 
Search WWH ::




Custom Search