Continuing with MongoDB article, our last one, we beginning setting the SO and installing MongoDB database. Now, we are going to set directories for 3 different instances, and create 3 different mongod.conf running on these directories.
First, lets create the directories:
[root@mongodbsrv ~]$ mkdir -p /srv/mongodb/rs0-0 /srv/mongodb/rs0-1 /srv/mongodb/rs0-2
Now, lets create .conf files:
[root@mongodbsrv ~]$ vim /etc/mongod_rs0-0.conf
replication:
replSetName: "rs0"
oplogSizeMB: 128
net:
bindIp: localhost,mongodbsrv|192.168.1.221
port: 27017
storage:
dbPath: /srv/mongodb/rs0-0
processManagement:
fork: true
pidFilePath: /srv/mongodb/rs0-0/mongod.pid
systemLog:
destination: file
path: "/srv/mongodb/rs0-0/mongod.log"
logAppend: true
[root@mongodbsrv ~]$ cp /etc/mongod_rs0-0.conf /etc/mongod_rs0-1.conf
[root@mongodbsrv ~]$ cp /etc/mongod_rs0-0.conf /etc/mongod_rs0-2.conf
Lets modify each file for each MongoDB instance:
[root@mongodbsrv ~]$ vim /etc/mongod_rs0-1.conf
replication:
replSetName: "rs0"
oplogSizeMB: 128
net:
bindIp: localhost,mongodbsrv|192.168.1.221
port: 27018
storage:
dbPath: /srv/mongodb/rs0-1
processManagement:
fork: true
pidFilePath: /srv/mongodb/rs0-1/mongod.pid
systemLog:
destination: file
path: "/srv/mongodb/rs0-1/mongod.log"
logAppend: true
[root@mongodbsrv ~]$ vim /etc/mongod_rs0-2.conf
replication:
replSetName: rs0
oplogSizeMB: 128
net:
bindIp: localhost,mongodbsrv|192.168.1.221
port: 27019
storage:
dbPath: /srv/mongodb/rs0-2
processManagement:
fork: true
pidFilePath: /srv/mongodb/rs0-2/mongod.pid
systemLog:
destination: file
path: "/srv/mongodb/rs0-2/mongod.log"
logAppend: true
Lets first stop and removing mongod service, if running:
[root@mongodbsrv ~]$ systemctl stop mongod
[root@mongodbsrv ~]$ systemctl status mongod
○ mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; preset: disabled)
Active: inactive (dead) since Sun 2026-03-22 21:07:49 -03; 48s ago
Duration: 31min 4.533s
Docs: https://docs.mongodb.org/manual
Process: 1096 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 1096 (code=exited, status=0/SUCCESS)
CPU: 14.865s
Mar 22 20:36:44 mongodbsrv systemd[1]: Started MongoDB Database Server.
Mar 22 20:36:44 mongodbsrv mongod[1096]: {"t":{"$date":"2026-03-22T23:36:44.535Z"},"s":"I", "c":"CONTROL", "id":10181001,"ctx":"main","msg":"Unable to open procfs path, falling back to default value","attr":{"p>
Mar 22 20:36:44 mongodbsrv mongod[1096]: {"t":{"$date":"2026-03-22T23:36:44.535Z"},"s":"I", "c":"CONTROL", "id":7484500, "ctx":"main","msg":"Environment variable MONGODB_CONFIG_OVERRIDE_NOFORK == 1, overriding >
Mar 22 21:07:48 mongodbsrv systemd[1]: Stopping MongoDB Database Server...
Mar 22 21:07:49 mongodbsrv systemd[1]: mongod.service: Deactivated successfully.
Mar 22 21:07:49 mongodbsrv systemd[1]: Stopped MongoDB Database Server.
Mar 22 21:07:49 mongodbsrv systemd[1]: mongod.service: Consumed 14.865s CPU time, 216.9M memory peak.
[root@mongodbsrv ~]$ systemctl disable mongod
Removed "/etc/systemd/system/multi-user.target.wants/mongod.service".
We can start the instances to test if everything its ok:
[root@mongodbsrv ~]$ mongod --config /etc/mongod_rs0-0.conf
[root@mongodbsrv ~]$ mongod --config /etc/mongod_rs0-1.conf
[root@mongodbsrv ~]$ mongod --config /etc/mongod_rs0-2.conf
Check the processes:
[root@mongodbsrv ~]$ ps -ef |grep mongod
pcp 2878 1 0 20:36 ? 00:00:00 /usr/bin/pmie -b -m pmie_check -F -P -l /var/log/pcp/pmie/mongodbsrv/pmie.log -c config.default
root 14448 1 1 21:16 ? 00:00:05 mongod --config /etc/mongod_rs0-0.conf
root 14632 1 4 21:22 ? 00:00:00 mongod --config /etc/mongod_rs0-1.conf
root 14701 1 6 21:22 ? 00:00:00 mongod --config /etc/mongod_rs0-2.conf
root 14773 8796 0 21:22 pts/0 00:00:00 grep --color=auto mongod
We can start the Replicaset configuration:
[root@mongodbsrv ~]$ mongosh --port 27017
Current Mongosh Log ID: 69c0880b4abd0c057959b253
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.8.1
Using MongoDB: 8.0.20
Using Mongosh: 2.8.1
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2026-03-22T21:16:49.489-03:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2026-03-22T21:16:49.489-03:00: You are running this process as the root user, which is not recommended
2026-03-22T21:16:49.489-03:00: Soft rlimits for open file descriptors too low
2026-03-22T21:16:49.489-03:00: For customers running the current memory allocator, we suggest changing the contents of the following sysfsFile
2026-03-22T21:16:49.489-03:00: We suggest setting the contents of sysfsFile to 0.
2026-03-22T21:16:49.489-03:00: Your system has glibc support for rseq built in, which is not yet supported by tcmalloc-google and has critical performance implications. Please set the environment variable GLIBC_TUNABLES=glibc.pthread.rseq=0
2026-03-22T21:16:49.490-03:00: We suggest setting swappiness to 0 or 1, as swapping can cause performance problems.
------
Enterprise test> rsconf = {
| _id: "rs0",
| members: [
| { _id: 0, host: "mongodbsrv:27017" },
| { _id: 1, host: "mongodbsrv:27018" },
| { _id: 2, host: "mongodbsrv:27019" }
| ]
| }
{
_id: 'rs0',
members: [
{ _id: 0, host: 'mongodbsrv:27017' },
{ _id: 1, host: 'mongodbsrv:27018' },
{ _id: 2, host: 'mongodbsrv:27019' }
]
}
Enterprise test> rs.initiate( rsconf )
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1774226586, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1774226586, i: 1 })
}
Enterprise rs0 [direct: secondary] test> rs.conf()
{
_id: 'rs0',
version: 1,
term: 1,
members: [
{
_id: 0,
host: 'mongodbsrv:27017',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long('0'),
votes: 1
},
{
_id: 1,
host: 'mongodbsrv:27018',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long('0'),
votes: 1
},
{
_id: 2,
host: 'mongodbsrv:27019',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long('0'),
votes: 1
}
],
protocolVersion: Long('1'),
writeConcernMajorityJournalDefault: true,
settings: {
chainingAllowed: true,
heartbeatIntervalMillis: 2000,
heartbeatTimeoutSecs: 10,
electionTimeoutMillis: 10000,
catchUpTimeoutMillis: -1,
catchUpTakeoverDelayMillis: 30000,
getLastErrorModes: {},
getLastErrorDefaults: { w: 1, wtimeout: 0 },
replicaSetId: ObjectId('69c08c9a3a7c8207c57cdb2a')
}
}
To manage the stop and start service via systemd, we will stop the Mongodb instance and create services files:
[root@mongodbsrv ~]$ kill -9 14448 14632 14701
[root@mongodbsrv ~]$ ps -ef |grep mongod
pcp 2878 1 0 20:36 ? 00:00:00 /usr/bin/pmie -b -m pmie_check -F -P -l /var/log/pcp/pmie/mongodbsrv/pmie.log -c config.default
root 15843 8796 0 21:42 pts/0 00:00:00 grep --color=auto mongod
[root@mongodbsrv ~]$ cd /etc/systemd/system/
[root@mongodbsrv system]$ vim mongod_rs0-0.service
[Unit]
Description=Mongodb Service 0
After=network.target
[Service]
Type=forking
ExecStart=/usr/bin/mongod --config /etc/mongod_rs0-0.conf
[Install]
WantedBy=multi-user.target
Lets create another 2 and modify each one the instances:
[root@mongodbsrv system]$ cp mongod_rs0-0.service mongod_rs0-1.service
[root@mongodbsrv system]$ cp mongod_rs0-0.service mongod_rs0-2.service
[root@mongodbsrv system]$ vim mongod_rs0-1.service
[Unit]
Description=Mongodb Service 1
After=network.target
[Service]
Type=forking
ExecStart=/usr/bin/mongod --config /etc/mongod_rs0-1.conf
[Install]
WantedBy=multi-user.target
[root@mongodbsrv system]$ vim mongod_rs0-2.service
[Unit]
Description=Mongodb Service 2
After=network.target
[Service]
Type=forking
ExecStart=/usr/bin/mongod --config /etc/mongod_rs0-2.conf
[Install]
WantedBy=multi-user.target
[root@mongodbsrv system]$
We need to reload the daemons and enable MongoDB services on systemd:
[root@mongodbsrv system]$ systemctl daemon-reload
[root@mongodbsrv system]$ systemctl enable mongod_rs0-0.service
Created symlink /etc/systemd/system/multi-user.target.wants/mongod_rs0-0.service → /etc/systemd/system/mongod_rs0-0.service.
[root@mongodbsrv system]$ systemctl enable mongod_rs0-1.service
Created symlink /etc/systemd/system/multi-user.target.wants/mongod_rs0-1.service → /etc/systemd/system/mongod_rs0-1.service.
[root@mongodbsrv system]$ systemctl enable mongod_rs0-2.service
Created symlink /etc/systemd/system/multi-user.target.wants/mongod_rs0-2.service → /etc/systemd/system/mongod_rs0-2.service.
Now, lets start the services:
[root@mongodbsrv system]$ systemctl start mongod_rs0-0.service
[root@mongodbsrv system]$ systemctl start mongod_rs0-1.service
[root@mongodbsrv system]$ systemctl start mongod_rs0-2.service
[root@mongodbsrv system]$ systemctl status mongod_rs0-0.service
● mongod_rs0-0.service - Mongodb Service 0
Loaded: loaded (/etc/systemd/system/mongod_rs0-0.service; enabled; preset: disabled)
Active: active (running) since Sun 2026-03-22 23:05:37 -03; 28s ago
Process: 28722 ExecStart=/usr/bin/mongod --config /etc/mongod_rs0-0.conf (code=exited, status=0/SUCCESS)
Main PID: 28724 (mongod)
Tasks: 77 (limit: 22648)
Memory: 215.7M (peak: 216.6M)
CPU: 1.061s
CGroup: /system.slice/mongod_rs0-0.service
└─28724 /usr/bin/mongod --config /etc/mongod_rs0-0.conf
Mar 22 23:05:36 mongodbsrv systemd[1]: Starting Mongodb Service 0...
Mar 22 23:05:36 mongodbsrv mongod[28722]: about to fork child process, waiting until server is ready for connections.
Mar 22 23:05:36 mongodbsrv mongod[28724]: forked process: 28724
Mar 22 23:05:37 mongodbsrv mongod[28722]: child process started successfully, parent exiting
Mar 22 23:05:37 mongodbsrv systemd[1]: Started Mongodb Service 0.
[root@mongodbsrv system]$ systemctl status mongod_rs0-1.service
● mongod_rs0-1.service - Mongodb Service 1
Loaded: loaded (/etc/systemd/system/mongod_rs0-1.service; enabled; preset: disabled)
Active: active (running) since Sun 2026-03-22 23:07:15 -03; 10s ago
Process: 28953 ExecStart=/usr/bin/mongod --config /etc/mongod_rs0-1.conf (code=exited, status=0/SUCCESS)
Main PID: 28955 (mongod)
Tasks: 82 (limit: 22648)
Memory: 215.9M (peak: 216.7M)
CPU: 766ms
CGroup: /system.slice/mongod_rs0-1.service
└─28955 /usr/bin/mongod --config /etc/mongod_rs0-1.conf
Mar 22 23:07:14 mongodbsrv systemd[1]: Starting Mongodb Service 1...
Mar 22 23:07:14 mongodbsrv mongod[28953]: about to fork child process, waiting until server is ready for connections.
Mar 22 23:07:14 mongodbsrv mongod[28955]: forked process: 28955
Mar 22 23:07:15 mongodbsrv mongod[28953]: child process started successfully, parent exiting
Mar 22 23:07:15 mongodbsrv systemd[1]: Started Mongodb Service 1.
[root@mongodbsrv system]$ systemctl status mongod_rs0-2.service
● mongod_rs0-2.service - Mongodb Service 2
Loaded: loaded (/etc/systemd/system/mongod_rs0-2.service; enabled; preset: disabled)
Active: active (running) since Sun 2026-03-22 23:07:18 -03; 11s ago
Process: 29059 ExecStart=/usr/bin/mongod --config /etc/mongod_rs0-2.conf (code=exited, status=0/SUCCESS)
Main PID: 29062 (mongod)
Tasks: 96 (limit: 22648)
Memory: 243.3M (peak: 244.0M)
CPU: 806ms
CGroup: /system.slice/mongod_rs0-2.service
└─29062 /usr/bin/mongod --config /etc/mongod_rs0-2.conf
Mar 22 23:07:17 mongodbsrv systemd[1]: Starting Mongodb Service 2...
Mar 22 23:07:17 mongodbsrv mongod[29059]: about to fork child process, waiting until server is ready for connections.
Mar 22 23:07:17 mongodbsrv mongod[29062]: forked process: 29062
Mar 22 23:07:18 mongodbsrv mongod[29059]: child process started successfully, parent exiting
Mar 22 23:07:18 mongodbsrv systemd[1]: Started Mongodb Service 2.
Lets check the cluster status:
[root@mongodbsrv system]$ mongosh --port 27017
Current Mongosh Log ID: 69c0a0dc73310fd57559b253
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.8.1
Using MongoDB: 8.0.20
Using Mongosh: 2.8.1
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
------
The server generated these startup warnings when booting
2026-03-22T23:05:37.241-03:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2026-03-22T23:05:37.241-03:00: You are running this process as the root user, which is not recommended
2026-03-22T23:05:37.241-03:00: Soft rlimits for open file descriptors too low
2026-03-22T23:05:37.241-03:00: For customers running the current memory allocator, we suggest changing the contents of the following sysfsFile
2026-03-22T23:05:37.241-03:00: We suggest setting the contents of sysfsFile to 0.
2026-03-22T23:05:37.241-03:00: Your system has glibc support for rseq built in, which is not yet supported by tcmalloc-google and has critical performance implications. Please set the environment variable GLIBC_TUNABLES=glibc.pthread.rseq=0
2026-03-22T23:05:37.241-03:00: We suggest setting swappiness to 0 or 1, as swapping can cause performance problems.
------
Enterprise rs0 [direct: primary] test> rs.status
[Function: status] AsyncFunction {
apiVersions: [ 0, 0 ],
returnsPromise: true,
serverVersions: [ '0.0.0', '999.999.999' ],
topologies: [ 'ReplSet', 'Standalone', 'Sharded', 'LoadBalanced' ],
returnType: { type: 'unknown', attributes: {} },
deprecated: false,
platforms: [ 'Compass', 'Browser', 'CLI' ],
isDirectShellCommand: false,
acceptsRawInput: false,
shellCommandCompleter: undefined,
newShellCommandCompleter: undefined,
help: [Function (anonymous)] Help
}
Enterprise rs0 [direct: primary] test> rs.status()
{
set: 'rs0',
date: ISODate('2026-03-23T02:09:49.581Z'),
myState: 1,
term: Long('4'),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long('2000'),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 3,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
lastCommittedWallTime: ISODate('2026-03-23T02:09:45.984Z'),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
appliedOpTime: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
durableOpTime: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
writtenOpTime: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
lastAppliedWallTime: ISODate('2026-03-23T02:09:45.984Z'),
lastDurableWallTime: ISODate('2026-03-23T02:09:45.984Z'),
lastWrittenWallTime: ISODate('2026-03-23T02:09:45.984Z')
},
lastStableRecoveryTimestamp: Timestamp({ t: 1774231775, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate('2026-03-23T02:07:25.957Z'),
electionTerm: Long('4'),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long('-1') },
lastSeenWrittenOpTimeAtElection: { ts: Timestamp({ t: 1774230581, i: 1 }), t: Long('3') },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1774230581, i: 1 }), t: Long('3') },
numVotesNeeded: 2,
priorityAtElection: 1,
electionTimeoutMillis: Long('10000'),
numCatchUpOps: Long('0'),
newTermStartDate: ISODate('2026-03-23T02:07:25.968Z'),
wMajorityWriteAvailabilityDate: ISODate('2026-03-23T02:07:25.973Z')
},
members: [
{
_id: 0,
name: 'mongodbsrv:27017',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 253,
optime: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
optimeDate: ISODate('2026-03-23T02:09:45.000Z'),
optimeWritten: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
optimeWrittenDate: ISODate('2026-03-23T02:09:45.000Z'),
lastAppliedWallTime: ISODate('2026-03-23T02:09:45.984Z'),
lastDurableWallTime: ISODate('2026-03-23T02:09:45.984Z'),
lastWrittenWallTime: ISODate('2026-03-23T02:09:45.984Z'),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1774231645, i: 1 }),
electionDate: ISODate('2026-03-23T02:07:25.000Z'),
configVersion: 1,
configTerm: 4,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: 'mongodbsrv:27018',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 154,
optime: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
optimeDurable: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
optimeWritten: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
optimeDate: ISODate('2026-03-23T02:09:45.000Z'),
optimeDurableDate: ISODate('2026-03-23T02:09:45.000Z'),
optimeWrittenDate: ISODate('2026-03-23T02:09:45.000Z'),
lastAppliedWallTime: ISODate('2026-03-23T02:09:45.984Z'),
lastDurableWallTime: ISODate('2026-03-23T02:09:45.984Z'),
lastWrittenWallTime: ISODate('2026-03-23T02:09:45.984Z'),
lastHeartbeat: ISODate('2026-03-23T02:09:48.024Z'),
lastHeartbeatRecv: ISODate('2026-03-23T02:09:48.562Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: 'mongodbsrv:27017',
syncSourceId: 0,
infoMessage: '',
configVersion: 1,
configTerm: 4
},
{
_id: 2,
name: 'mongodbsrv:27019',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 151,
optime: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
optimeDurable: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
optimeWritten: { ts: Timestamp({ t: 1774231785, i: 1 }), t: Long('4') },
optimeDate: ISODate('2026-03-23T02:09:45.000Z'),
optimeDurableDate: ISODate('2026-03-23T02:09:45.000Z'),
optimeWrittenDate: ISODate('2026-03-23T02:09:45.000Z'),
lastAppliedWallTime: ISODate('2026-03-23T02:09:45.984Z'),
lastDurableWallTime: ISODate('2026-03-23T02:09:45.984Z'),
lastWrittenWallTime: ISODate('2026-03-23T02:09:45.984Z'),
lastHeartbeat: ISODate('2026-03-23T02:09:48.024Z'),
lastHeartbeatRecv: ISODate('2026-03-23T02:09:48.562Z'),
pingMs: Long('0'),
lastHeartbeatMessage: '',
syncSourceHost: 'mongodbsrv:27017',
syncSourceId: 0,
infoMessage: '',
configVersion: 1,
configTerm: 4
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1774231785, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1774231785, i: 1 })
}
That’s it.