# MongoDB Cluster with 3 Linux Instances

## **Setup MongoDB Cluster**&#x20;

### **Prerequisite**

Require two or more servers to set cluster

Ubuntu 22.04

Follow the below steps to set up a **3-node MongoDB cluster**:

## Configuration and installation

### Update && Upgrade the system

Before processing, update and upgrade all packages.

```
sudo apt-get update -y && sudo apt-get upgrade -y
```

Check your current hostname by running the following command:

```
hostname
```

### Set the Hostname in each server

To change the hostname, you can use the `hostnamectl` command. For example, to set the hostname to "hostname", you would run.

```
sudo hostnamectl set-hostname myhostname
```

On Linux and Unix-like systems, the `hosts` file is located in the `/etc/` directory. To edit this file on each of your three servers, use your preferred text editor.

<pre><code><strong>sudo vi /etc/hosts
</strong></code></pre>

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FhZPvI251lbXkTTlAMiZR%2Fimage.png?alt=media&#x26;token=35d95be2-5b83-49ba-a282-3bf1724797ef" alt=""><figcaption></figcaption></figure>

### **Server Hostname Configuration**

Configuring your servers with descriptive hostnames is advisable as it enhances clarity and ease of management.

* **mongo0.replset.member**
* **mongo1.replset.member**
* **mongo2.replset.member**

Using these hostnames, your `/etc/hosts` files would look similar to the following highlighted lines:

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F4O9epR2z4KrEORoEEiZg%2Fimage.png?alt=media&#x26;token=d666f049-f591-4bc8-acf4-f91cdc54bb07" alt=""><figcaption></figcaption></figure>

### **To add the MongoDB GPG key**

**Import the MongoDB GPG Key**

First, import the MongoDB GPG key to verify the authenticity of the software packages:

To get the latest version of the software, add MongoDB’s dedicated package repository to your APT sources. Then, install the meta-package that always points to the newest MongoDB version.

First, import the public GPG key for the latest stable version of MongoDB using the following command:

```
sudo wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Ff51B9QgQlAjOSwNvLMng%2Fimage.png?alt=media&#x26;token=698f9383-eece-4a14-95bb-4ab330880c00" alt=""><figcaption></figcaption></figure>

**Add MongoDB Repository**

Next, add the MongoDB repository to your system's list:

APT (Advanced Package Tool) uses two main locations for finding online package repositories: the `sources.list` file and the `sources.list.d` directory. The `sources.list` file lists repository sources line-by-line, placing the most preferred at the top. The `sources.list.d` directory allows for modular and organized management by maintaining additional repository sources in separate files.

Execute the following command to create a file named `mongodb-org-4.4.list` in the `sources.list.d` directory:

```
sudo echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FeDgoixlNjmXr4hAKz2mg%2Fimage.png?alt=media&#x26;token=1d106ed5-d00d-40df-b254-528399a7758b" alt=""><figcaption></figcaption></figure>

After executing this command, refresh your server’s local package index to ensure APT can locate the MongoDB-org package:

```
sudo apt-get update -y
```

### **Install MongoDB**

Now, you can install MongoDB using the following command:

```
sudo apt-get install mongodb-org -y
```

This command will install the MongoDB package and its dependencies.

### **Start MongoDB Service**

After installing MongoDB, it should start automatically. To check the service status, run:

```
sudo systemctl start mongod
sudo systemctl status mongod
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FrTYcp0UkRuNp1U8CEWIj%2Fimage.png?alt=media&#x26;token=17600f71-0edb-4ab2-8a97-c034f310eb0e" alt=""><figcaption></figcaption></figure>

#### Enable MongoDB to Start on the Boot

```
sudo systemctl enable mongod
```

#### Verify Installation

To verify that MongoDB is installed successfully, open the MongoDB shell by following these steps:

<pre><code><strong>sudo mongo --version
</strong></code></pre>

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FKRKMo9ZVnYgnxW4aUE0m%2Fimage.png?alt=media&#x26;token=f5d53efa-f221-4696-989c-b5ebe6904fb0" alt=""><figcaption></figcaption></figure>

```
sudo mongo
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FJPD18VlR4szVQQ9iC2NN%2Fimage.png?alt=media&#x26;token=63b9094b-e4c6-4512-8063-b36d0f227871" alt=""><figcaption></figcaption></figure>

## **Enable Replication in Each Server's**  <a href="#step-3-enabling-replication-in-each-server-s-mongodb-configuration-file" id="step-3-enabling-replication-in-each-server-s-mongodb-configuration-file"></a>

#### **MongoDB Configuration File:-** <a href="#step-3-enabling-replication-in-each-server-s-mongodb-configuration-file" id="step-3-enabling-replication-in-each-server-s-mongodb-configuration-file"></a>

You have successfully configured the **/etc/hosts** file on your servers to map hostnames to their respective IP addresses.

To change MongoDB settings, edit the primary configuration file found at **/etc/mongod.conf**. This file is created automatically during installation.

Use a text editor **vi** to open and modify the file. Here’s the command:

```
sudo vi /etc/mongod.conf
```

In MongoDB, various configuration options allow you to customize the server's behavior. The **system log** option manages the database’s logging settings, enabling you to specify what gets logged and where. Conversely, the **net** option is essential for configuring network-related settings, providing control over parameters such as port configuration and network interfaces.

Update the IP address in each server from 127.0.0.0 to 0.0.0.0.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FjHQUt60qGRcD2zOgn4kT%2Fimage.png?alt=media&#x26;token=bfe9e1dc-c8f0-4897-b6ab-1331185999cf" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FYYweABUedW8vhni93SfC%2Fimage.png?alt=media&#x26;token=8a7f47f4-2933-446b-8535-42a756e34f27" alt=""><figcaption></figcaption></figure>

Create a mongo key file in each server

```
sudo mkdir -p /etc/mongodb/key-files/
```

Now create an OpenSSL and run these commands

<pre><code><strong>sudo openssl rand  -base64  756 > /etc/mongodb/key-files/mongo-key
</strong></code></pre>

Copy these mongo-key files in another 2 vms

**$ sftp root\@your\_IP\_address**

Enabling Replication in Each Server's MongoDB Configuration File&#x20;

After copying the necessary files, proceed by adding the specified keys to the `/etc/mongod.conf` file on each server.

```
sudo vi /etc/mongod.conf
```

Uncomment the security option and add the key

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FmE2VsZZ0frnncCDEohft%2Fimage.png?alt=media&#x26;token=e44bf51b-39ba-4b74-887c-a38a6f184c2e" alt=""><figcaption></figcaption></figure>

Add the key

**keyFile: /etc/mongodb/key-files/mongo-key**

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FLJuAnnFcHVNmRYEYYJAm%2Fimage.png?alt=media&#x26;token=761255f6-c58d-42b9-bc64-3d984e6817f8" alt=""><figcaption></figcaption></figure>

Configure the file permissions and ownership in each server

```
sudo chmod 400 /etc/mongodb/key-files/mongo-key
sudo chown -R mongodb:mongodb /etc/mongodb
```

Before executing the following command, ensure the Mongod service is stopped.

```
sudo systemctl stop mongod
```

Replication runs these commands in all nodes for set replace

```
sudo /usr/bin/mongod --replSet "evermight" --config /etc/mongod.conf --fork 
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fcs31gTWbFX75WLQRHeaw%2Fimage.png?alt=media&#x26;token=69d9e6fe-d1b1-4948-9182-f659f68da39a" alt=""><figcaption></figcaption></figure>

Also, add replication of all nodes

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F2J0KSuVlZKvcydUVCJPY%2Fimage.png?alt=media&#x26;token=561c6f5a-3cf7-454e-9867-791b95d7930a" alt=""><figcaption></figcaption></figure>

```
sudo systemctl start mongod
```

### **Starting the Replica Set and Adding Members:**

After setting up your three MongoDB instances, initiate replication by opening a MongoDB shell and adding each instance as a replication member.

```
sudo mongo
```

To begin the replication process, execute these commands:

```
mongo> rs.initiate({_id: "evermight", members: [{_id: 1, host: "mongo1"},{_id: 2, host: "mongo2"}] });
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FkswhKkop2fn3RqDHI4uF%2Fimage.png?alt=media&#x26;token=92733f2d-7a65-4754-acf2-39ff079852e7" alt=""><figcaption></figcaption></figure>

### Conclusion <a href="#h-conclusion" id="h-conclusion"></a>

MongoDB is a free, open-source NoSQL database management system (DBMS) popular for large-scale websites or applications. Since it doesn’t use a fixed schematic structure to store data, it is more flexible and scalable than SQL.
