How to Install MongoDB 5 on Debian 11 Bullseye

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

Run the below command to update the system and install the required dependencies for the Database.

apt update

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

Add the MongoDB GPG key to your system.

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

Enable the MongoDB repository.

add-apt-repository 'deb https://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main'

Install the MongoDB with the following command,

apt update

apt install mongodb-org

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

systemctl start mongod
systemctl enable mongod

Output:

root@server:~# systemctl start mongod
root@server:~# systemctl enable mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.

Now, check the status of the MongoDB service.

systemctl status mongod

Output:

root@server:~# systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor pres>
     Active: active (running) since Thu 2021-08-19 15:21:41 EDT; 1min 3s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 18003 (mongod)
     Memory: 77.9M
        CPU: 1.373s
     CGroup: /system.slice/mongod.service
             └─18003 /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@server:~# mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v5.0.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a27e1b61-8661-451c-a155-10118ef1b887") }
MongoDB server version: 5.0.2
{
  "authInfo" : {
    "authenticatedUsers" : [ ],
    "authenticatedUserRoles" : [ ]
  },
  "ok" : 1
}

Creating Administrative MongoDB User

First, access the MongoDB Shell.

mongo

Connect to the admin database.

use admin

Output:

> use admmin
switched to db admmin

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

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

Output:

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

Exit the mongo shell.

quite()

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

mongo -u mongoAdmin -p --authenticationDatabase admin

Output:

root@server:~# mongo -u mongoAdmin -p --authenticationDatabase admin
MongoDB shell version v5.0.2
Enter password: 

Switch to the admin database.

use admin

Output:

> use admmin
switched to db admmin

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

show users

Output:

> show users
{
        "_id" : "admin.mongoAdmin",
        "userId" : UUID("773c7b28-9950-47b1-8b7d-19bb4e24ecab"),
        "user" : "mongoAdmin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}