Skip to main content
Version: 7.0

Configuring a MongoDB replica set

info

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.

danger

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
info

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 ServerC:\Program Files\MongoDB\Server\7.0\bin\mongod.cfg

You can check the path from the MongoDB 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):

# network interfaces
net:
port: 27017
bindIp: 0.0.0.0

replication:
replSetName: rs0
warning

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:

systemctl restart mongod.service

Initializing the replica set and adding/removing members

Initializing the replica set

Connect to the MongoDB shell on the primary server (example.mongo.01):

mongosh

Run the initialization and add the replica set members:

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:

rs.status()

Adding a node to the replica set

Connect to the replica set:

mongosh --host rs0/example.mongo.01:27017,example.mongo.02:27017,example.mongo.03:27017

Add the node:

rs.add("example.mongo.04:27017")

Check the status:

rs.status()

Removing a node from the replica set

Connect to the replica set (including the node being removed):

mongosh --host rs0/example.mongo.01:27017,example.mongo.02:27017,example.mongo.03:27017,example.mongo.04:27017

Remove the node:

rs.remove("example.mongo.04:27017")

Check the status:

rs.status()

Configuring Passwork to connect to the replica set

Edit the Passwork configuration file — config.env:

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 ServerC:\inetpub\wwwroot\passwork\init\config.env

Save the changes and reload the Passwork web interface page to verify the connection to the replica set.