> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# MongoDB Installation on Red Hat

> You can perform MongoDB Replicaset 8.0 installation on servers with Red Hat Enterprise Linux or Rocky Linux operating system. It is recommended to use Red Hat 8.x or 9.x operating system. Operating system configurations, MongoDB application installation, replicaset configuration and high availability settings can be made during the installation process.

## Checks to be Made Before Starting Installation

<Warning>
  **Very Important**

  Before starting installations, make sure that the server's hostname is **not localhost.localdomain and each one is unique** (with hostname command). If it is, be sure to change it before starting operations.

  ```bash theme={null}
  #(If necessary) Changing hostname
  hostnamectl set-hostname your-new-hostname
  ```

  There should not be an IP block hostname assignment like **127.0.1.1** in the `/etc/hosts` file.

  There should not be an entry like **nameserver 127.0.1.1** in the `/etc/resolv.conf` file.
</Warning>

<Info>
  **Important**

  For the installation to be healthy, your servers need to access the following addresses.

  **MongoDB:**

  * [https://www.mongodb.org/static/pgp/server-8.0.asc](https://www.mongodb.org/static/pgp/server-8.0.asc)
  * [https://repo.mongodb.org/yum/redhat/](https://repo.mongodb.org/yum/redhat/)
</Info>

## Operating System Configurations

<Note>
  These steps should be performed on all MongoDB servers.
</Note>

```bash theme={null}
# Apinizer user is created and authorized.
sudo adduser apinizer
sudo passwd apinizer
sudo usermod -aG wheel apinizer

# Switch to user and continue operations
su - apinizer

# It is recommended that the following tools be installed on all servers
sudo yum install -y net-tools yum-utils bind-utils device-mapper-persistent-data lvm2 telnet wget zip curl lsof jq

# Firewall is turned off
sudo systemctl stop firewalld
sudo systemctl disable firewalld

# SELinux is disabled to prevent communication problems on servers
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config

# Swap is turned off and the swap line in /etc/fstab file is deleted to prevent it from restarting
sudo swapoff -a
sudo vi /etc/fstab
# vi file is saved and closed (:wq)
```

## MongoDB Installation

### Installing MongoDB Application

<Note>
  These steps should be performed on all MongoDB servers.
</Note>

```bash theme={null}
sudo vi /etc/yum.repos.d/mongodb-org-8.0.repo
```

```ini theme={null}
[mongodb-org-8.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/8.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-8.0.asc
```

```bash theme={null}
sudo yum install -y mongodb-org-8.0.17
```

### MongoDB Configurations

<Note>
  These steps should be performed on all MongoDB servers.
</Note>

**Creating key:**

```bash theme={null}
sudo mkdir -p /etc/mongodb/keys/
sudo chown -Rf apinizer:apinizer /etc/mongodb/keys
sudo chmod -Rf 700 /etc/mongodb/keys
sudo openssl rand -base64 756 > /etc/mongodb/keys/mongo-key
sudo chmod -Rf 400 /etc/mongodb/keys/mongo-key
sudo chown -Rf mongod:mongod /etc/mongodb
```

You need to add the following parameters to the `/etc/mongod.conf` file by setting them according to your environment:

* storage / wiredTiger
* replication
* security
* setParameter
* processManagement

The following are edited and added without changing other lines.

```yaml theme={null}
storage:
  dbPath: /var/lib/mongo
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 25080
  bindIp: 0.0.0.0

replication:
  replSetName: apinizer-replicaset

security:
  authorization: enabled
  keyFile: /etc/mongodb/keys/mongo-key

setParameter:
  transactionLifetimeLimitSeconds: 300

processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo
```

Then MongoDB application is started.

```bash theme={null}
sudo systemctl start mongod
sudo systemctl enable mongod
```

<Info>
  If MongoDB installation will be done on multiple servers, keys created on Primary node are moved to all nodes and the same permissions are given.

  ```bash theme={null}
  # Key file is copied from Primary server to all Secondary servers
  scp -r /etc/mongodb/keys/ apinizer@mongoDb2:/tmp
  # the same operation must be done separately for mongoDb3

  # Key is moved to where it should be on Secondary servers
  sudo mv /tmp/mongo-key /etc/mongodb/keys/

  # Permissions are checked and corrected
  chmod -Rf 400 /etc/mongodb/keys
  chown -Rf mongod:mongod /etc/mongodb
  ```
</Info>

### ReplicaSet Configuration and Authorized User Definition

<Note>
  These steps should be performed only on MongoDB Primary server.
</Note>

<Warning>
  Replicaset activation operation should be done only on Primary server.
</Warning>

**Activating Replicaset:**

```bash theme={null}
mongosh mongodb://localhost:25080
#If connection error is given at this stage, server name with server address should be added under /etc/hosts and it should be checked if one of the values of 127.0.0.1 expression is localhost

rs.initiate()
rs.status()
```

**Creating authorized user for Apinizer application:**

```javascript theme={null}
use admin
db.createUser(
  {
    user: 'apinizer',
    pwd: '<YOUR_PASSWORD>',
    roles: [ { role: 'root', db: 'admin' } ]
  }
);
exit;
```

<Info>
  If MongoDB's local management will be done by you, a user with the following role needs to be created.

  ```javascript theme={null}
  roles: [ { role: "readWrite", db: "apinizerdb" } ]
  ```
</Info>

<Info>
  **If password change is desired**

  ```javascript theme={null}
  use admin
  db.changeUserPassword("apinizer", passwordPrompt())
  ```
</Info>

Replicaset settings are made.

```bash theme={null}
mongosh mongodb://localhost:25080 --authenticationDatabase "admin" -u "apinizer" -p
cfg = rs.conf()
cfg.members[0].host = "<MONGO_IP_ADDRESS>:25080"
rs.reconfig(cfg)
rs.status()
```

<Info>
  Grant permission to a user on previously created MongoDB using the following command lines.

  ```javascript theme={null}
  use admin;
  db.grantRolesToUser('admin', [{ role: 'root', db: 'admin' }])
  ```
</Info>

<Info>
  **If arbiter is desired to be added to MongoDB:**

  ```javascript theme={null}
  db.adminCommand({ setDefaultRWConcern: 1, defaultWriteConcern: { w: "majority" } })
  rs.addArb("<MONGO_IP_ADDRESS>:25080")
  ```
</Info>

### MongoDB ReplicaSet Installation on Multiple Servers

<Note>
  These steps should be performed only on MongoDB Primary server.
</Note>

<Info>
  Apinizer recommends MongoDB's high availability feature. High availability enables Secondary Nodes to come into play when Primary Node fails.

  At least 3 servers (1 Primary and 2 Secondary) are required for high availability in MongoDB as well. If Primary node encounters a problem, a Secondary node automatically becomes Primary, so the system works without interruption. When Primary node becomes active again, it remains as Secondary node. However, this function cannot be seen when there are fewer than 3 active servers.

  High availability is not limited to only 3 servers; it can also be applied with Arbiter or more servers. Servers can be positioned in different locations to increase system continuity.

  For more information, you can check the link [https://www.mongodb.com/docs/manual/core/replica-set-architectures/](https://www.mongodb.com/docs/manual/core/replica-set-architectures/).
</Info>

When you restart Mongod services, you can configure Secondary nodes on Primary node with replica set architecture using the following commands.

```bash theme={null}
mongosh mongodb://<PRIMARY_NODE>:25080 --authenticationDatabase "admin" -u "apinizer" -p
rs.add("mongoDb02:25080")
rs.add("mongoDb03:25080")
rs.status()
exit;
```

With this step, a structure consisting of a total of three servers, one Primary and the other two Secondary, has been established.

In high availability situation, if Primary server's connection is cut or it does not work, a Secondary server should automatically take over the main server role. To set this situation, the following steps should be applied for all nodes on Primary node.

```bash theme={null}
mongosh mongodb://<PRIMARY_NODE>:25080 --authenticationDatabase "admin" -u "apinizer" -p
cfg = rs.conf()
cfg.members[0].priority = 1
cfg.members[0].votes = 1
cfg.members[1].priority = 1
cfg.members[1].votes = 1
cfg.members[2].priority = 1
cfg.members[2].votes = 1
rs.reconfig(cfg)
rs.conf()
rs.status()
exit;
```

<Info>
  Priority specifies the priority of the node being selected as new Primary and this value can be scaled between 0 and 1. While value 0 indicates that it can never be Primary node, Primary priority is found according to how close other values are to 1.

  Votes specifies whether a node can vote in a new Primary election and takes value 0 or 1. This value shows whether the node will vote in the election.
</Info>

<Note>
  This installation has been prepared assuming that DNSs in the form of "mongoDb01, mongoDb02, mongoDb03, k8sWorkerIP" can be resolved by the system. In cases where servers cannot resolve these DNSs, either this situation should be fixed or all DNSs should be fixed as IP.
</Note>
