> For the complete documentation index, see [llms.txt](https://docs.enginsight.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.enginsight.com/docs/manual/english/installation-and-configuration/installation-and-configuration/enginsight-platform/manual-installation/database-server-database.md). Agents can also search and read this documentation through its [MCP server](https://docs.enginsight.com/docs/~gitbook/mcp).

# Database Server (Database)

Follow the steps below to install and set up the database.

***

## Installing the Database

{% stepper %}
{% step %}

### Complete the prerequisite steps

Make sure that you have completed the [prerequisite steps](https://docs.enginsight.com/docs/manual/english/installation-and-configuration/installation-and-configuration/enginsight-platform/manual-installation.md#preparation) correctly before starting the installation.
{% endstep %}

{% step %}

### Install MongoDB

Install MongoDB on the prepared Database Server according to the vendor's instructions: [Install MongoDB Community Edition](https://www.mongodb.com/docs/manual/administration/install-community/?operating-system=linux\&linux-distribution=red-hat\&linux-package=default\&search-linux=with-search-linux).

{% hint style="danger" %}
**Please note:** Intel Advanced Vector Extensions (AVX) is required for the installation of MongoDB.
{% endhint %}
{% endstep %}
{% endstepper %}

***

## Configuring the Database

{% stepper %}
{% step %}

### Start MongoDB

Start MongoDB using the following command if it does not start automatically:

```
sudo systemctl enable mongod && sudo systemctl start mongod
```

Press **Enter** to confirm your entry.
{% endstep %}

{% step %}

### Open the MongoDB configuration file

Open the configuration file `/etc/mongod.conf` using the following command:

```
sudo nano /etc/mongod.conf
```

Press **Enter** to confirm your entry.
{% endstep %}

{% step %}

### Adjust the configuration file

Navigate to the `net` section and adjust it as follows to make MongoDB accessible from external systems:

```
net:
  port: 27017
  bindIp: 0.0.0.0
```

{% endstep %}

{% step %}

### Configure the replica set

Add the following replica set configuration to the configuration file so that the database can send information to the Enginsight API:

```
replication:
   replSetName: "rs0"
```

{% hint style="danger" %}
**Please note:** If the database is not running in replica mode, many Enginsight functions will not work correctly.
{% endhint %}
{% endstep %}

{% step %}

### Save the configuration file

Save the configuration file (**Ctrl** + **o**) and confirm the save process. Close the file (**Ctrl** + **x**).
{% endstep %}

{% step %}

### Restart MongoDB

Restart MongoDB using the following command:

```
sudo systemctl restart mongod
```

Press **Enter** to confirm your entry.
{% endstep %}

{% step %}

### Open the MongoDB shell

Open the MongoDB Command Line Interface (CLI) using the following command:

```
sudo mongosh
```

Press **Enter** to confirm your entry.
{% endstep %}

{% step %}

### Enable the replica set

Build the following command in a text editor of your choice and execute it to enable the replica set:

<pre><code>rs.initiate( {
   _id : "rs0",
   members: [
<strong>      { _id: 0, host: "&#x3C;DatabaseServerIPAddress>:27017" }
</strong>   ]
})
</code></pre>

{% hint style="info" %}
Remember to replace `<DatabaseServerIPAddress>` accordingly.
{% endhint %}
{% endstep %}

{% step %}

### Check the replica set status

Use the following command to check that the replica set is active and press **Enter** to confirm your entry:

```
rs.status()
```

If the configuration is correct, the following should be displayed:

```
rs0:PRIMARY>
```

{% endstep %}

{% step %}

### Close the MongoDB shell

Close the MongoDB CLI using the following command:

```
quit()
```

Press **Enter** to confirm your entry.
{% endstep %}

{% step %}

### Secure the Enginsight database

{% hint style="danger" %}
**Please note:** This step is important to ensure the security of the Enginsight platform. It ensures that only the Application Server can communicate with the database. Unauthorized access is prevented.
{% endhint %}

Secure the database using **iptables** rules. Enter the following commands and press **Enter** to confirm each entry:

<pre><code>sudo iptables -A INPUT -p tcp -m tcp --dport 27017 -s 127.0.0.1/8 -j ACCEPT
<strong>sudo iptables -A INPUT -p tcp -m tcp --dport 27017 -s &#x3C;ApplicationServerIPAddress> -j ACCEPT
</strong><strong>sudo iptables -A INPUT -p tcp -m tcp --dport 27017 -s &#x3C;DatabaseServerIPAddress> -j ACCEPT
</strong>sudo iptables -A INPUT -p tcp -m tcp --dport 27017 -j DROP
</code></pre>

{% hint style="info" %}
Remember to replace `<ApplicationServerIPAddress>` with the IP address of the Application Server that is reachable from the database, and `<DatabaseServerIPAddress>` with the IP address of the Database Server that is reachable from the application.
{% endhint %}
{% endstep %}

{% step %}

### Save the iptables rules

Use the following command to save the iptables rules and ensure that they are loaded automatically after each reboot:

```
sudo apt-get install -y iptables-persistent
```

{% endstep %}

{% step %}

### Set up log rotation

Now set up log rotation (`logrotate`) to prevent your log files from growing indefinitely and consuming the available disk space.

1. If it is not already installed, install the `logrotate` package using the following command:

```
sudo apt install logrotate
```

2. Set up log rotation using the following command:

```
sudo tee /etc/logrotate.d/mongodb << 'EOF'
/var/log/mongodb/*.log {
    daily
    rotate 30
    size 50M
    compress
    dateext
    missingok
    notifempty
    sharedscripts
    postrotate
        /bin/kill -SIGUSR1 `cat /var/lib/mongodb/mongod.lock 2> /dev/null` 2> /dev/null || true
    endscript
}
EOF 
```

{% endstep %}
{% endstepper %}

***

## Next Steps

Continue with the [Installation of the Enginsight platform](https://docs.enginsight.com/docs/manual/english/installation-and-configuration/installation-and-configuration/enginsight-platform/manual-installation/application-server-platform.md).

***

## Further Resources

**Knowledge Base**

* <mark style="background-color:pink;">​</mark>[How do I create a backup of my Enginsight instance?​](https://docs.enginsight.com/docs/knowledge-base/english/database/how-do-i-create-a-backup-of-my-enginsight-instance)
* [How do I create a backup of one or more organizations in Enginsight?​](https://docs.enginsight.com/docs/knowledge-base/english/database/how-do-i-create-a-backup-of-one-or-more-organizations-in-enginsight)
* ​[How do I set up a password for the Enginsight database?​](https://docs.enginsight.com/docs/knowledge-base/english/database/how-do-i-set-up-a-password-for-the-enginsight-database)
* [How do I update the Enginsight database from MongoDB 5.0 to MongoDB 8.0?​](https://docs.enginsight.com/docs/knowledge-base/english/database/how-do-i-update-the-enginsight-database-from-mongodb-5.0-to-mongodb-8.0)
* [What changes do I need to make if the IP address of my Enginsight database changes?](https://docs.enginsight.com/docs/knowledge-base/english/database/what-changes-do-i-need-to-make-if-the-ip-address-of-my-enginsight-database-changes)

***
