How to Install MongoDB on Debian 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.

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-4.2.asc | sudo apt-key add -

Enable the MongoDB repository.

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

Install the 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

Output:

root@vps:~# systemctl start mongod
root@vps:~# 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@vps:~# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2021-06-10 10:25:37 EDT; 11min ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 28983 (mongod)
   Memory: 81.7M
   CGroup: /system.slice/mongod.service
           └─28983 /usr/bin/mongod --config /etc/mongod.conf

Jun 10 10:25:37 vps.server.com systemd[1]: Started MongoDB Database Server.

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

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

Output:

root@vps:~# mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v4.2.14
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d8e8ed25-8b88-4e5a-9dcc-41e3abad28f9") }
MongoDB server version: 4.2.14
{
        "authInfo" : {
                "authenticatedUsers" : [ ],
                "authenticatedUserRoles" : [ ]
        },
        "ok" : 1
}

Creating Administrative MongoDB User

First, access the MongoDB shell.

mongo

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: "SecurePassword",
        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:

root@vps:~# mongo -u mongoAdmin -p --authenticationDatabase admin
MongoDB shell version v4.2.14
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("773c7b28-9950-47b1-8b7d-19bb4e24ecab"),
        "user" : "mongoAdmin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}