﻿---
path: faq/mongodb-replica-set.mdx
title: Configuring a MongoDB replica set
slug: mongodb-replica-set
description: >-
  Manual MongoDB replica set configuration for Passwork: mongod.conf settings,
  rs0 initialization, adding and removing nodes, and configuring the Passwork
  connection.
keywords:
  - Passwork
  - MongoDB
  - Replica Set
  - replication
  - fault tolerance
  - mongod.conf
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

:::info
Before configuring a MongoDB replica set, we recommend reading the [**Fault tolerance**](/fault-tolerance/intro) 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](/manual-installation/linux/intro).
:::

## 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](/faq/docker/fault-tolerance/migration-to-replica-set) or [Installation with a pre-configured replica set](/faq/docker/fault-tolerance/replica-set-install).
:::

## 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:

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

```bash
var cmdLineOpts = db.serverCmdLineOpts();
print("config: " + cmdLineOpts.parsed.config);
```

  </TabItem>
</Tabs>

### 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`):

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

```yaml
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

replication:
  replSetName: rs0
```

  </TabItem>
</Tabs>

:::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:

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="Linux" label="Linux">

```bash
systemctl restart mongod.service
```

  </TabItem>
  <TabItem className="tab-item-container" value="Windows" label="Windows Server">

```bash
net stop MongoDB
net start MongoDB
```

  </TabItem>
</Tabs>

## Initializing the replica set and adding/removing members

### Initializing the replica set

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

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

```bash
mongosh
```

  </TabItem>
</Tabs>

Run the initialization and add the replica set members:

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

```bash
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" }
   ]
})
```

  </TabItem>
</Tabs>

Check the status:

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

```bash
rs.status()
```

  </TabItem>
</Tabs>

### Adding a node to the replica set

Connect to the replica set:

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

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

  </TabItem>
</Tabs>

Add the node:

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

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

  </TabItem>
</Tabs>

Check the status:

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

```bash
rs.status()
```

  </TabItem>
</Tabs>

### Removing a node from the replica set

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

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

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

  </TabItem>
</Tabs>

Remove the node:

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

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

  </TabItem>
</Tabs>

Check the status:

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

```bash
rs.status()
```

  </TabItem>
</Tabs>

## Configuring Passwork to connect to the replica set

Edit the Passwork configuration file — `config.env`:

<Tabs className="tabs-container">
  <TabItem className="tab-item-container" value="shell" label="shell">

```bash
MONGODB_URL=mongodb://example.mongo.01:27017,example.mongo.02:27017,example.mongo.03:27017/?replicaSet=rs0
MONGODB_DB=pw
MONGODB_USERNAME=
MONGODB_PASSWORD=
```

  </TabItem>
</Tabs>

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.
