Skip to content

MongoDB Replica Set

A Replica Set is one of MongoDB's core high-availability features. It continuously transmits the primary node's oplog to secondary nodes and replays it to maintain consistency between primary and secondary nodes. Combined with a heartbeat mechanism, when the primary node becomes inaccessible or crashes, secondary nodes use an election mechanism to select a new primary node from the remaining secondary nodes, achieving automatic failover.

MongoDB Replica Sets are the minimum requirement for production environments. Of course, you can also deploy a sharded cluster for greater stability and performance.

Setting up a MongoDB Replica Set is relatively simple, but it still requires hands-on practice.

Prepare Your Raspberry Pi

I don't recommend practicing deployment on your own computer — it'll affect your work. This is where the Raspberry Pi comes in handy. First, get a Raspberry Pi with Linux and MongoDB installed. If you're not sure how, check out my earlier article.

Configuration Files

We'll create three nodes: one primary and two secondaries. First, create directories for each replica set's data and logs:

bash
mkdir data

cd data

mkdir -p data{1,2,3}

Create configuration files:

bash
sudo vim data1/mongod.conf

Enter the following:

bash
systemLog:
  destination: file
  path: data/data1/mongod.log   # Log file path
  logAppend: true
storage:
  dbPath: data/data1    # Data directory
net:
  bindIp: 0.0.0.0
  port: 28017   # Port must not conflict
replication:
  replSetName: rs0
processManagement:
  fork: true  # This is critical

Create configuration files for the other two nodes in their respective directories:

bash
systemLog:
  destination: file
  path: data/data2/mongod.log   # Log file path
  logAppend: true
storage:
  dbPath: data/data2    # Data directory
net:
  bindIp: 0.0.0.0
  port: 28018   # Port must not conflict
replication:
  replSetName: rs0
processManagement:
  fork: true
bash
systemLog:
  destination: file
  path: data/data3/mongod.log   # Log file path
  logAppend: true
storage:
  dbPath: data/data3    # Data directory
net:
  bindIp: 0.0.0.0
  port: 28019   # Port must not conflict
replication:
  replSetName: rs0
processManagement:
  fork: true

Start Processes

bash
cd ~

sudo mongod -f data/data1/mongod.conf

sudo mongod -f data/data2/mongod.conf

sudo mongod -f data/data3/mongod.conf

On successful startup, you should see:

success

Configure Replica Set

Enter the shell:

bash
mongo --port 28017

Create the replica set:

bash
rs.initiate({
  _id: "rs0",
  members: [{
      _id: 0,
      host: "localhost:28017"
  },{
      _id: 1,
      host: "localhost:28018"
  },{
      _id: 2,
      host: "localhost:28019"
  }]
})

Check the status:

bash
rs.status()

The shell will output the current status showing one PRIMARY and two SECONDARY nodes.

Summary

That completes our MongoDB Replica Set setup. It's not particularly difficult — for production environments, just replace localhost with your server IPs.