Configuring a MongoDB replica set
Before configuring a MongoDB replica set, we recommend reading the Fault tolerance section. It describes the principles of the fault-tolerant Passwork architecture, the license requirements, and the general component layout.
Configuring MongoDB replication requires at least three servers. MongoDB must be installed on each server as described in the documentation.
Overview
The example uses three MongoDB servers:
- example.mongo.01
- example.mongo.02
- example.mongo.03
If DNS is not configured, add the records to hosts on every node (the path depends on the operating system):
- Example records:
- 172.16.X.X example.mongo.01
- 172.16.X.X example.mongo.02
- 172.16.X.X example.mongo.03
- Linux —
/etc/hosts - Windows Server —
C:\Windows\System32\drivers\etc\hosts
If you are configuring replication in a Docker build, use the ready-made examples: Migrating from standalone to a replica set or Installation with a pre-configured replica set.
Configuration file location and changing MongoDB parameters
Configuration file location
Depending on the operating system:
- Linux —
/etc/mongod.conf - Windows Server —
C:\Program Files\MongoDB\Server\7.0\bin\mongod.cfg
You can check the path from the MongoDB shell:
- shell
var cmdLineOpts = db.serverCmdLineOpts();
print("config: " + cmdLineOpts.parsed.config);
Changing the configuration file parameters
Open the configuration file and change or add the parameters (substitute your own host names instead of example.mongo.01, example.mongo.02, example.mongo.03):
- shell
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: rs0
Indentation in the configuration file matters for the syntax.
- bindIp — the IP addresses on which MongoDB accepts connections. It lets you restrict access to the database.
- replication — the rs0 replica set name used for fault tolerance and data distribution.
After making the changes, restart the MongoDB service:
- Linux
- Windows Server
systemctl restart mongod.service
net stop MongoDB
net start MongoDB
Initializing the replica set and adding/removing members
Initializing the replica set
Connect to the MongoDB shell on the primary server (example.mongo.01):
- shell
mongosh
Run the initialization and add the replica set members:
- shell
rs.initiate( {
_id : "rs0",
members: [
{ _id: 0, host: "example.mongo.01:27017" },
{ _id: 1, host: "example.mongo.02:27017" },
{ _id: 2, host: "example.mongo.03:27017" }
]
})
Check the status:
- shell
rs.status()
Adding a node to the replica set
Connect to the replica set:
- shell
mongosh --host rs0/example.mongo.01:27017,example.mongo.02:27017,example.mongo.03:27017
Add the node:
- shell
rs.add("example.mongo.04:27017")
Check the status:
- shell
rs.status()
Removing a node from the replica set
Connect to the replica set (including the node being removed):
- shell
mongosh --host rs0/example.mongo.01:27017,example.mongo.02:27017,example.mongo.03:27017,example.mongo.04:27017
Remove the node:
- shell
rs.remove("example.mongo.04:27017")
Check the status:
- shell
rs.status()
Configuring Passwork to connect to the replica set
Edit the Passwork configuration file — config.env:
- shell
MONGODB_URL=mongodb://example.mongo.01:27017,example.mongo.02:27017,example.mongo.03:27017/?replicaSet=rs0
MONGODB_DB=pw
MONGODB_USERNAME=
MONGODB_PASSWORD=
Configuration file location:
- Linux —
/var/www/init/config.env - Windows Server —
C:\inetpub\wwwroot\passwork\init\config.env
Save the changes and reload the Passwork web interface page to verify the connection to the replica set.