Using MongoDB | Replication | Basic Configuration For Replica Set With Ubuntu
Posted By : Amit Kumar | 24-Feb-2014
Setup Replication in MongoDB in Ubuntu
Hello guys, this tutorial will help you to setup replication in MongoDB in Open Source Environment(Ubuntu). Before we proceed further i assumed that you have some prior knowledge of MongoDB basics queries. Now question is that what is Replication in MongoDB? Replication means maintaining multiple copy of data so that its availability increases.
How MongoDB Handles Replication Process ?
MongoDB provides replication through replica set concept. It generally need minimum three node to maintain this process. In replica set each node is mongod process.Only a single node can become primary node , which has read/write policy for database and the others are secondary node which maintain copy of original data.
When primary mongod server is down the other (one of the secondary node) server become primary , this is generally based on internal voting of nodes. In MongoDB replication process is asynchronous.
Initials :
1.) We need minimum three ubuntu server(mongo-1,mongo-2,mongo-3), on each one mongod instance should be running
2.) Make sure you can access each mongo server from other mongo servers
Configuration on each mongod server
Step 1:
edit the /etc/mongodb.conf and add below lines :
Code Snippet :-
port = 27017 fork = true replSet = name_of_repl_set
Where :-
port -> specifies, on which port the mongod instance will run
fork -> it means create child process
replSet -> and it is name of replica .[Note :It should be same in all nodes of one replica set and i use rs1 as replica set name]
Step 2:
Re-Start all mongod instance on each server using this command on terminal.
Code Snippet :-
sudo mongod --config /etc/mongodb.conf
Step 3:
Now issue the mongo command on that server which will be Primary Node
Code Snippet :-
mongo ip-address-of-mongo-1:27017 rs.initiate();
The above command will make the current mongo server as primary node. But i have faced some ip-address issue when i was doing this. You can now issue this command to see the status .
Code Snippet :-
rs.conf();
The above will output the primary server information. After few seconds you can see the prompt changed to something like this eg: rs1:PRIMARY> .You can see the host name field, it is the name of the current machine and we have to change it first into ip address of the current machine because in my case it was not resolving host name through DNS. For resolving this, execute the following queries in mongo console.
cfg=rs.conf(); cfg.members[0].host = "ip-address-of-mongo-1-machine" rs.reconfig(cfg)
Step 4:
Add the other server using Primary Node.
Code Snippet :-
rs.add("ip-address-of-mongo-2-machine:27017") rs.add("ip-address-of-mongo-3-machine:27017") rs.status();
Above queries will add both the mongod server as Secondary node in replica set. And you see that replica set information using rs.status()
Now If you create,add or delete any database,collection in primary node it will reflect in all secondary nodes also.
You can also access primary node's database in secondary node by issuing below command and you can easily access the collection(only read possible)
rs.slaveOk();
The above command will tell the replica set that current node is secondary node and then after that you can see what primary node has replicated into secondary.
For more information you can see this MongoDB for replication http://docs.mongodb.org/manual/replication/
Thanks
Amit
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Amit Kumar
Amit is a bright Groovy and Grails developer and has worked on development of various SaaS applications using Grails framework.