# Installing MongoDB

The following steps outline how to install MongoDB using the [Optimal Flexible Architecture](/dmk-mongodb/installation-and-setup/optimal_flexible_architecture.md) (OFA) approach.

#### Installation requirements

{% code title="installation\_requirements.sh" overflow="wrap" lineNumbers="true" %}

```bash
dnf -y install wget unzip
```

{% endcode %}

#### Create the directory structure

Create all the mandatory directories for the installation as `root` user.

{% code title="directory\_creation.sh" overflow="wrap" lineNumbers="true" %}

```bash
export MONGO_BASE=/u01/app/mongodb
export MONGO_DATA_ROOT=/u02/mongodb/data
export MONGO_JOURNAL_ROOT=/u03/mongodb/journal # If you don't want to separate journal and data files, you can omit this variable.
export MONGO_LOG_ROOT=/u04/mongodb/log # Same thing
export MONGO_BACKUP_ROOT=/u90/mongodb/backup
mkdir -p $MONGO_BASE
mkdir -p $MONGO_DATA_ROOT
mkdir -p $MONGO_JOURNAL_ROOT
mkdir -p $MONGO_LOG_ROOT
mkdir -p $MONGO_BACKUP_ROOT
mkdir -p $MONGO_BASE/local
mkdir -p $MONGO_BASE/admin
mkdir -p $MONGO_BASE/etc
mkdir -p $MONGO_BASE/product
mkdir -p $MONGO_BASE/artifacts
```

{% endcode %}

#### Download MongoDB binaries

Depending on your requirements, visit the official MongoDB website to download the appropriate binaries. Select the desired edition (e.g., Community Edition), version, and platform. Be sure to choose the `.tgz` package and copy the corresponding download links. A `.tgz` installation is preferred to allow multiple versions of MongoDB on the same system.

For example, for MongoDB 8.0.16 Community Edition on RHEL 8.10, the download links are:

* [MongoDB Community Server 8.0.16](https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel8-8.0.16.tgz)
* [MongoDB Shell 2.5.10](https://downloads.mongodb.com/compass/mongosh-2.5.10-linux-x64.tgz)
* [MongoDB CLI Database Tools 100.13.0](https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel88-x86_64-100.13.0.tgz)

The MongoDB Shell and CLI Database Tools versions are independent of the MongoDB Server version.

On the server, download and extract the binaries as `root`:

{% code title="mongo\_bin\_download.sh" overflow="wrap" lineNumbers="true" %}

```bash
MONGO_VERSION=8.0.16
cd $MONGO_BASE/artifacts
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel8-${MONGO_VERSION}.tgz
wget https://downloads.mongodb.com/compass/mongosh-2.5.10-linux-x64.tgz
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel88-x86_64-100.13.0.tgz
mkdir -p $MONGO_BASE/product/$MONGO_VERSION/bin
for file in $MONGO_BASE/artifacts/*.tgz; do
    tar -xvzf "$file" -C $MONGO_BASE/product/$MONGO_VERSION
done

find $MONGO_BASE/product/$MONGO_VERSION/ -executable -type f -exec ln -s {} $MONGO_BASE/product/$MONGO_VERSION/bin/ ';'
ls -l $MONGO_BASE/product/$MONGO_VERSION/bin/

# Output of ls -l
lrwxrwxrwx. 1 root root 90 Dec  4 10:01 bsondump -> /u01/app/mongodb/product/8.0.16/mongodb-database-tools-rhel88-x86_64-100.13.0/bin/bsondump
lrwxrwxrwx. 1 root root 86 Dec  4 10:01 install_compass -> /u01/app/mongodb/product/8.0.16/mongodb-linux-x86_64-rhel88-8.0.16/bin/install_compass
lrwxrwxrwx. 1 root root 77 Dec  4 10:01 mongod -> /u01/app/mongodb/product/8.0.16/mongodb-linux-x86_64-rhel88-8.0.16/bin/mongod
lrwxrwxrwx. 1 root root 91 Dec  4 10:01 mongodump -> /u01/app/mongodb/product/8.0.16/mongodb-database-tools-rhel88-x86_64-100.13.0/bin/mongodump
lrwxrwxrwx. 1 root root 93 Dec  4 10:01 mongoexport -> /u01/app/mongodb/product/8.0.16/mongodb-database-tools-rhel88-x86_64-100.13.0/bin/mongoexport
lrwxrwxrwx. 1 root root 92 Dec  4 10:01 mongofiles -> /u01/app/mongodb/product/8.0.16/mongodb-database-tools-rhel88-x86_64-100.13.0/bin/mongofiles
lrwxrwxrwx. 1 root root 93 Dec  4 10:01 mongoimport -> /u01/app/mongodb/product/8.0.16/mongodb-database-tools-rhel88-x86_64-100.13.0/bin/mongoimport
lrwxrwxrwx. 1 root root 94 Dec  4 10:01 mongorestore -> /u01/app/mongodb/product/8.0.16/mongodb-database-tools-rhel88-x86_64-100.13.0/bin/mongorestore
lrwxrwxrwx. 1 root root 77 Dec  4 10:01 mongos -> /u01/app/mongodb/product/8.0.16/mongodb-linux-x86_64-rhel88-8.0.16/bin/mongos
lrwxrwxrwx. 1 root root 68 Dec  4 10:01 mongosh -> /u01/app/mongodb/product/8.0.16/mongosh-2.5.10-linux-x64/bin/mongosh
lrwxrwxrwx. 1 root root 80 Dec  4 10:01 mongosh_crypt_v1.so -> /u01/app/mongodb/product/8.0.16/mongosh-2.5.10-linux-x64/bin/mongosh_crypt_v1.so
lrwxrwxrwx. 1 root root 91 Dec  4 10:01 mongostat -> /u01/app/mongodb/product/8.0.16/mongodb-database-tools-rhel88-x86_64-100.13.0/bin/mongostat
lrwxrwxrwx. 1 root root 90 Dec  4 10:01 mongotop -> /u01/app/mongodb/product/8.0.16/mongodb-database-tools-rhel88-x86_64-100.13.0/bin/mongotop
```

{% endcode %}

#### MongoDB User Creation

Create a `mongodb` group and user, and grant the user ownership of the directories you just created.

{% code title="user\_creation.sh" overflow="wrap" lineNumbers="true" %}

```bash
groupadd mongodb -g 1500
useradd --uid 1500 -d /home/mongodb -g mongodb -m -s /bin/bash mongodb
# Changing ownership of all directories. If yours are not called /u0*, change these commands accordingly.
chown -R mongodb: /u01
chown -R mongodb: /u02
chown -R mongodb: /u03
chown -R mongodb: /u04
chown -R mongodb: /u90
```

{% endcode %}

{% hint style="success" %}
You have successfully completed the MongoDB installation process.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dbi-services.gitbook.io/dmk-mongodb/installation-and-setup/installing_mongodb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
