How to create Replicate Set in MongoDB using three Ubuntu
Virtual Machine
For
creating Replica Set we need 3 MongoDB running instance on different or same
system.In this example we have taken 3 different ubuntu virtual machine.
Now before
we actually see how to make a replica set we need to check the connectivity
between all the machine.
So in our
case we have three ubuntu machine running on virtual box
1.
All three machine must be able to
access the other two machine by SSH. If they cannot they we need to install
SSH. We can do that by:
sudo apt-get install openssh-server
2 Next we need to
check that all three machine runs on different “Adapter” in Vitual Box .We can
check this by opening Virtual Box Window and going to “setting” option for each
machine and then to “Network” option and choosing different Adapter for each
machine.Also select “Bridged Adapter” in “attached to” option in “Adapter” option
for all machine.
3 Now all the three
machine should ping each other.
4. All the three
must have different IPs.
Now we
are ready to create our Replica Set with three member.
First of all execute under line
commands on each of the mongodb instance machine.
sudo nano /etc/mongodb.conf
on executing this a file opens do
the following entries in the file
#enable rest
rest =true
adding these lines would turn on
REST.Now we move at the bottom of the file and uncomment the replset option and
define our replica set name.
replSet = set
Now we save the file.
Now we restart all the three mongodb instance on different
machines.
sudo service mongodb restart
Now on the mongodb instance which
we want to make primary open mongodb shell and execute following command
mongo
>rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"info2" : "no configuration explicitly specified -- making one",
"me" :
"dbserver1:27017",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
Now for adding secondary to the
replicate set execute
>rs.add(“ip_of_secondary:port”)
Eg.
>rs.add(“172.26.32.150:27017”)
{ “ok” : 1 }
Do this for both the secondary
mongodb instance machine.
You are done with creation of
Replica Set with one primary and two secondary.you can check the status by
running command
set:primary>rs.status()
Last and most important thing do
not forget to make the entry of each machine ip and hostname in other machines “host
file” .Host file is at /etc/host.