Ubuntu 20.04: setup MongoDB Replica Set


Example we install MongoDB v5.0
We have 4 servers :

167.235.17.110
167.235.17.111
167.235.17.112
167.235.17.113

Install MongoDB v5.0 on each server
Step 1*:
sudo apt-get install gnupg

root@Ubuntu-2004-focal-amd64-base ~ # sudo apt-get install gnupg
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-image-5.4.0-121-generic linux-modules-5.4.0-121-generic
  linux-modules-extra-5.4.0-121-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  dirmngr gnupg-l10n gnupg-utils gpg-wks-client gpg-wks-server gpgsm libksba8
Suggested packages:
  dbus-user-session pinentry-gnome3 tor parcimonie xloadimage
The following NEW packages will be installed:
  dirmngr gnupg gnupg-l10n gnupg-utils gpg-wks-client gpg-wks-server gpgsm
  libksba8
0 upgraded, 8 newly installed, 0 to remove and 15 not upgraded.
Need to get 1,618 kB of archives.
After this operation, 4,729 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://de.archive.ubuntu.com/ubuntu focal/main amd64 libksba8 amd64 1.3.5-2 [92.6 kB]
Get:2 http://de.archive.ubuntu.com/ubuntu focal-updates/main amd64 dirmngr amd64 2.2.19-3ubuntu2.2 [330 kB]
Get:3 http://de.archive.ubuntu.com/ubuntu focal-updates/main amd64 gnupg-l10n all 2.2.19-3ubuntu2.2 [51.7 kB]
Get:4 http://de.archive.ubuntu.com/ubuntu focal-updates/main amd64 gnupg-utils amd64 2.2.19-3ubuntu2.2 [481 kB]
Get:5 http://de.archive.ubuntu.com/ubuntu focal-updates/main amd64 gpg-wks-client amd64 2.2.19-3ubuntu2.2 [97.4 kB]
Get:6 http://de.archive.ubuntu.com/ubuntu focal-updates/main amd64 gpg-wks-server amd64 2.2.19-3ubuntu2.2 [90.2 kB]
Get:7 http://de.archive.ubuntu.com/ubuntu focal-updates/main amd64 gpgsm amd64 2.2.19-3ubuntu2.2 [217 kB]
Get:8 http://de.archive.ubuntu.com/ubuntu focal-updates/main amd64 gnupg all 2.2.19-3ubuntu2.2 [259 kB]
Fetched 1,618 kB in 0s (7,538 kB/s)
Selecting previously unselected package libksba8:amd64.
(Reading database ... 36873 files and directories currently installed.)
Preparing to unpack .../0-libksba8_1.3.5-2_amd64.deb ...
Unpacking libksba8:amd64 (1.3.5-2) ...
Selecting previously unselected package dirmngr.
Preparing to unpack .../1-dirmngr_2.2.19-3ubuntu2.2_amd64.deb ...
Unpacking dirmngr (2.2.19-3ubuntu2.2) ...
Selecting previously unselected package gnupg-l10n.
Preparing to unpack .../2-gnupg-l10n_2.2.19-3ubuntu2.2_all.deb ...
Unpacking gnupg-l10n (2.2.19-3ubuntu2.2) ...
Selecting previously unselected package gnupg-utils.
Preparing to unpack .../3-gnupg-utils_2.2.19-3ubuntu2.2_amd64.deb ...
Unpacking gnupg-utils (2.2.19-3ubuntu2.2) ...
Selecting previously unselected package gpg-wks-client.
Preparing to unpack .../4-gpg-wks-client_2.2.19-3ubuntu2.2_amd64.deb ...
Unpacking gpg-wks-client (2.2.19-3ubuntu2.2) ...
Selecting previously unselected package gpg-wks-server.
Preparing to unpack .../5-gpg-wks-server_2.2.19-3ubuntu2.2_amd64.deb ...
Unpacking gpg-wks-server (2.2.19-3ubuntu2.2) ...
Selecting previously unselected package gpgsm.
Preparing to unpack .../6-gpgsm_2.2.19-3ubuntu2.2_amd64.deb ...
Unpacking gpgsm (2.2.19-3ubuntu2.2) ...
Selecting previously unselected package gnupg.
Preparing to unpack .../7-gnupg_2.2.19-3ubuntu2.2_all.deb ...
Unpacking gnupg (2.2.19-3ubuntu2.2) ...
Setting up libksba8:amd64 (1.3.5-2) ...
Setting up gpgsm (2.2.19-3ubuntu2.2) ...
Setting up dirmngr (2.2.19-3ubuntu2.2) ...
Created symlink /etc/systemd/user/sockets.target.wants/dirmngr.socket → /usr/lib/systemd/user/dirmngr.socket.
Setting up gnupg-l10n (2.2.19-3ubuntu2.2) ...
Setting up gpg-wks-server (2.2.19-3ubuntu2.2) ...
Setting up gnupg-utils (2.2.19-3ubuntu2.2) ...
Setting up gpg-wks-client (2.2.19-3ubuntu2.2) ...
Setting up gnupg (2.2.19-3ubuntu2.2) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...

Step 2*:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

Step 3*:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Step 4*:
sudo apt-get update

root@Ubuntu-2004-focal-amd64-base ~ # sudo apt-get update
Hit:1 http://mirror.hetzner.de/ubuntu/packages focal InRelease
Hit:2 http://mirror.hetzner.de/ubuntu/packages focal-updates InRelease
Hit:3 http://mirror.hetzner.de/ubuntu/packages focal-backports InRelease
Hit:4 http://mirror.hetzner.de/ubuntu/packages focal-security InRelease
Hit:5 http://de.archive.ubuntu.com/ubuntu focal InRelease
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:7 http://de.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:8 http://de.archive.ubuntu.com/ubuntu focal-backports InRelease
Ign:9 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 InRelease
Get:10 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 Release [4,406 B]
Get:11 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 Release.gpg [801 B]
Get:12 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 Packages [22.1 kB]
Get:13 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse arm64 Packages [18.3 kB]
Fetched 45.6 kB in 1s (49.1 kB/s)
Reading package lists... Done

Step 5*: install mongodb package
sudo apt-get install -y mongodb-org

root@Ubuntu-2004-focal-amd64-base ~ # sudo apt-get install -y mongodb-org
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-image-5.4.0-121-generic linux-modules-5.4.0-121-generic
  linux-modules-extra-5.4.0-121-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org-database
  mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server
  mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org mongodb-org-database
  mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server
  mongodb-org-shell mongodb-org-tools
0 upgraded, 9 newly installed, 0 to remove and 15 not upgraded.
Need to get 145 MB of archives.
After this operation, 463 MB of additional disk space will be used.
Get:1 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-database-tools amd64 100.6.0 [47.8 MB]
Get:2 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-mongosh amd64 1.6.0 [37.4 MB]
Get:3 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-shell amd64 5.0.12 [14.4 MB]
Get:4 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-server amd64 5.0.12 [26.4 MB]
Get:5 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-mongos amd64 5.0.12 [18.6 MB]
Get:6 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-database-tools-extra amd64 5.0.12 [7,752 B]
Get:7 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-database amd64 5.0.12 [3,544 B]
Get:8 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org-tools amd64 5.0.12 [2,896 B]
Get:9 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0/multiverse amd64 mongodb-org amd64 5.0.12 [2,932 B]
Fetched 145 MB in 3s (53.5 MB/s)
Selecting previously unselected package mongodb-database-tools.
(Reading database ... 37017 files and directories currently installed.)
Preparing to unpack .../0-mongodb-database-tools_100.6.0_amd64.deb ...
Unpacking mongodb-database-tools (100.6.0) ...
Selecting previously unselected package mongodb-mongosh.
Preparing to unpack .../1-mongodb-mongosh_1.6.0_amd64.deb ...
Unpacking mongodb-mongosh (1.6.0) ...
Selecting previously unselected package mongodb-org-shell.
Preparing to unpack .../2-mongodb-org-shell_5.0.12_amd64.deb ...
Unpacking mongodb-org-shell (5.0.12) ...
Selecting previously unselected package mongodb-org-server.
Preparing to unpack .../3-mongodb-org-server_5.0.12_amd64.deb ...
Unpacking mongodb-org-server (5.0.12) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../4-mongodb-org-mongos_5.0.12_amd64.deb ...
Unpacking mongodb-org-mongos (5.0.12) ...
Selecting previously unselected package mongodb-org-database-tools-extra.
Preparing to unpack .../5-mongodb-org-database-tools-extra_5.0.12_amd64.deb ...
Unpacking mongodb-org-database-tools-extra (5.0.12) ...
Selecting previously unselected package mongodb-org-database.
Preparing to unpack .../6-mongodb-org-database_5.0.12_amd64.deb ...
Unpacking mongodb-org-database (5.0.12) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../7-mongodb-org-tools_5.0.12_amd64.deb ...
Unpacking mongodb-org-tools (5.0.12) ...
Selecting previously unselected package mongodb-org.
Preparing to unpack .../8-mongodb-org_5.0.12_amd64.deb ...
Unpacking mongodb-org (5.0.12) ...
Setting up mongodb-mongosh (1.6.0) ...
Setting up mongodb-org-server (5.0.12) ...
Adding system user `mongodb' (UID 107) ...
Adding new user `mongodb' (UID 107) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 113) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org-shell (5.0.12) ...
Setting up mongodb-database-tools (100.6.0) ...
Setting up mongodb-org-mongos (5.0.12) ...
Setting up mongodb-org-database-tools-extra (5.0.12) ...
Setting up mongodb-org-database (5.0.12) ...
Setting up mongodb-org-tools (5.0.12) ...
Setting up mongodb-org (5.0.12) ...

Step 6*: edit file /etc/mongod.conf

nano /etc/mongod.conf

...
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,167.235.17.110
...
replication:
  replSetName: mongodb-replicaset
...

Step 7*: start mongodb
sudo systemctl start mongod

Step 8*: UFW

sudo ufw allow from 167.235.17.0/24 to any port 27017

read more: How To Set Up a Firewall with UFW on Ubuntu 20.04

Step 9*: add to /etc/hosts file:

167.235.17.110 mongoset1
167.235.17.111 mongoset2
167.235.17.112 mongoset3
167.235.17.113 mongoset4

Step 10: Configure Replica Set

On master server (167.235.17.110)

Use mongo :

db = (new Mongo('mongoset1:27017')).getDB('test')
config={"_id":"mongodb-replicaset","members":[{"_id":0,"host":"mongoset1:27017"},{"_id":1,"host":"mongoset2:27017"},{"_id":2,"host":"mongoset3:27017"},{"_id":3,"host":"mongoset4:27017"}]}
rs.initiate(config)

Done!

Leave a Reply