How to Install Latest MongoDB on Ubuntu 22.04

MongoDB is an open-source document database used in many modern web applications. and MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as in the traditional relational databases.

Install the dependencies

apt update

apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Add MongoDB GPG Key

Download and add the MongoDB GPG key with the following command

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

Create a list for MongoDB

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 database.

apt-get update

Install the MongoDB with the following command.

apt-get install -y mongodb-org

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

systemctl start mongod
systemctl enable mongod

Now, check the status of the MongoDB service.

systemctl status mongod

Output:

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.

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

Output:

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.

mongo

Output:

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.

use admin

Output:

> use admin
switched to db admin
> 

Run the following command to create a new user and set the password for the user.

db.createUser(
  {
    user: "mongoAdmin",
    pwd: "KAb3747d",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
 )

Output:

Successfully added user: {
"user" : "mongoAdmin",
"roles" : [
  {
    "role" : "userAdminAnyDatabase",
    "db" : "admin"
  }
 ]
}
> 

Exit the mongo shell.

quit()

To test the changes, access the mongo shell using the created administrative user.

mongo -u mongoAdmin -p --authenticationDatabase admin

Output:

mongo -u mongoAdmin -p --authenticationDatabase admin
MongoDB shell version v5.0.6
Enter password:

Switch to the admin database.

use admin

Output:

> use admin
switched to db admin

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

show users

Output:

> 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.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS