Over a million developers have joined DZone.

6 Easy Steps to Configure MongoDB Replication Set

DZone's Guide to

6 Easy Steps to Configure MongoDB Replication Set

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

In this tutorial we'll create a 3-node cluster, where the first serves as a primary node, the second as a failover node, and the third as an Arbiter.

1. Setup Mongo and Set a Configuration File

In all the 3 servers adjust the configuration file /etc/mongod.conf:

#Select your replication set name
#Select the replication log size
Disable the bind_ip parameter to avoid binding to only interface

2. Restart All 3 mongod Daemons

 > sudo service mongod restart

3. Create an Initial Configuration on the Primary

Login to the primary mongo and create an initial configuration. Please notice to use the private IP and not the loopback address (

> mongo
Primary> cfg = {"_id" : "[replication_set_name]", "members" : [{"_id" : 0,"host" : "[Primary_Host_IP]:27017"}]}
Primary> rs.initiate(cfg);

4. Add the Failover Instance to the Replication Set

 Primary> rs.add("[Failover_Host_IP]:27017")

5. Add the Arbier Instance to the Replication Set


6. Verify the Replication Set Status


Bottom Line

I wish every data cluster setup was as easy as a setup of a MongoDB replication set.

Keep Performing, [a href="https://www.linkedin.com/in/moshekaplan" target="_blank"]Moshe Kaplan

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.


Published at DZone with permission of Moshe Kaplan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}