# How To Install MongoDB on Ubuntu 22.04

MongoDB is an open-source NoSQL database that uses a flexible, document-oriented data model. Unlike traditional relational databases that use tables and rows, MongoDB stores data in JSON-like documents with varied structures. This makes it highly adaptable and suitable for applications requiring scalable, high-performance, and flexible data storage solutions.

{% embed url="<https://youtu.be/D2EBPhhotzE?si=Fb6MazzdmkNFlpu>\_" %}

## Prerequisites

* Launch the [Ubuntu 22.04 instance](https://docs.neevcloud.com/neevcloud-products/computes/getting-started-launch-vms) on Neevcloud
* Access the server using [SSH](https://docs.neevcloud.com/neevcloud-products/computes/getting-started-launch-vms#ssh)

### Update && Upgrade the system <a href="#update-and-and-upgrade-the-system" id="update-and-and-upgrade-the-system"></a>

First, ensure your package list is up to date to avoid any issues with outdated packages

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

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FdZa3iaxHY7Wq85I1zb4Z%2Fimage.avif?alt=media&#x26;token=9c8c13b9-8e03-420f-ad1c-869c707426fc" alt=""><figcaption></figcaption></figure>

## Installation MongoDB

Execute the given command to install the MongoDB package.

```
 sudo apt install software-properties-common gnupg apt-transport-https ca-certificates -y
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FWydd8bE6PMY1oxcuNle2%2Fimage.png?alt=media&#x26;token=e86e5dba-03a6-4925-86aa-1c1c32a7b191" alt=""><figcaption></figcaption></figure>

### Add necessary packages

To install the most recent MongoDB package, you need to add the MongoDB package repository to your sources list file on Ubuntu.

```
curl -fsSL https://pgp.mongodb.com/server-7.0.asc |  sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
```

After this next. add MongoDB APT repository to the **`/etc/apt/sources.list.d/`** directory.

```
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
```

Next, include the MongoDB source list file in the `/etc/apt/sources.list.d/` directory, which consists of a lone line stating

```
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse"
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F3CwQT5YOAaUr8qJ3EN34%2Fimage.png?alt=media&#x26;token=761632c7-f680-406f-9b35-a40950a058ad" alt=""><figcaption></figcaption></figure>

* Add all repositories first, then update the packages

```
sudo apt update 
```

**Now, proceed to install the mongodb-org meta-package, which includes MongoDB.**

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

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FenMb5hobrBngnz1rV0YX%2Fimage.png?alt=media&#x26;token=307bb978-d554-441a-addf-e8e17e4b40db" alt=""><figcaption></figcaption></figure>

### Verify the version of MongoDB Installed

```
sudo mongod --version
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FJXOIgmQdwo5vzN2S7yEH%2Fimage.png?alt=media&#x26;token=6a9bbfd4-1985-4a23-8ecc-54ceec6b7aa1" alt=""><figcaption></figcaption></figure>

### Start and Enable MongoDB <a href="#start-and-enable-mariadb" id="start-and-enable-mariadb"></a>

* After installation, the MongoDB service starts and enables

```
sudo systemctl start mongod
```

* Enable it to start when the system rebooted

```
sudo systemctl enable mongod
```

### Manage MongoDB Service

To verify the database's status, make sure the server is actively listening on port 27017.

```
ss -antpl | grep 27017
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FO8HsIjHpyy6pJBw0nOvN%2Fimage.png?alt=media&#x26;token=e9ab3759-55b0-4286-8124-01484120825a" alt=""><figcaption></figcaption></figure>

### To check the status <a href="#to-check-the-status" id="to-check-the-status"></a>

Verify that the database service is active and running. Run the following command

```
sudo systemctl status mongod
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FFAUvxBOjPX0nazkPZtN9%2Fimage.png?alt=media&#x26;token=dfb53173-8ce8-4d7e-9963-b0c1f30e6104" alt=""><figcaption></figcaption></figure>

## Create MongoDB databases and User

Make sure the MongoDB instance is up and set up for remote access, then learn how to create a database and a user in MongoDB.

### To access the MongoDB, run the following command.

```
sudo mongosh
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FoV6O7Gbvg61AELDPui6N%2Fimage.png?alt=media&#x26;token=29d7f80a-b3dc-45be-ba00-c22329af07c6" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
By default, installation results in the creation of three databases. This includes admin, config, and local.
{% endhint %}

### To list the existing databases, run these commands.

```
show dbs
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FSU3ukoHm2ZGXiNRla1Rd%2Fimage.png?alt=media&#x26;token=e8cb38e4-2cba-4cd8-bf19-2fb9533f5ad5" alt=""><figcaption></figcaption></figure>

To make a database, execute the **`use`** command with the name of the database. For example, to create a database named **`test_db`**, run the command

```
use test_db
```

To verify the database you are using, execute the **`db`** command. In this scenario, the output will be **`test_db`**.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FvXJ3IJC3MIYdCdxc3yvx%2Fimage.png?alt=media&#x26;token=127ab069-7603-4212-a658-d897bda4d728" alt=""><figcaption></figcaption></figure>

### To create a User

MongoDB offers various shell techniques to control your database. The `db.createUser` function enables the creation of a new user within a database.

This is how you can create a user name **`test_user`** with read-and-write roles for the **`test_db`** database.

{% hint style="info" %}

```bash
db.createUser(
  {
    user: "test_user",
    pwd: "test-password",
    roles: [ { role: "readWrite", db: "test_db" } ]
  }
)
```

{% endhint %}

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F1iISoHTnRl2sfZCLfh6J%2Fimage.png?alt=media&#x26;token=94edbda1-938d-4439-8d80-88f00e5a76d8" alt=""><figcaption></figcaption></figure>

You can list the users created using the `db.getUsers()` command.

```
db.getUsers();
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FQHN93XHQQyZDyrrlsiok%2Fimage.png?alt=media&#x26;token=5c70b26f-9496-42dd-b6c3-80c8d2c89f9e" alt=""><figcaption></figcaption></figure>

Alternatively, run the command

```
show users
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fh5u8NsIS76NHZvIv34WT%2Fimage.png?alt=media&#x26;token=86cb1bea-4091-4bcc-bedc-382b8823adb1" alt=""><figcaption></figcaption></figure>
