﻿# MongoDB

> [HTML Version](mongodb.html)

This article describes how to:

- Install MongoDB 8.0 for Ubuntu Linux 24.04. You can also refer to the brief guide in the [official MongoDB documentation](https://www.mongodb.com/docs/manual/installation/).

- [Prepare a MongoDB database for restoration](#prepare-to-restore).

Before installing the DBMS:

- Check the supported versions of MongoDB for correct system operation in the [System requirements for BRIX On-Premises](elma365-enterprise-on-premises.md#mongo) article.

- Evaluate the planned workload. For large data volumes, it is recommended to install the DBMS on a separate virtual machine. This will prevent resource competition and ensure stable operation of the BRIX system. For more details on infrastructure organization options, refer to [Prepare BRIX On-Premises infrastructure](infrastructure-preparation.md). 

If you are installing MongoDB 5.0, please note that this version requires using an AVX set of instructions. Check their availability by running the following command:

````
cat /proc/cpuinfo | grep avx

начало внимание 

````
In this example, the database name is **brix365**, the user is **brix365**, and the password is **SecretPassword**. When configuring MongoDB, set these data according to the security policy adopted in your organization.

````
конец внимание

````
Installation consists of six steps:

1. [Install MongoDB](#installation).

2. [Configure MongoDB](#add_db).

3. [Configure connection to MongoDB](#connection-setup).

4. [Initialize the replica](#replica-initial).

5. [Connect to MongoDB](#connection).

## Step 1: Install MongoDB

To install MongoDB, add the official repository:

````
sudo apt-get install gnupg  
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor  
echo "deb \[ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg \] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list  
sudo apt-get update

````
Then install MongoDB:

````
sudo apt install mongodb-org

````
Run MongoDB:

````
sudo systemctl enable --now mongod

## ````
Step 2: Configure MongoDB

````
Начало примечание

````
**Note**

The password can contain the following characters:

- Uppercase Latin letters: A to Z

- Lowercase Latin letters: a to z

- Digits: 0 to 9

- Symbols: -\_

Reserved (invalid) characters: \! \* ' ( ) ; : @ \& = + \$ , / ? % # \[ \]

````
Конец примечание

1. ````
Access **mongosh** (Command Line Interface) and create a database:

````
mongosh

2. ````
Use the I database. If it doesn't exist, create it:

````
use BRIX

3. ````
Create a dedicated user, **brix365**, to work with the database, with the password **SecretPassword**. Username and password are provided as an example:

````
db.createUser(\{user:'brix365', pwd:'SecretPassword', roles:\[\{role:"readWrite", db:"brix365"\},\{"role":"root", "db":"admin"\}\]\})

4. ````
Make sure that the user is created:

````
show users

5. ````
To enable authentication, create a superuser:

````
use admin  
db.createUser(\{user:'superuser', pwd:'SecretPassword', roles: \["root"\]\})

6. ````
Make sure that the user is created:

````
show users

7. ````
Complete the configuration:

````
exit

8. ````
Create a file with a general authentication key, set access to the file:

````
openssl rand -base64 756 > /var/lib/mongodb/keyfile  
chmod 400 /var/lib/mongodb/keyfile  
chown mongodb:mongodb /var/lib/mongodb/keyfile

9. ````
Enable secure access to the MongoDB server.

To do that, edit the \[OBJECT\] file:

````
. . .  
setParameter:  
  enableLocalhostAuthBypass: false  
security:  
   authorization: "enabled"  
keyFile: /var/lib/mongodb/keyfile  
. . .

10. ````
Restart MongoDB:

````
sudo systemctl restart mongod

## ````
Step 3: Configure connection to MongoDB

Make changes to the configuration file \[OBJECT\]:

````
sudo nano /etc/mongod.conf

````
Configure the values for:

- \[OBJECT\] is a list of addresses from which connections on port 27017 can be accepted (in this case, it makes the MongoDB service available from all external addresses);

- \[OBJECT\] is the name of the replica set, by default it is **"rs0"**

````
. . .  
\# network interfaces  
net:  
  port: 27017  
  bindIp: 0.0.0.0  
. . .  
replication:  
  replSetName: "rs0"  
  enableMajorityReadConcern: true  
. . .

````
Configuring TLS/SSL in MongoDB

  
To enable TLS/SSL support in MongoDB, follow these steps:

1. Prepare a fullchain certificate. See the [Create a fullchain certificate](fullchain-sertificate.md) article for instructions on how to do this.

2. Copy the contents of the **your\_domain.key** private key into **mongodb.pem**:

````
cat your\_domain.key > mongodb.pem

3. ````
Copy the contents of the fullchain certificate into **mongodb.pem**.

````
cat fullchain\_your\_domain.pem >> mongodb.pem

4. ````
Add a configuration block for net on each node. In the **net.tls.certificateKeyFile** parameter, specify the path to the **PEM** file containing the fullchain certificates and associated private keys. Each node has unique domain names, and certificates must be issued for each node.

````
net:  
  tls:  
    mode: requireTLS  
    certificateKeyFile: /path/to/mongodb.pem

````
  
For more detailed information on configuring TLS/SSL in MongoDB, refer to the [official MongoDB documentation](https://www.mongodb.com/docs/v5.0/tutorial/configure-ssl/).

Restart MongoDB to apply the changes:

````
sudo systemctl restart mongod

## ````
Step 4: Initialize the replica

1. Open the **mongosh** console for configuration.

To connect to MongoDB:

````
sudo mongosh -u superuser admin

````
To connect to MongoDB with TLS/SSL enabled:

````
sudo mongosh -u superuser --tls --host mongodb-server1.your\_domain --tlsCAFile /etc/ssl/CA.pem

2. ````
Initialize the replica:

````
rs.initiate(\{ \_id: "rs0", members: \[\{ \_id: 0, host: "<mongodb-server-address>" \}\]\})

3. ````
Check the **replicaSet** configuration:

````
rs.conf()

4. ````
Check the state of MongoDB:

````
rs.status()

## ````
Step 5: Connect to MongoDB

Connection string for MongoDB:

````
mongodb://brix365:SecretPassword@<mongodb-server-address>:27017/brix365?ssl=false\&replicaSet=rs0\&readPreference=nearest

````
Connection string for MongoDB with TLS/SSL:

````
mongodb://brix365:SecretPassword@mongodb-server.your\_domain:27017/brix365?ssl=true\&replicaSet=rs0\&readPreference=nearest

## ````
Prepare a MongoDB database for restoration

If you need to restore a database from backup, first prepare the database for restoration.

For more details on backup, refer to the following articles:

- [External BRIX backup](dump_db.md).

- [Backup and recover databases](database-backup-and-recovery.md).

````
начало внимание 

````
Ensure you have a backup copy of the database you are deleting. Only after that, proceed with deleting the database.

````
Конец внимание

1. ````
Open the **mongosh** console for configuration:

- To connect to MongoDB:

````
sudo mongosh -u superuser

- ````
To connect to MongoDB with TLS/SSL enabled:

````
sudo mongosh -u superuser --tls --host mongodb-server1.your\_domain --tlsCAFile /etc/ssl/CA.pem

2. ````
Delete the **brix365** database:

````
use brix365  
db.dropDatabase()

3. ````
Create an empty **brix365** database to restore the database from backup. To do this, use the commands from [Step 2: Configure MongoDB](#create-db).

4. After that, depending on your BRIX On-Premises edition and backup method, perform data restoration:

- [Restore a backup copy using the utility](database-backup-and-recovery.md#recovery-database).

- [Restore a backup copy using WAL-G](backup-and-recovery-with-wal-g-and-rclone.md#walg-mongo).

- [Restore a backup copy when using external means](dump_db.md#restore).