How to Install MongoDB 5 on Ubuntu 21.10

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 -

Output:

root[email protected]:~# wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
OK

Adding the MongoDB repository with the following command.

add-apt-repository 'deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse' 

Output:

[email protected]:~# add-apt-repository 'deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse'
Hit:1 http://de.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://de.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://de.archive.ubuntu.com/ubuntu focal-backports InRelease
Ign:4 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 InRelease
Hit:5 http://de.archive.ubuntu.com/ubuntu focal-security InRelease
Get:6 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 Release [4417 B]    

Update the local package database.

apt-get update

Install MongoDB with the following command.

apt install 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:

[email protected]:~# systemctl status mongod
● mongod.service - MongoDB Database Server
 Loaded: loaded (/lib/systemd/system/mongod.service; enabl>
 Active: active (running) since Mon 2021-10-25 21:02:08 UT>
   Docs: https://docs.mongodb.org/manual
Main PID: 2756 (mongod)
Memory: 62.8M
    CPU: 1.010s
 CGroup: /system.slice/mongod.service
         └─2756 /usr/bin/mongod --config /etc/mongod.conf

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

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

Output:

[email protected]:~# mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c927d775-911b-4c44-88d7-4c0c5deb3edb") }
MongoDB server version: 5.0.3
{
  "authInfo" : {
    "authenticatedUsers" : [ ],
    "authenticatedUserRoles" : [ ]
  },
  "ok" : 1
}

Creating Administrative MongoDB User

First, access the MongoDB shell.

mongo

Output:

[email protected]:~# mongo
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("bc78947c-5503-4820-a168-229cd94c8bfd") }
MongoDB server version: 5.0.3
================
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.
We recommend you begin using "mongosh".
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:

[email protected]:~# mongo -u mongoAdmin -p --authenticationDatabase admin
MongoDB shell version v5.0.3
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("3e3f639a-de55-4693-bce9-e9788bf2967e"),
  "user" : "mongoAdmin",
  "db" : "admin",
  "roles" : [
    {
      "role" : "userAdminAnyDatabase",
      "db" : "admin"
    }
  ],
  "mechanisms" : [
    "SCRAM-SHA-1",
    "SCRAM-SHA-256"
  ]
}

Done.