Home

Setting up a MongoDB replica set on Ubuntu 20.04

What is a replica set?

A MongoDB replica set is a group of mongod instances that hold the same data. It gives you high availability, redundancy, and an easy path to scale reads. If a node fails, an automatic failover elects a new primary and the cluster keeps serving traffic.

Architecture is simple: at any moment exactly one node is the primary (accepts writes), and the rest are secondaries (replicate from the primary, can serve reads). A replica set should have at least 3 nodes so a majority can elect a primary even if one is down.

For a single-node MongoDB install on Ubuntu, see my other post. This one assumes you already have it on each box.

What you'll need

Configure each node

The key edits in /etc/mongod.conf: bind the right IP and tell MongoDB it's part of a replica set.

In this guide:

bash
node 1 = 192.168.159.135node 2 = 192.168.159.136node 3 = 192.168.159.137

Edit /etc/mongod.conf on each node:

bash
sudo vi /etc/mongod.conf

Node 1 — bind its own IP, set the replica set name:

yaml
net:  port: 27017  bindIp: 192.168.159.135replication:  replSetName: "rs1"

Node 2 — same thing, but its own IP:

yaml
net:  port: 27017  bindIp: 192.168.159.136replication:  replSetName: "rs1"

Node 3:

yaml
net:  port: 27017  bindIp: 192.168.159.137replication:  replSetName: "rs1"

Restart MongoDB on every node so the changes take effect:

bash
sudo systemctl restart mongod

Initiate the replica set

Connect to node 1 with mongosh:

bash
mongosh --host 192.168.159.135

Initiate the replica set:

bash
rs.initiate()

Sample output:

bash
{  info2: 'no configuration specified. Using a default configuration for the set',  me: '192.168.159.135:27017',  ok: 1,  ...}

Now add the other two nodes:

bash
rs.add("192.168.159.136")rs.add("192.168.159.137")

Each call returns { ok: 1, ... }. After this, MongoDB runs an election internally and picks a primary.

Verify the cluster

bash
rs.status()

You'll get a long object back. The interesting bits are under members[] — each node has a stateStr (PRIMARY or SECONDARY) and a health: 1. A healthy 3-node replica set looks like:

bash
members: [  { _id: 0, name: '192.168.159.135:27017', health: 1, stateStr: 'PRIMARY',   ... },  { _id: 1, name: '192.168.159.136:27017', health: 1, stateStr: 'SECONDARY', ... },  { _id: 2, name: '192.168.159.137:27017', health: 1, stateStr: 'SECONDARY', ... }]

That's it — you've got a 3-node replica set. Writes go to the primary; reads can be routed to the primary or any secondary.

Smoke test: insert and read across the set

On the primary, insert a document:

bash
use linuxpedidb.linuxpedi.insertOne({ "name": "MertYavuz" })

Connect to one of the secondaries:

bash
mongosh --host 192.168.159.136

Reads on a secondary are disabled by default — you need to opt in for the session:

bash
rs.secondaryOk()// ordb.getMongo().setReadPref('secondaryPreferred')

Then query:

bash
use linuxpedidb.linuxpedi.find().pretty()

You should see the document you inserted on the primary. Replication's working.

A few production-readiness notes

This guide gets you a working replica set, but for real workloads also think about: