Database replication is a service for shipping changes to your database, off to a copy housed on another server, potentially even in another data center.It ensures redundancy, backup, and automatic failover.
Replication occurs through groups of servers known as replica sets, each of which stores the same dataset as the other members of the replica set.
Replication in MongoDB
In MongoDB, replication is implemented in two forms:
- Replica Sets: Group of replication nodes containing one primary node and multiple secondary nodes. Primary accepts write operations, and secondaries are the replicating members. MongoDB’s replica sets provide automated failover. If a primary fails, the remaining members will automatically try to elect a new primary.
- Master-Slave Architecture: When the number of the replica nodes need to be more than 12, then Master-Slave architecture comes in. It is similar to Replica Sets except that it does not provide automated failover.
Replication in MongoDB
Deploying a Replica Set
With trivial knowledge gathered about Replica Sets in MongoDB, try deploying one using the steps below:
Step 1: Follow Step 1,2 and 3 for setting up MongoDB on all the nodes of the Replica Set as given in my previous post.
Step 2: Create a conf file (for example "mongod.conf") at a location owned and accessible by the user with the following content. You can configure "port" and name your replica set by setting the field "replSet" to that name which would be same on all the nodes of the replica set.
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log logappend=true nojournal = true port = 27017 bind_ip = machine_ip (for eg. 100.11.11.11) fork = true replSet = rsName |
Step 3: Start the MongoD daemon on each node of the Replica Set using:
/bin/mongod --config /pathToFile/mongod.conf
|
Step 4: Open a mongo terminal on the node which you want to be the primary node using:
bin/mongo --host ip_of_the_machine
|
rs.initiate()
|
Step 5: To add the other nodes, run the add commands in the opened mongo terminal on the primary node:
rs.add("ip_of_secondary_node_1")
rs.add("ip_of_secondary_node_2") and so on. |
rs.conf()
|
Add secondaries to the Replica Set using a JavaScript
After initializing the primary using "rs.initiate()" add the secondaries using :
mongo primary_ip:mongod_port /path_to_js_file
|
where the .js file contains
rs.add('ip_of_sec1:port_of_mongod');
rs.add('ip_of_sec2:port_of_mongod'); . . . rs.add('ip_of_secN:port_of_mongod'); |
To check, you can open the mongo terminal on the primary node and run the "rs.conf()" command to see the status post running the JavaScript file.
Hope that the above steps helped you create a Replica Set successfully. My next post would be on "Creating a MongoDB Sharded Cluster".
I am confused little bit.
ReplyDeletea) Can we have multinode on a single machine? How do we go for it?
b) rs.add("ip_of_secondary_node_1"), Is this ip of a different machine? Can we have it on same machine with different port?
c) Do we need to execute Step 2 on all nodes with same port and rsname? What if we want to start multiple nodes on a single machine?
Unable to Take MongoDB Backup? Contact to MongoDB Technical Support for Backup
ReplyDeleteIn the event that you are not ready to take the whole reinforcement of your MongoDB at that point does not stress simply endeavor to contact MongoDB Online Support or Apache Cassandra Support. Here we are working with experienced experts who guaranteed of auspicious and exact resolutions of your MongoDB. With our adaptable approach you can without much of a stretch get the propel bolster. We additionally offer Support for MongoDB Database Software.
For More Info: https://cognegicsystems.com/
Contact Number: 1-800-450-8670
Email Address- info@cognegicsystems.com
Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801