Installing Instances for a Replica Set
How to install and configure MongoDB instances for a replica set
This document provides detailed instructions to install and configure MongoDB instances for a three-node replica set using DMK, ensuring a reliable and consistent setup.
Create a MongoDB instance for a replica set
On each node, install a MongoDB instance as described in Creating a MongoDB Instance.
DMK provides templates for replica set installation. For instance:
mongo_cfg:
instance_name: mdbrs01
home_path: /u01/app/mongodb/product/8.0.11
data_path: ${MONGO_DATA}
bindIp: 0.0.0.0
port: 27017
template: ${DMK_HOME}/templates/dbcreate/mongo_conf_replicaset.yaml
replicaset_name: rs01
This template uses a different MongoDB configuration file:
# Generated by MongoDB - DMK dbi services
processManagement:
fork: true
pidFilePath: ${admin_path}/pid/mongod.pid
net:
bindIp: ${bindIp}
port: ${port}
storage:
dbPath: ${data_path}
engine: wiredTiger
systemLog:
destination: file
path: ${log_path}/${instance_name}.log
logAppend: true
replication:
replSetName: ${replicaset_name}
Compared to a standard configuration, this file includes the replication.replSetName
parameter.
Here is the output of the MongoDB instance creation:
mongodb@vm01:/home/mongodb/ [DUMMY] dmk_dbcreate.py -c ${DMK_HOME}/templates/dbcreate/dbcreate_template_replicaset.yaml
2025-07-01 09:15:46 | INFO | Creating new MongoDB instance using configuration file: '/u01/app/mongodb/local/dmk/templates/dbcreate/dbcreate_template_replicaset.yaml'
2025-07-01 09:15:46 | WARNING | bindIp is set to 0.0.0.0, which might cause issues if TLS is used. It might also represent a security risk.
2025-07-01 09:15:46 | INFO | Creating directory layout for mdbrs01 ...
2025-07-01 09:15:46 | INFO | Directory '/u02/mongodb/data/mdbrs01' created.
2025-07-01 09:15:46 | INFO | Directory '/u01/app/mongodb/admin/mdbrs01' created.
2025-07-01 09:15:46 | INFO | Directory '/u04/mongodb/logs/mdbrs01' created.
2025-07-01 09:15:46 | INFO | Directory '/u90/mongodb/backup/mdbrs01' created.
2025-07-01 09:15:46 | INFO | Directory '/u01/app/mongodb/admin/mdbrs01/pid' created.
2025-07-01 09:15:46 | INFO | Directory '/u01/app/mongodb/admin/mdbrs01/etc' created.
2025-07-01 09:15:46 | INFO | Directory '/u01/app/mongodb/admin/mdbrs01/dump' created.
2025-07-01 09:15:46 | INFO | Directory '/u01/app/mongodb/admin/mdbrs01/secret' created.
2025-07-01 09:15:46 | INFO | Directory '/u03/mongodb/journal/mdbrs01' created.
2025-07-01 09:15:46 | INFO | Created symlink: /u01/app/mongodb/admin/mdbrs01/backup -> /u90/mongodb/backup/mdbrs01
2025-07-01 09:15:46 | INFO | Created symlink: /u01/app/mongodb/admin/mdbrs01/logs -> /u04/mongodb/logs/mdbrs01
2025-07-01 09:15:46 | INFO | Created symlink: /u02/mongodb/data/mdbrs01/journal -> /u03/mongodb/journal/mdbrs01
2025-07-01 09:15:46 | INFO | Creating MongoDB configuration file from template: /u01/app/mongodb/local/dmk/templates/dbcreate/mongo_conf_replicaset.yaml
2025-07-01 09:15:46 | INFO | Created MongoDB configuration file at '/u01/app/mongodb/admin/mdbrs01/etc/mdbrs01.conf'
2025-07-01 09:15:46 | INFO | Creating systemd file from template at '/u01/app/mongodb/local/dmk/templates/systemd/mongod.service.template'
2025-07-01 09:15:46 | WARNING | Created service file for systemd at '/u01/app/mongodb/admin/mdbrs01/etc/mongod_mdbrs01.service'.
--------------------------------------------------------------------------------
Copy the service file to /etc/systemd/system as root user :
cp -p /u01/app/mongodb/admin/mdbrs01/etc/mongod_mdbrs01.service /etc/systemd/system/
systemctl daemon-reload
systemctl start mongod_mdbrs01.service
systemctl status mongod_mdbrs01.service
--------------------------------------------------------------------------------
2025-07-01 09:15:46 | WARNING | If this is the first MongoDB installation, and no log rotation is set, run as root:
--------------------------------------------------------------------------------
cp /u01/app/mongodb/local/dmk/templates/etc/mongo_logrotate.template /etc/logrotate.d/mongodb
sed -i 's|MONGO_LOG_ROOT|/u04/mongodb/logs|g' /etc/logrotate.d/mongodb
chown root:root /etc/logrotate.d/mongodb
chmod 644 /etc/logrotate.d/mongodb
--------------------------------------------------------------------------------
2025-07-01 09:15:46 | INFO | Updating '/u01/app/mongodb/etc/mongodb.lst' file, adding mdbrs01
2025-07-01 09:15:46 | WARNING | Source DMK to load environment for the new instance mdbrs01 by executing the command 'dmk mdbrs01'.
2025-07-01 09:15:46 | INFO | MongoDB instance mdbrs01 created.
Start all instances and test the connections
Once all three instances are installed, load the environment and start them with <instance_name>; mgstart
.
mdbrs01; mgstart # On vm01
mdbrs02; mgstart # On vm02
mdbrs03; mgstart # On vm03
You can now test the connections. From all three nodes, run the following commands:
mongosh "mongodb://vm01:27017"
mongosh "mongodb://vm02:27017"
mongosh "mongodb://vm03:27017"
Initiate the replica set
To initiate the replica set, connect on the primary node using the ms
alias, and run the following rs.initiate()
command. If needed, change the replica set name _id
, the DNS/IP of your servers, and the ports.
rs.initiate({
_id: "rs01",
members: [
{ _id: 0, host: "vm01:27017" },
{ _id: 1, host: "vm02:27017" },
{ _id: 2, host: "vm03:27017" }
]
})
test> rs.initiate({
... _id: "rs01",
... members: [
... { _id: 0, host: "vm01:27017" },
... { _id: 1, host: "vm02:27017" },
... { _id: 2, host: "vm03:27017" }
... ]
... })
...
{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1751438185, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1751438185, i: 1 })
}
You have successfully configured a MongoDB three-node replica set with DMK.
If you reload the environment with mdbrs01
, you will now have new information on your instance:
REPLICA SET STATUS
: eitherPRIMARY
orSECONDARY
.REPLICA SET PRIMARY
: current primary node.REPLICA SET MEMBERS
: members of the replica set.
mongodb@vm01:/home/mongodb/ [mdbrs01] mdbrs01
---------- dbi services ----------
INSTANCE : mdbrs01
STATUS : STARTED
VERSION : 8.0.11
BIND : 0.0.0.0
PORT : 27101
REPLICA SET STATUS : PRIMARY
REPLICA SET PRIMARY : 152.67.70.125:27101
REPLICA SET MEMBERS : 152.67.70.125:27101, 152.67.76.17:27102, 152.67.78.131:27103
CONFIGURATION FILE : /u01/app/mongodb/admin/mdbrs01/etc/mdbrs01.conf
DATA PATH : /u02/mongodb/data/mdbrs01
LOG FILE : /u04/mongodb/logs/mdbrs01/mdbrs01.log
------------------------------------
Last updated