Debian

How to install MongoDB on Debian 11

MongoDB is a NoSQL database that is simple, object-oriented, scalable, and a dynamic database. It is very popular nowadays in comparison to other NoSQL databases, even from relational databases because of its ease of learning.

Unlike relational databases, it is free from the tables, you can add and modify the data easily due to its document-oriented data model and because of this model, it can correspond to different data types of many programming languages.

MongoDB has some distinctive features like it is schema-less so you are free from the burden of type-mapping, similarly, it can be used as a file system with data replication and load balancing features over multiple machines for storing files.

In this writeup, we will discuss the method to install MongoDB on Debian 11.

How to install MongoDB on Debian 11

For the installation of the latest version of MongoDB on Debian, first, we will install the “wget” package. This package is used to import the GPG key of MongoDB.

$ sudo apt-get install wget

We will import the GPG key of MongoDB from its official website.

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

The output of the executed command should be “OK”. There is a warning that the key we are going to be added is deprecated but we can still use it. Enable the MongoDB repository by creating a file /etc/apt/sources.list.d/mongodb-org-5.0.list and as the “bullseye” version of MongoDB is still not released so we use the “buster” version of MongoDB.

$ 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

Update the repository.

Now we will install the MongoDB package along with other packages which are related to MongoDB.

$ sudo apt-get install mongodb-org mongodb-org-server mongodb-org-database mongodb-org-mongos mongodb-org-shell mongodb-org-tools -y

After the successful installation of MongoDB, start it using the systemctl command.

$ sudo systemctl start mongod

Once the MongoDB is started, enable it as

$ sudo systemctl enable mongod

Check the status of MongoDB and verify it is running successfully.

$ sudo systemctl status mongod

To check the version of MongoDB which is installed.

$ mongod --version

To secure the MongoDB, launch the MongoDB.

$ mongo

Type the “admin” in the environment of MongoDB.

use admin

You will be switched to the DB main. Write the following command to add a user to create user, username is “mongoAdmin” and password is “abc123”, you can change username and password accordingly:

 db.createuser(
   {
   user: “mongoAdmin”,
   pwd:  “abc123”,
   Roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ]
   }
   )

The output shows the user is added successfully. Once done type “quit()” to exit the MongoDB environment.

To enable the security of MongoDB, open the configuration file of MongoDB.

$ sudo nano /etc/mongod.conf

Find out the “Security”, uncomment it by removing “#” and write a line next to it.

authorization: enabled

Save it by pressing CTRL + S and then exit by pressing CTRL + X. Restart the MongoDB so it can refresh the modified changes:

$ sudo systemctl restart mongod

To verify that the user has been added we will run the following command, change “mongoAdmin” in the command by your username, and after execution, it will ask for a password, enter the password which in our case is “abc123”.

$ mongo -u mongoAdmin -p --authenticationDatabase admin

Type the “use admin”, once you are in the MongoDB environment.

use admin

To display the users type “show users”:

show users

We can see the user has been displayed which we created.

Conclusion

MongoDB is easy to use if you are familiar with JavaScript. Also, it is schemaless. Other than that, it is recommended to beginners due to its ease of use and efficiency. In this write-up, we have discussed the installation procedure of MongoDB, by importing its key to the repository of Debian and then installing it. Confirm its status after starting and enabling it, and then add a user to MongoDB by assigning it a role.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.