How to Set Up MongoDB Replica Sets

MongoDB replica sets provide redundancy and automatic failover — keeping multiple synchronized copies of your data across servers, with automatic promotion of a new primary if the current one fails.

What a Replica Set Provides

A group of MongoDB servers maintaining identical data sets — one primary handles writes, secondaries replicate from it and can serve reads; if the primary becomes unavailable, an automatic election promotes a secondary to primary.

Prerequisites

  • At least 3 servers running MongoDB (odd numbers are recommended for proper election quorum)
  • MongoDB already installed on each (see How to Install MongoDB on Ubuntu & Debian)
  • Network connectivity between all servers

Step 1 — Configure Each Server for Replica Set Mode

sudo nano /etc/mongod.conf
replication:
  replSetName: "rs0"

net:
  bindIp: 0.0.0.0

Step 2 — Restart MongoDB on Each Server

sudo systemctl restart mongod

Step 3 — Initiate the Replica Set (From One Server)

mongosh
rs.initiate({
  _id: "rs0",
  members: [
    { _id: 0, host: "server1:27017" },
    { _id: 1, host: "server2:27017" },
    { _id: 2, host: "server3:27017" }
  ]
})

Step 4 — Verify Replica Set Status

rs.status()

Confirm all members show as expected, with one PRIMARY and the others as SECONDARY.

Step 5 — Restrict Network Access Appropriately

Configure your firewall (see How to Configure UFW Firewall on a Linux VPS) to allow MongoDB's port (27017) only between the replica set members themselves and authorized application servers, never openly to the internet.

Connecting Your Application to the Replica Set

mongodb://server1:27017,server2:27017,server3:27017/mydb?replicaSet=rs0

The MongoDB driver automatically discovers the current primary and handles failover, routing writes correctly even after an election changes which server is primary.

Read Preference Configuration

readPreference=secondaryPreferred

Configure whether reads should go to the primary, secondaries, or a mix — useful for distributing read load, though be aware secondary reads may reflect slightly stale data given replication lag.

Testing Automatic Failover

Deliberately stop the current primary (in a test environment) and verify a secondary is automatically elected primary within a reasonable time — confirm your application correctly reconnects to the new primary without manual intervention.

Using an Arbiter (Cost-Saving Option for Odd Member Count)

rs.addArb("server4:27017")

An arbiter participates in elections (helping maintain quorum) without storing actual data — useful if you want an odd number of voting members without the cost of a full additional data-storing replica.

Common Errors

Replica set members can't see each other — verify network connectivity and firewall rules allow port 27017 between all member servers, and that hostnames used in the configuration actually resolve correctly from each server.

Continue Reading

Browse more articles in Databases.

  • mongodb replica set, mongodb high availability, mongodb failover, mongodb replication setup
  • 0 istifadəçi bunu faydalı hesab edir
Bu cavab sizə kömək etdi?

Uyğun məqalələr

How to Install and Secure MySQL 8 on Ubuntu & Debian

MySQL is one of the world's most widely used relational database systems, powering WordPress,...

How to Install MariaDB on Ubuntu & Debian

MariaDB is a community-developed, fully open-source fork of MySQL, offering strong compatibility...

How to Install PostgreSQL on Ubuntu & Debian

PostgreSQL is an advanced, standards-compliant open-source relational database known for...

How to Install MongoDB on Ubuntu & Debian

MongoDB is a NoSQL, document-oriented database designed for flexibility and horizontal...

How to Install and Secure Redis on Ubuntu & Debian

Redis is a fast, in-memory data store used as a cache, session store, message broker, and queue...