Sharding-Replica-set with MongoDB
Setting Replica set on three machines
Suppose three machines with host name and ip as follows
192.168.1.25: mongodbTest25
192.168.1.26: mongodbTest26
192.168.1.27: mongodbTest27
Steps 1 : Install MongoDB in all server in /usr/local/ folder as 'mongodb'
Step 2 : On each of the host execute the the following command as
mongodbTest25$ mkdir -p /data/db
mongodbTest26$ ?mkdir -p /data/db
mongodbTest27$ ?mkdir -p /data/db
Step 3 : Go to the /usr/local/mongodb/bin/ directory in all machines and execute the following
command on each machine as
mongodbTest25$ cd /usr/local/mongodb/bin/
$./mongod --port 10000 --rest --replSet testset
Similarly on 26 and 27 server.
Step 4 : we can execute the following command on any of the machines.
$./mongo
>? config = {_id: '?testset', members: [ {_id: 0, host: '192.168.1.25:10000'},
{_id: 1, host: '?192.168.1.26:10000'},{_id: 2, host: '?192.168.1.27:10000'}] };
>rs.initiate(config);
>rs.status();
Step 5 : after executing the above steps and you can view the status as
http://192.168.1.25:11000/
Sharding and Replica set in a cluster
we will set up a nearly production ready cluster of MongoDB instances with the following setup:
3 replica sets (each replica set consisting of 3 mongod instances)
3 config servers
1 mongos server
Step 1 : Install the mongodb in all instances in /usr/local/mongodb
Creating replica set 1:
Step 2 : Open one terminal on each machines
execute on server 1 ie 192.168.1.25
mongodbTest25$ cd /usr/local/mongodb/bin
$ mkdir -p /data/db/
$mkdir /data/db/test1
$./mongod --shardsvr --dbpath /data/db/test1 --port 10000 --rest --replSet testset > /tmp/shard1.log &
execute on server 2 ie 192.168.1.26
mongodbTest26$ cd /usr/local/mongodb/bin
$ mkdir -p /data/db/
$mkdir /data/db/test1
$./mongod --shardsvr --dbpath /data/db/test1 --port 10000 --rest --replSet testset > /tmp/shard1.log &
execute on server 3 ie 192.168.1.27
mongodbTest27$ cd /usr/local/mongodb/bin
$ mkdir -p /data/db/
$mkdir /data/db/test1
$./mongod --shardsvr --dbpath /data/db/test1 --port 10000 --rest --replSet testset > /tmp/shard1.log &
on server 1
$./mongo localhost:10000/admin
>config = {_id: 'testset', members: [{_id: 0, host: '192.168.1.25:10000'},{_id: 1, host: '192.168.1.26:10000'},
{_id: 2, host: '192.168.1.159:10000'}] };
>rs.initiate(config)
>rs.status();
Step 3 : Now we need to start our config server,which will store our meta data (there can be a maximum of 3
config servers).
Again on each of the virtual machines execute the following commands:
$ mkdir /data/db/config
$./mongod --configsvr --dbpath /data/db/config --port 20000 > /tmp/config.log &
Step 4 : Starting a mongos instance (dispatcher): Now we need a mongos instance that will act as a
routing service and load balancer. In production environment it is recommended to have a mongos instance
on every application
server. This way the app server can communicate to it locally and there will be no requests to mongos
servers that are not there.
Open up a new terminal and connect to server1 ie 192.168.1.25
$./mongos --configdb 192.168.1.25:20000,192.168.1.26:20000,192.168.1.27:20000 --port 23000 > /tmp/mongos.log &
Step 5 : Now everything is ready to create the Shard1 :In the terminal where you started the mongos instance enter
the following commands:
$./mongo localhost:23000/admin
>db.runCommand({addshard : "testset/192.168.1.25:10000,192.168.1.26:10000,192.168.1.27:10000"});
Step 6 : Activate sharding for the (data)collection and manage it by the num key (sharding will be done
according to the ‘num’ key, you need to find a balanced key in order to have good sharding):
Enable sharding on the db then on the collection
>use admin
>db.runCommand( { enablesharding : "test" } );
>db.runCommand( { shardcollection : "test.data" , key : { num : 1 } } );
Step 7 : now look at the shard list as
>db.runCommand( {listshards : 1} );
Similarly we will be creates Replica set 2
Step 1 :
On each of the machines execute the following commands:
$ mkdir /data/db/test2
$./mongod --shardsvr --dbpath /data/db/test2 --port 10001 --rest --replSet testset2 > /tmp/shard2.log &
Step 2 : Enter the MongoDB shell on server1:
$./mongo localhost:10001/admin
>config = {_id: 'testset2', members: [{_id: 0, host: '192.168.1.25:10001'},{_id: 1, host: '192.168.1.26:10001'},
{_id: 2, host: '192.168.1.27:10001'}]};
>rs.initiate(config);
>rs.status();
Step 3 : Creating shard 2:
//Connect to mongo shell on the mongos instance on server1:
$./mongo localhost:23000/admin
//In the mongo shell add the replica set 'testset2' to a new shard:
>db.runCommand({addshard : "testset2/192.168.1.25:10001,192.168.1.26:10001,192.168.1.27:10001"});
>db.runCommand( {listshards : 1} );
Similarly we will be creates Replica set 3:
Step 1 :
On each of the machines execute the following commands:
$ mkdir /data/db/test3
$./mongod --shardsvr --dbpath /data/db/test3 --port 10002 --rest --replSet testset3 > /tmp/shard3.log &
Step 2 :
Enter the MongoDB shell on server1:
$./mongo localhost:10002/admin
>config = {_id: 'testset3', members: [{_id: 0, host: '192.168.1.25:10002'},{_id: 1, host: '192.168.1.26:10002'},
{_id: 2, host: '192.168.1.27:10002'}]};
>rs.initiate(config);
>rs.status();
Step 3 : Creating shard 3:
Connect to mongo shell on the mongos instance on server1:
$./mongo localhost:23000/admin
>db.runCommand({addshard : "testset3/192.168.1.25:10002,192.168.1.26:10002,192.168.1.27:10002"});
>db.runCommand( {listshards : 1} );
Step 4 : Congratulations, you have setup a production ready MongoDB cluster.