Configure MongoDB 4.4 replica set on Ubuntu 20.04 | 18.04 | 16.04
You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose
In this guide, I will show you how to configure MongoDB replica set on Ubuntu 20.04 | 18.04 | 16.04 server. MongoDB is an enterprise-level NoSQL database system with automatic expansion, high availability and high performance. In NoSQL databases, data is stored in the document structure using MongoDB BSON format. You cannot use SQL statements to insert or retrieve data in MongoDB.
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 foundation of all production deployments because they provide data redundancy and high availability.
This tutorial will cover the steps required to create a valid three-node replica set without configuring access control to restrict access. This guide is based on the following system IP addresses:
MongoDB Server1: 10.10.5.2
MongoDB Server2: 10.10.5.3
MongoDB Server3: 10.10.5.4
Step 1: Set requirements
Before configuring the replica set, you must install MongoDB on all servers. Ensure that the correct hostname is configured on the server used for replication.
If you do not have an available DNS server, please set the mapping between IP address and FQDN locally on the server.
# Edit and set the values correctly
10.10.5.2 mongodb-01.example.net mongodb-01
10.10.5.3 mongodb-02.example.net mongodb-02
10.10.5.4 mongodb-03.example.net mongodb-03
Step 2: Install MongoDB on all Ubuntu 20.04 | 18.04 nodes
Please follow the steps below to install MongoDB on the Ubuntu server.
Update system:
sudo apt-get update
sudo apt-get upgrade && sudo reboot
Import MongoDB public GPG key:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
After importing the key, add the repository:
### Ubuntu 20.04 ###
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
### Ubuntu 18.04 ###
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
### Ubuntu 16.04 ###
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Install MongoDB package
sudo apt-get update
sudo apt install -y mongodb-org
Use a dedicated disk to store MongoDB data (optional)
For my setup, I have 10GB Specially used for auxiliary storage of MongoDB data.I partitioned it and installed it to /data/mongodb.
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 32G 0 disk
|-sda1 8:1 0 487M 0 part /boot
|-sda2 8:2 0 1.9G 0 part [SWAP]
`-sda3 8:3 0 29.6G 0 part /
vda 252:0 0 10G 0 disk
Create a GPT partition table for the secondary disk, it can be multiple disks
parted -s -a optimal -- /dev/vda mklabel gpt
parted -s -a optimal -- /dev/vda mkpart primary 0% 100%
parted -s -- /dev/vda align-check optimal 1
Then create the LVM volume, which will make it easy to extend the partition
$ sudo pvcreate /dev/vda1
Physical volume "/dev/vda1" successfully created.
$ sudo vgcreate vg11 /dev/vda1
Volume group "vg11" successfully created
$ sudo lvcreate -n data -l 100%FREE vg11
Logical volume "data" created
Create ext4
The file system created on the logical volume.
$ sudo mkfs.ext4 /dev/mapper/vg11-data
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 2620416 4k blocks and 655360 inodes
Filesystem UUID: b98e07e5-1b04-4282-a9db-fa5b73c74d2f
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
Create mount point and mount partition
echo "/dev/mapper/vg11-data /data ext4 defaults 0 0" | sudo tee -a /etc/fstab
mkdir /data
mount -a
Confirm that the partition is installed successfully:
$ df -hT | grep /data
/dev/mapper/vg11-data ext4 9.8G 37M 9.3G 1% /data
Create a folder for MongoDB data
sudo mkdir /data/mongodb
sudo chown -R mongodb:mongodb /data/mongodb
sudo chmod -R 775 /data/mongodb
Configure the storage path to the storage path previously created on all nodes
$ sudo vim /etc/mongod.conf
# Where and how to store data.
storage:
dbPath: /data/mongodb
journal:
enabled: true
Step 3: Configure MongoDB replica set
Now that we have prepared all the necessary things, let’s proceed to configure the MongoDB replica set.
Change MongoDB listening address from local IP on all nodes
$ sudo vim /etc/mongod.conf
# 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.
Configure MongoDB replica set
One of the MongoDB nodes starts with PRIMARY
, All other nodes will be SECONDARY
.Data is always PRIMARY
Node, then copy the data set to all other nodes SECONDARY
node.
Edit the MongoDB configuration file mongod.conf and enable replica sets 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 OS boot
sudo systemctl enable mongod.service
sudo systemctl restart mongod.service
Check the listening address of the MongoDB service:
# 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 <->
Start MongoDB replica set
Our MongoDB Node1 (mongodb-01)will be PRIMARY
And the other two will act as SECONDARY
Log in to the mongodb-01 server and start the mongo shell.
$ mongo 10.10.5.2
MongoDB shell version v4.0.1
connecting to: mongodb://10.10.5.2:27017/test
MongoDB server version: 4.0.1
Welcome to the MongoDB shell.
For interactive help, type "help".
...
>
Initialize the replica set node1
By running the following 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 node
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) } } }
Use the following command to check the status of the replica set:
replica01:PRIMARY> rs.status()
Check the status of the master station
replica01:PRIMARY> rs.isMaster()
Test 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
in conclusion
Now you have valid MongoDB replication.You can increase the cluster size from three nodes to Fives node Seven Nodes or more. This number is calculated according to the following formula: [Master Eligible Nodes) / 2 + 1], Which means that high availability requires an odd number of nodes, the minimum number is three.
reference:
- https://docs.mongodb.com/master/replication/
- https://docs.mongodb.com/manual/tutorial/deploy-replica-set/
You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose