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:
./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