3-node MongoDB 8.0 replica set on Oracle Linux


STEP-BY-STEP TECHNICAL GUIDE

MongoDB 8.0

3-Node Replica Set on Oracle Linux

Installation  |  OS Tuning  |  Replication  |  Authentication  |  Switchover

Munish Kumar Karna

Senior Database Solution Architect  |  Oracle DBA

Lab environment: three Oracle Linux nodes. Two nodes run Oracle Linux 8 and one runs Oracle Linux 7. The replica set members are odi14c, odi12c and fleet.

What this guide covers

•      Installing MongoDB 8.0 from the official repository

•      Binding the network interface and starting the service

•      OS tuning: Transparent Huge Pages (THP), THP defragmentation and the memory allocator

•      Default databases: admin, config and local

•      Building a replica set (rs0) and administrative user

•      Keyfile authentication between nodes

•      Primary switchover with rs.stepDown()

1  Installation

Create the MongoDB repository file

sudo vi /etc/yum.repos.d/mongodb-org.repo

 

[mongodb-org-8.0]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/8.0/x86_64/

gpgcheck=1

enabled=1

gpgkey=https://pgp.mongodb.com/server-8.0.asc

 

Install MongoDB packages

[root@odi14c ~]# dnf install -y mongodb-org

 

MongoDB Repository                               55 kB/s |  75 kB     00:01

Last metadata expiration check: 0:00:01 ago on Thu 01 Oct 2026 12:27:32 AM EDT.

Dependencies resolved.

 

Title: Figure 1 - Description: MongoDB packages installed successfully

Figure 1. MongoDB packages installed successfully

2  Binding IP

vi /etc/mongod.conf

 

Title: Figure 2 - Description: net section of mongod.conf with bindIp set

Figure 2. net section of mongod.conf with bindIp set

3  Start MongoDB

systemctl start mongod

systemctl status mongod

 

Title: Figure 3 - Description: mongod service active (running)

Figure 3. mongod service active (running)

4  OS Tuning for MongoDB

Disable Transparent Huge Pages (THP)

MongoDB's memory allocator (TCMalloc) heavily prefers the Transparent Huge Pages setting to be explicitly turned off (0 or never) to avoid severe performance degradation and latency spikes.

Run the following command to see if THP is active:

cat /sys/kernel/mm/transparent_hugepage/enabled

 

If it shows [always] or [madvise] enclosed in brackets, THP is active.

Add the kernel parameters to disable THP and its defragmentation system:

grubby --update-kernel=ALL --args="transparent_hugepage=never"

 

Reboot the system and verify:

Title: Figure 4 - Description: THP is now set to [never]

Figure 4. THP is now set to [never]

What the three options in the output mean:

•      always: THP is fully enabled across the system.

•      madvise: THP is only used for applications that explicitly request it using the madvise() system call.

•      [never]: THP is completely deactivated system-wide.

Configure THP defragmentation

Check THP defragmentation:

[root@odi14c ~]# cat /sys/kernel/mm/transparent_hugepage/defrag

always defer defer+madvise [madvise] never

 

The defrag setting above is currently set to [madvise] (indicated by the brackets).

Fix:

sudo sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/defrag'

 

Verify:

Title: Figure 5 - Description: THP defrag is now set to [never]

Figure 5. THP defrag is now set to [never]

Configure the memory allocator (GLIBC_TUNABLES)

Create a systemd drop-in configuration directory for MongoDB:

Title: Figure 6 - Description: Creating the mongod.service.d drop-in directory

Figure 6. Creating the mongod.service.d drop-in directory

Create a service override file to handle kernel environment tuning (GLIBC_TUNABLES):

vi /etc/systemd/system/mongod.service.d/options.conf

 

[Service]

Environment="GLIBC_TUNABLES=glibc.pthread.rseq=0"

 

Title: Figure 7 - Description: options.conf override file

Figure 7. options.conf override file

Restart MongoDB:

systemctl restart mongod

 

Title: Figure 8 - Description: mongosh connection attempt after the restart

Figure 8. mongosh connection attempt after the restart

Check version:

Title: Figure 9 - Description: MongoDB version 8.0.32

Figure 9. MongoDB version 8.0.32

5  Default Databases at a Glance

Database

Primary purpose

What it is used for

1. admin

Authentication, Authorization, and Global Administration.

Storing user credentials, roles, and access control permissions.

2. config

Cluster Topology and Metadata.

Primarily used in sharded clusters to store information about the cluster layout, chunk ranges, and which shard holds which data.

3. local

Node-Specific Data (Never Replicated).

Storing data that is strictly local to that specific machine and never replicated to other nodes in a cluster.

 

6  Replication

Configure replication on every node

Modify the replication section to bind to your network interface and assign a replica set name:

Title: Figure 10 - Description: mongod.conf on the first node: bindIp 192.168.46.150 and replSetName "rs0"

Figure 10. mongod.conf on the first node: bindIp 192.168.46.150 and replSetName "rs0"

Title: Figure 11 - Description: mongod.conf on the second node: bindIp 192.168.46.140 and replSetName "rs0"

Figure 11. mongod.conf on the second node: bindIp 192.168.46.140 and replSetName "rs0"

Title: Figure 12 - Description: mongod.conf on the third node: bindIp 192.168.46.151 and replSetName "rs0"

Figure 12. mongod.conf on the third node: bindIp 192.168.46.151 and replSetName "rs0"

Restart MongoDB on all nodes

systemctl restart mongod

 

Initialize the replica set

Connect to the MongoDB shell on your primary node:

mongosh

 

Check IP and port reachability from all nodes:

nc -zv <ip address> 27017

 

Initiate the replica set using the rs.initiate() command with your node hostnames:

rs.initiate({

  _id: "rs0",

  members: [

    { _id: 0, host: "odi14c:27017" },

    { _id: 1, host: "odi12c:27017" },

    { _id: 2, host: "fleet:27017" }

  ]

})

 

Title: Figure 13 - Description: rs.initiate() returns ok: 1

Figure 13. rs.initiate() returns ok: 1

Check replication status

Title: Figure 14 - Description: rs.status() output, view 1

Figure 14. rs.status() output, view 1

Title: Figure 15 - Description: rs.status() output, view 2

Figure 15. rs.status() output, view 2

Title: Figure 16 - Description: rs.status() output, view 3

Figure 16. rs.status() output, view 3

Title: Figure 17 - Description: Shell prompt confirming the node is PRIMARY

Figure 17. Shell prompt confirming the node is PRIMARY

7  Create an Administrative User

Switch to the admin database:

use admin

 

Title: Figure 18 - Description: Switched to the admin database

Figure 18. Switched to the admin database

Create the administrative user:

db.createUser({

  user: "clusterAdmin",

  pwd: "<your-password>",

  roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { role: "clusterAdmin", db: "admin" } ]

})

 

Title: Figure 19 - Description: clusterAdmin user created

Figure 19. clusterAdmin user created

Verify the user

db.runCommand({ usersInfo: "clusterAdmin" })

 

Title: Figure 20 - Description: User details on the primary node

Figure 20. User details on the primary node

Replication is already configured between the nodes, so the same user can be verified on the other nodes:

Title: Figure 21 - Description: The same user replicated to the second node

Figure 21. The same user replicated to the second node

Title: Figure 22 - Description: The same user replicated to the third node

Figure 22. The same user replicated to the third node

8  Enable Keyfile Authentication for Data Synchronization

Generate the custom keyfile

[root@odi14c ~]# openssl rand -base64 756 > /tmp/custom-mongodb-keyfile

 

Title: Figure 23 - Description: Keyfile generated and copied to /etc

Figure 23. Keyfile generated and copied to /etc

Copy the keyfile to all nodes

scp /etc/custom-mongodb-keyfile fleet:/etc/

scp /etc/custom-mongodb-keyfile odi12c:/etc/

 

Change permission and ownership

chmod 400 /etc/custom-mongodb-keyfile

chown mongod:mongod /etc/custom-mongodb-keyfile

 

Update mongod.conf on all nodes

Title: Figure 24 - Description: security section with authorization enabled and keyFile

Figure 24. security section with authorization enabled and keyFile

Restart the mongod service on all nodes

systemctl restart mongod

 

Verify that all nodes are communicating properly

Title: Figure 25 - Description: All members healthy: fleet is PRIMARY, odi14c and odi12c are SECONDARY

Figure 25. All members healthy: fleet is PRIMARY, odi14c and odi12c are SECONDARY

9  Switchover

Get priorities for all nodes

Title: Figure 26 - Description: Replica set configuration: all members have priority 1 and 1 vote

Figure 26. Replica set configuration: all members have priority 1 and 1 vote

Verify the primary before switchover

Title: Figure 27 - Description: odi12c is currently PRIMARY

Figure 27. odi12c is currently PRIMARY

Goal: Currently odi12c is primary, and I want another node to become primary.

Force the current primary to step down for 30 seconds

rs.stepDown(30)

 

Title: Figure 28 - Description: rs.stepDown(30) executed on the primary

Figure 28. rs.stepDown(30) executed on the primary

Verify

Title: Figure 29 - Description: odi14c is now PRIMARY

Figure 29. odi14c is now PRIMARY

Result: Switchover complete: odi14c became the primary node.

 

Comments