How to Install MongoDB on Debian 11 Linux

MongoDB follows the BSON model to store data of websites and this data is stored in the form of collections that are similar to tables of relational databases, and these collections combine to form a database.

There are no limitations to following any specified schema for inserting the data in the database, this means a user can define the schema at any instant of time.

The most important features of MongoDB are:

  • Indexing: which enable the search engine operations easily
  • Replication: this allows to make the copies of data and save it among different servers so, in case of failure of the server, it can be extracted from any other server

In this write-up, we will discuss the installation of the MongoDB on Debian 11 or Debian Bullseye, which is a distribution of Linux.

How to install MongoDB on Debian 11

To install MongoDB on Debian 11, first, we will install the package of wget, which will be used to import the gpg key of MongoDB from its official website. The wget package will be installed as:

$ sudo apt install wget

Now execute the next command to import the gpg key from MongoDB official website using the wget package:

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

‘OK’ should be displayed in the output which will indicate that the key has been successfully imported from the MongoDB website. Now to add the key in the repository by creating a file on the path /etc/apt/sources.list.d/mongodb-org-5.0.list

$ 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

After the addition of the key of MongoDB in the repository of Debian 11, we will update the repository of Debian 11.

$ sudo apt update

After the update of the repository, we will install the MongoDB package along with its dependency packages using the command:

$ sudo apt-get install mongodb-org -y

Once the MongoDB is installed, to confirm its installation we will check the version of installed MongoDB using:

$ mongod --version

After the confirmation of MongoDB installation, we will run its service by using the systemctl command:

$ sudo systemctl start mongod

After the installation we will enable it by:

$ sudo systemctl enable mongod

To check the status of the MongoDB service, we will run the systemctl command:

$ sudo systemctl status mongod

How to secure MongoDB

Once the MongoDB is installed and its service is also being started, now we will make MongoDB secure by entering into the MongoDB environment.

$ mongo

Now type “use admin” in the MongoDB environment:

use admin

Now to add a user, we will create a user by setting the user name and its password, here we are using user name Linux and password abc123, you can set the user name and password by your own wish.

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

Once this process is done, type the quit() to exit from the MongoDB environment.

quit()

To make the security authentication enabled, open the configuration file of MongoDB using:

$ sudo nano /etc/mongod.conf

Search a keyword ‘security’, once found, remove the ‘#’ from it to uncomment it and add the following next to it:

authorization: enabled

Make sure the authorization line should be intended with two spaces, once it has been done, press CTRL+S to save the changes and then CTRL+X to exit the editor mode. Now restart the MongoDB using the systemctl command:

$ sudo systemctl restart mongod 

Now to confirm the security authentication, we will enter the MongoDB environment with the help of the user we created that is ‘Linux’ in our case, and it will ask for a password which we set as ‘abc123’:

$ mongo -u Linux -p --authenticationDatabase admin

Once it is successfully executed, type the “use admin” to switch the main database:

use admin

To display the users, type here “show users”:

show users

We can see the user which we have created with its role.

How to remove the MongoDB from Debian 11

To remove the MongoDB from Debian 11, execute the command:

$ sudo apt purge mongodb-org* -y

Also remove the log directory as:

$ sudo rm -r /var/log/mongodb

And remove lib directory too by:

$ sudo rm -r /var/lib/mongodb

To confirm uninstallation, we will check its version by:

$ mongod --version

Conclusion

MongoDB is the most popular database among the NoSQL databases as it can handle a huge amount of unorganized data very easily. This write-up is related to the installation procedure of MongoDB in Debian 11, in which we import the gpg key of MongoDB from its official website and added it to the repository. Once it is added, we installed it along with the other packages on which MongoDB is dependent. Next, we start the service of MongoDB. Along with the installation, we also discussed how to make MongoDB secure by making a user and setting its password.

Subscribe to Our Newsletter
Join our Newsletter and get the latest Linux news and tips
in your inbox for FREE.