How to install MongoDB on Ubuntu 22.0 LTS
Tags: database, mongodb, javascript, webdev
Published: 2022-12-09 13:00:17



# Install the Dependencies

```bash
sudo apt update
apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
```

# Add MongoDB GDB key

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

```

# Create a list for MongoDB

```bash
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
```

update the local package Datebase

```bash
sudo apt-get update
```

install the mongodb using the following command

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

if you run into an error like this

```bash
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 mongodb-org-mongos : Depends: libssl1.1 (>= 1.1.1) but it is not installable
 mongodb-org-server : Depends: libssl1.1 (>= 1.1.1) but it is not installable
E: Unable to correct problems, you have held broken packages.
```

Run the folloing commands:

```bash
echo "deb http://security.ubuntu.com/ubuntu focal-security main" | sudo tee /etc/apt/sources.list.d/focal-security.list

sudo apt-get update

sudo apt-get install libssl1.1
```

Start the MongoDB service and enable it to start automatically after rebooting the system.

```bash
systemctl start mongod
systemctl enable mongod
```

Now, check the status of the MongoDB service.

```bash
systemctl status mongod
```

Output:

```bash
root@crown:~# systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset>
     Active: active (running) since Wed 2022-03-23 18:54:34 UTC; 51min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 3619 (mongod)
     Memory: 172.3M
        CPU: 18.403s
     CGroup: /system.slice/mongod.service
             └─3619 /usr/bin/mongod --config /etc/mongod.conf
```

To verify whether the installation has completed successfully by running the following command.

```bash
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
```

Output:

```bash
root@crown:~# mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9722b711-f1f0-43f2-aec6-f6172da9d237") }
MongoDB server version: 5.0.6
{
        "authInfo" : {
                "authenticatedUsers" : [ ],
                "authenticatedUserRoles" : [ ]
        },
        "ok" : 1
}
```

# Creating Administrative MongoDB user

First, access the MongoDB shell.

```bash
mongo
```

Output:

```bash
root@crown:~#  mongo
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("08890b80-d543-47c1-8523-57ac3c66cf73") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
```

Connect to the admin Database:

```bash
use admin
```

Output:

```bash
> use admin
switched to db admin
```

List the users and see if you can list the created user.

```bash
show users
```

Output:

```bash
> show users
{
        "_id" : "admin.mongoAdmin",
        "userId" : UUID("861b55a3-eaf2-4617-ac4e-34f5284f8a87"),
        "user" : "mongoAdmin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}
```

**This concludes the installation and setup of MongoDB on your Ubuntu 22.04 server.**