Thursday, 11 July 2013

Implementing Sharded Cluster in MongoDB



In previous post we have seen how to form a replication set in MongoDB.

In this post we will see how to form Shard Cluster  in MongoDB.
For a sharded cluster we need to follow following structure :




In the previous post we have created First Replication Set.Create it in the same way.
We need to create a more replication set ( which will have 3 instances of mongodb in cluster).
For now to start three instances of mongodb on same machine we can do following :
First of all make three folders , for our example we create :
 

C:\data\secondset1
C:\data\secondset2
C:\data\secondset3



Now we run the following commands on different cmd  to start second replication set with name 
secondset


mongod --dbpath c:\data\secondset1 --port 10004 --replSet secondset --oplogSize 700 –rest
mongod --dbpath c:\data\secondset2 --port 10005 --replSet secondset --oplogSize 700 –rest
mongod --dbpath c:\data\secondset3 --port 10006 --replSet secondset --oplogSize 700 –rest

Now follow the previous post to start initiate replication set with name secondset by command
rs.initiate() by connecting to mongo at port 10004.


Now we need to create three config server.
For now to make three config server we make three folders :



C:\data\config1
C:\data\config2
C:\data\config3

Now we run commands on different cmds to start all three config servers :

mongod --configsvr --dbpath C:\data\config1 --port 20001 –rest
mongod --configsvr --dbpath C:\data\config2 --port 20002 –rest
mongod --configsvr --dbpath C:\data\config3 --port 20003 –rest

Now we start a mongos with these three config server by command :

mongos --configdb 172.26.32.85:20001,172.26.32.85:20002,172.26.32.85:20003 --port 29017 --chunkSize 1

Here chunkSize specifies the size of the chunk which will be stored in shards.Default size being 64MB.Also we can see we have started this mongos on port 29017

Once the mongos is started we can hit command
mongo localhost:29017/admin

This will start mongos on port 29017 with admin rights.
Now from here we can add shards :

db.runCommand( { addShard : "myset_name/IP_of_machine_ReplicationSet:port, IP_of_machine_ReplicationSet:port, IP_of_machine_ReplicationSet:port " } )

Similarly we add second shard

Now we can execute command to see the status of sharding cluster:
db.printShardingStatus()

This command will show all shards in the cluster & also all databases in the shard & status of these databases weather they are  partitioned or not(i.e. sharded or not).
For now we can see we have no database with partitioned = true.

We can enable sharding for particular database by :

db.runCommand( { enableSharding : "testing" } )   
here testing is name of database

Now run the same command: db.printShardingStatus(),we will see that the database for which we have enabled sharding ,its partitioned will become  true.

Now we need to enable sharding for specific collection  by :

db.runCommand( { shardCollection : "testing.testcollection", key : {"hair":1} })
here testcollection is name of collection ,

key is the important thing here , for enabling sharding in particular collection we need to first create a index for the collection .We must see that we create a efficient index.
Then this index will we used , while enabling sharding in the cluster  (i.e.) in our case we are using “hair” as a index.
It will we this key according to which data will be sharded among the shards.

Once we are done with this we again run the command: db.printShardingStatus(),

Now we can see the status of various chunks which are formed in the different shards.

We can now run command to see more about which shard contains how much data :
use testing
db.testcollection.getShardDistribution()

Now we are done with the Sharded Cluster.

No comments:

Post a Comment