In this guide, I’m going to show you how to configure MongoDB Replica Set on Ubuntu server. MongoDB is an enterprise-class NoSQL database system with auto-scaling, high availability and high performance. In a NoSQL database, data is stored in a document structure using MongoDB BSON format. SQL statements cannot be used in MongoDB to insert or retrieve data.
What is MongoDB Replication / Replica set?
In MongoDB, a replica set is a group of mongod processes that maintain the same data set. Replica sets are the basis for all production deployments as they provide data redundancy and high availability.

This tutorial will describe the steps required to create a working three nodes replica set without configured access control to restrict access. This guide is based on below system IP Addresses:
Node1: 10.10.5.2
Node2: 10.10.5.3
Node3: 10.10.5.4
Step 1: Setup Requirements
Before you can configure a replica set, you must install MongoDB on each system that will be part of the replica set. Set correct hostnames for all the servers. If you have an active DNS server, add A records for all servers, or modify /etc/hosts file. Add these on all nodes.
$ sudo vim /etc/hosts
10.10.5.2 mongodb-01
10.10.5.3 mongodb-02
10.10.5.4 mongodb-03
FYI: My Lab Setup is based on below Vagrantfile
running on KVM.
# -*- mode: ruby -*-
# vim: set ft=ruby :
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'
# Check required plugins
REQUIRED_PLUGINS_LIBVIRT = %w(vagrant-libvirt)
exit unless REQUIRED_PLUGINS_LIBVIRT.all? do |plugin|
Vagrant.has_plugin?(plugin) || (
puts "The #{plugin} plugin is required. Please install it with:"
puts "$ vagrant plugin install #{plugin}"
false
)
end
Vagrant.configure("2") do |config|
config.vm.define "mongodb-01" do |node|
node.vm.hostname = "mongodb-01"
node.vm.box = "generic/ubuntu2204"
node.vm.box_check_update = false
#node.vm.synced_folder '.', '/vagrant', :disabled => true
node.vm.network "private_network", ip: "10.10.5.2"
node.vm.provider :libvirt do |domain|
domain.memory = 2048
domain.storage :file, :size => '20G'
end
end
config.vm.define "mongodb-02" do |node|
node.vm.hostname = "mongodb-02"
node.vm.box = "generic/ubuntu2204"
node.vm.box_check_update = false
#node.vm.synced_folder '.', '/vagrant', :disabled => true
node.vm.network "private_network", ip: "10.10.5.3"
node.vm.provider :libvirt do |domain|
domain.memory = 2048
domain.storage :file, :size => '20G'
end
end
config.vm.define "mongodb-03" do |node|
node.vm.hostname = "mongodb-03"
node.vm.box = "generic/ubuntu2204"
node.vm.box_check_update = false
#node.vm.synced_folder '.', '/vagrant', :disabled => true
node.vm.network "private_network", ip: "10.10.5.4"
node.vm.provider :libvirt do |domain|
domain.memory = 2048
domain.storage :file, :size => '20G'
end
end
end
I then ran vagrant up
to start all VMs
Step 2: Install MongoDB on all nodes
If you have not already installed MongoDB, see our installation guide How to Install MongoDB Server on Ubuntu. I will include installation guide here for convenience.
Update OS repository list and packages index:
sudo apt-get update
Import MongoDB public GPG Key:
- MongoDB 7
curl -fsSL https://pgp.mongodb.com/server-7.0.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-7.gpg
echo "deb https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
- MongoDB 6
curl -fsSL https://pgp.mongodb.com/server-6.0.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-6.gpg
echo "deb https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
- MongoDB 5
curl -fsSL https://pgp.mongodb.com/server-5.0.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-5.gpg
echo "deb https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
- MongoDB 4
curl -fsSL https://pgp.mongodb.com/server-4.4.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/mongodb-4.gpg
echo "deb https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Update package APT index:
$ sudo apt update
Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
Get:2 http://mirrors.digitalocean.com/ubuntu focal InRelease [265 kB]
Ign:3 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 InRelease
Get:4 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release [5372 B]
Hit:5 http://repo.zabbix.com/zabbix/5.0/ubuntu focal InRelease
Hit:6 http://mirrors.digitalocean.com/ubuntu focal-updates InRelease
Get:7 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release.gpg [801 B]
Hit:8 http://mirrors.digitalocean.com/ubuntu focal-backports InRelease
Get:9 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4/multiverse amd64 Packages [10.7 kB]
Get:10 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4/multiverse arm64 Packages [7669 B]
Fetched 289 kB in 1s (347 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
Install MongoDB server and client
Install MongoDB packages
sudo apt update
sudo apt install -y openssl libcurl4 mongodb-org
Step 3: Configure MongoDB Replica set
Now that we have everything needed ready, let’s proceed to configure MongoDB replica set.
Change MongoDB Listen Address from localhost IP on all nodes
sudo nano /etc/mongod.conf
See my configurations as shared below:
# node 1
# network interfaces
net:
port: 27017
bindIp: 10.10.5.2 # Listen to local interface only, comment to listen on all interfaces.
# node 2
# network interfaces
net:
port: 27017
bindIp: 10.10.5.3 # Listen to local interface only, comment to listen on all interfaces.
# node 3
# network interfaces
net:
port: 27017
bindIp: 10.10.5.4 # Listen to local interface only, comment to listen on all interfaces.
Configuring MongoDB Replica Set
One of the MongoDB nodes run as the PRIMARY
, and all other nodes will work as SECONDARY
. Data is always to the PRIMARY
node and the data sets are then replicated to all other SECONDARY
nodes.
Edit the MongoDB configuration file mongod.conf and enable replica set on all nodes
replication:
replSetName: "replica01"
Open port 27017/tcp
on the firewall:
sudo ufw enable
sudo ufw allow ssh
sudo ufw allow 27017/tcp
Configure MongoDB to start during the operating system’s boot
sudo systemctl enable mongod.service
sudo systemctl restart mongod.service
Check the listen Address of MongoDB service:
$ sudo ss -tunelp | grep -i mongo
tcp LISTEN 0 128 10.10.5.2:27017 0.0.0.0:* users:(("mongod",pid=15288,fd=11)) uid:111 ino:46927 sk:4 <->
Initiate MongoDB Replica Set
Our MongoDB Node1 (mongodb-01) will be the PRIMARY
and the other two will act as SECONDARY
Login to the mongodb-01 server and start the mongo shell.
# Older versions
mongo 10.10.5.2
# Newer versions
mongosh 10.10.5.2
Initialize replica set on node1
by running below command:
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "10.10.5.2:27017",
"ok" : 1,
"operationTime" : Timestamp(1534797235, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534797235, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Make sure you get 1
for ok
state
Add secondary nodes
replica01:PRIMARY> rs.add("mongodb-02")
{
"ok" : 1,
"operationTime" : Timestamp(1534797580, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534797580, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
replica01:PRIMARY> rs.add("mongodb-03")
{
"ok" : 1,
"operationTime" : Timestamp(1534797614, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1534797614, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Check replica set status using:
replica01:PRIMARY> rs.status()
Check the status of the master
replica01:PRIMARY> rs.isMaster()
Testing MongoDB Replication
Create a test database on mongodb-01
replica01:PRIMARY> use test_db
switched to db test_db
Add some data
> db.test.save(
{
"desc": "My Test Database",
"apps": ["Test1", "Test2", "Test3", "Test4"],
})
replica01:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test_db 0.000GB
replica01:PRIMARY> use test_db
switched to db test_db
replica01:PRIMARY> show collections
test
Conclusion
You now have a working MongoDB replication. You can increase the cluster size from three nodes to five nodes, seven nodes or more. This number is calculated from: [Master Eligible Nodes) / 2 + 1], which means an odd number of nodes is required for high availability, the minimum number is three.
Reference:
- https://docs.mongodb.com/master/replication/
- https://docs.mongodb.com/manual/tutorial/deploy-replica-set/