Database Reference
In-Depth Information
And type the following into the second window to bring up the second server:
$ mkdir -p /db/shard1/data
$ mongod --port 27024 --dbpath /db/shard1/data
You have your servers up and running. Next, you need to tell the sharding system where the shard servers are
located. To do this, you need to connect to your shard controller ( mongos ) using your server's hostname. You could
use localhost, but that limits the scalability of your cluster to this machine alone. You should replace the <hostname>
tags with your own hostname when running the examples that follow. It's important to remember that, even though
mongos is not a full MongoDB instance, it appears to be a full instance to your application. Therefore, you can just use
the mongo command shell to attach to the shard controller and add your two shards, as shown here:
$ mongo <hostname> :27021
> sh.addShard(" <hostname> :27023")
{ "shardAdded" : "shard0000", "ok" : 1 }
> sh.addShard( " <hostname> :27024")
{ "shardAdded" : "shard0001", "ok" : 1 }
Your two shard servers are now activated; next, you need to check the shards using the listshards command:
> 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" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0000" }
You now have a working sharded environment, but no sharded data; next, you will create a new database called
testdb , and then activate a collection called testcollection inside this database. You will shard this collection, so
you will give this collection an entry called testkey that you will use as the sharding function:
> sh.enableSharding("testdb")
{ "ok" : 1 }
> sh.shardCollection("testdb.testcollection", {testkey : 1})
{ "collectionsharded" : "testdb.testcollection", "ok" : 1 }
Thus far, you have created a sharded cluster with two shard storage servers. You have also created a database on it
with a sharded collection. A server without any data in it is of no use to anybody, so it's time to get some data into this
collection, so you can see how the shards are distributed.
To do this, you will use a small PHP program to load the sharded collection with some data. The data you will
load consists of a single field called testkey . This field contains a random number and a second field with a fixed
chunk of text inside it (the purpose of this second field is to make sure you can create a reasonable number of chunks
 
Search WWH ::




Custom Search