How To Install MongoDB on Debian 11.3

To Install MongoDb on Debian 11.3

Introduction:

MongoDB is a NoSQL database application used to store data as JSON-like documents on servers. MongoDB has modern infrastructure with various services while being malleable and friendly to use, providing freedom to developers and their code.

Installation Procedure:

Step 1: Check the OS version by using the below command

[root@linuxhelp ~]# cat /etc/os-release

PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye

Step 2: Add MongoDB APT repository on Debian 11 by using the below command

[root@linuxhelp ~]#  install gnupg2 wget

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
wget is already the newest version (1.21-1+deb11u1).

Step 3: Add MongoDB APT repository by using the below command

[root@linuxhelp ~]#  wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK
[root@linuxhelp ~]#  echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main

Step 4: Update the packages by using the below command

[root@linuxhelp ~]#  apt update

Get:1 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB]
Hit:2 http://deb.debian.org/debian bullseye InRelease                                           
Get:3 http://deb.debian.org/debian bullseye-updates InRelease [39.4 kB]
108 packages can be upgraded. Run 'apt list --upgradable' to see them.

Step 5: Install MongoDB 5.0 the packages by using the below command

[root@linuxhelp ~]# apt install mongodb-org

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Unpacking mongodb-database-tools (100.5.2) ...
Selecting previously unselected package mongodb-mongosh.
Preparing to unpack .../2-mongodb-mongosh_1.5.0_amd64.deb ...
Adding user mongodb to group mongodb
Done.

Step 6: Check the Mangod version by using the below command

[root@linuxhelp ~]#  mongod --version

db version v5.0.9
Build Info: {
    "version": "5.0.9",
    "gitVersion": "6f7dae919422dcd7f4892c10ff20cdc721ad00e6",
    "openSSLVersion": "OpenSSL 1.1.1n  15 Mar 2022",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "debian10",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Step 7: Configure MongoDB by using the below command

[root@linuxhelp ~]# systemctl start mongod

[root@linuxhelp ~]# systemctl enable mongod

Step 8: Secure MongoDB 5.0 Instance by using the below command

[root@linuxhelp ~]#  mongosh
Current Mongosh Log ID: 62a0f9e18cbb755cef3b077f
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.5.0
Using MongoDB:          5.0.9
Using Mongosh:          1.5.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
   The server generated these startup warnings when booting

Step 9: Switch the Admin Database by using the below command

admin> use admin
already on db admin

Step 10: Create user in mongo Database by using the below command

admin> db.createUser(
...     {
.....         user: "Vultr",
.....         pwd:  "topSecret1",
.....         roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
.....     }
... )
{ ok: 1 }

Step 11: Show user in mongo Database by using the below command

admin> show users
[
  {
    _id: 'admin.Vultr',
    userId: UUID("ad8afccc-d6b0-4326-ae02-a426c9654e08"),
    user: 'Vultr',
    db: 'admin',
    roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ],
    mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
  }
]
admin>
(To exit, press Ctrl+C again or Ctrl+D or type .exit)
admin>

Step 12: Edit the MongoDB configuration file and enable authentication.

[root@linuxhelp ~]#  nano /etc/mongod.conf
Security: Authorization: enable

Step 13. Restart the mongo dB by using the below command

[root@linuxhelp ~]#  systemctl restart mongod

Step 14: Open the MongoDB Database authentication

 [root@linuxhelp ~]#  mongosh -u Vultr -p topSecret1

Current Mongosh Log ID: 62a0fb2bc00cb9c54d18e76f
Connecting to:          mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.5.0
Using MongoDB:          5.0.9
Using Mongosh:          1.5.0

Conclusion

We have reached the end of this article. In this guide, we have walked you through the steps required to install MongoDB on Debian 11.3. Your feedback is much welcome.

FAQ
Q
5. How to open the MongoDB Database authentication.
A
mongosh -u [username] -p [passwd]
Q
4. How to restart MongoDB?
A
systemctl restart Mongodb
Q
3. What is MongoDB APT repository?
A
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add –
Q
2. How to Create a user in Mongo Database?
A
admin> db.createUser(
... {
..... user: "Vultr",
..... pwd: "topSecret1",
..... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
..... }
... )
Q
1. How to Install MongoDB?
A
apt install mongodb-org