Thursday, February 12, 2015

MongoDB Master Slave Replication



                         Master-Slave replication configuration is easy to configure. Before configuring a Master-Slave Replication in MongoDB it is necessary to know exactly what is a master, slave, replica set and their difference. The database that receives all writes in a conventional master-slave replication is called master. A read-only database that replicates operations from a master database in conventional master/slave replication is called slave. A replica set is a group of mongod processes that maintain the same data set. The main difference between the Master-slave replication and the Replica set is that Replica set has the automatic failover options. The Master-slave replication has the manual failover options. Now let's try to configure the below model Master-Slave Replication in MongoDB with a single master and three slaves.
Follow the below steps for Master-Slave Replication in MongoDB:

Step 1: Create the required data directories. To configure a master-slave deployment, start a mongod instance (30001) as master. For MongoDB installation refer here. So start an instance as follows:
./mongod --dbpath /data/example/masterdb/ --master  --port 30001
Step 2: Start another mongod instance(30002) as a slave. So start an instance with the master port as a source  as follows:
./mongod --dbpath /data/example/slavedb1/ --slave --port 30002 --source=localhost:30001
Step 3: Similarly start another slave(30003) with the master host and port as the source.
./mongod --dbpath /data/example/slavedb2/ --slave --port 30003 --source=localhost:30001
Step 4: Similarly start another slave(30003) with the master host and port as the source.
./mongod --dbpath /data/example/slavedb3/ --slave --port 30004 --source=localhost:30001
Step 5: Start the Mongo instance for the master with the required port(30001)
./mongo localhost:30001/local
The master interface looks as below:

Step 6: Start the Mongo instance for the slave with the required port(30002)

./mongo localhost:30002/admin
The slave interface looks as below:

Step 7: Start the Mongo instance for the slave with the required port(30002)

./mongo localhost:30003/admin
The slave interface looks as below:



Similarly, create another slave mongo instance as above of the port 30004.Now the master-slave replication has been done with a single master and three slaves.
To check use whether it is master or not use:
db.isMaster()

If it is master it returns true.


If it is a slave it returns false.

To check whether the slave synced with master or behind use:
rs.printReplicationInfo()

            That's all now you have configured out a master with three slaves in your machine. Hope it helps :)

0 comments:

Post a Comment