How to Install MongoDB on Debian 11 (Community Edition Tutorial)

How to Install MongoDB on Debian 11 (Community Edition Tutorial). MongoDB is a popular open source NoSQL database. It is a document oriented database that supports structured and unstructured data, including JSON, XML, and organic data.

MongoDB server is highly available and highly scalable. The database stores data in collections of documents, which can include any sort of data (e.g., documents, graphs) that fits into a certain schema.

This tutorial goes through the features, benefits, and installation process of MongoDB Community Edition on Debian 11.

What is MongoDB

MongoDB is a free and open-source project that lets you store, manage and query data. It’s the leading NoSQL database, and it provides the best of both relational and document stores.

MongoDB uses the BSON format, which is a binary encoded JSON format, to store documents. This means that the information is stored in binary form, which is substantially quicker than JSON.

Additionally, this permits the storage of binary data, which is advantageous for the storage of pictures, films, and other binary data. It can also add indexes to your documents to speed up scans of large collections of data.

Designed for easy scalability MongoDB offers high performance and flexibility. It is an excellent choice for applications that require both ACID transactions (atomicity, consistency, isolation). With MongoDB it can handle very large amounts of data with low latency and high throughput (millions of operations per second).

It is designed to work well with the JavaScript programming language and the NoSQL approach to application design.

MongoDB is written in C++ and is compatible with Linux, macOS, and Windows. It can be installed as a service or as a standalone application.

It supports authentication mechanisms such as HTTP Basic Auth, OAuth 2.0, and Active Directory Federation Services (AD FS).

Next in How to Install MongoDB on Debian 11 we learn what are the MongoDB features. 

Features of MongoDB

MongoDB owes its popularity to the following features:

  • Document oriented.
  • Provides a rich query language for dynamic querying and supports ad-hoc queries.
  • High availability.
  • Aggregation.
  • Database triggers. 
  • Time series data.
  • Data embedding.
  • Efficient storage engine and memory handling.
  • Data consistency and integrity.

Benefits of MongoDB

MongoDB is highly convenient to use due to its following benefits.

  • NoSQL database without schemas. When using MongoDB, the database schema does not need to be designed. 
  • Does not supports Join operations.
  • Fields in MongoDB documents have a lot more flexibility. 
  • It includes diverse data. 
  • This database offers scalability, availability, and great performance. 
  • Supports geospatial efficiency.
  • The data is stored as BSON documents because the database is document-oriented.
  • Additionally, it supports multiple document ACID transitions (string from MongoDB 4.0).
  • Cost-effective DBMS that is easy to set up and use.
  • There is no need for SQL injection. 
  • It is simple to incorporate with Big Data Hadoop. 

Follow this post to learn how to install MongoDB Community Edition on Debian 11.

How to Install MongoDB on Debian 11 (Community Edition Tutorial)

Prerequisites

You will need:

  • Debian 11 system.
  • A user with root or sudo privileges.
  • A network connection.

Update Server Packages

Run the following commands to ensure all the packages are up-to-date and the server is ready for the MongoDB installation.

				
					sudo apt update
sudo apt dist-upgrade -y

				
			

Install Dependent Packages

You will need wget and GnuPG packages to download the MongoDB repository securely. Install these dependency packages with the following commands:

				
					sudo apt install wget
sudo apt install gnupg

				
			

Now, you can move on to adding MongoDB Community Edition to your system.

Import MongoDB Repository GPG Key

The MongoDB package is not included by default in the Debian default repository. As a result, you must add and install MongoDB from Debian’s official repository to the sources list.

Import the GPG key of the MongoDB repository with the following command.

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

Add MongoDB Repository to Sources List

Next, add and enable MongoDB Repository to APT with the following command.

				
					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
				
			

Output:

				
					deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main
				
			

Update Your Packages

Run the following command to update your packages and ensure the changes are in effect.

				
					sudo apt update
				
			

Output:

				
					Ign:1 http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 InRelease
Hit:2 http://security.debian.org/debian-security bullseye-security InRelease
…

				
			

Install MongoDB Community Edition

Now, run the following command to install MongoDB on your system.

				
					sudo apt install -y mongodb-org
				
			

Output:

				
					Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org-database mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server
  mongodb-org-shell mongodb-org-tools
…

				
			

Enable and Start the MongoDB Service

Now you can enable the MongoDB service to start on boot with the following command.

				
					sudo systemctl enable mongod
				
			

Output:

				
					Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.
				
			

Or you can simply start the service with the following command.

				
					sudo service mongod start
				
			

Similarly, you can check the status of your MongoDB service with the following command.

				
					sudo service mongod status
				
			

Output:

				
					● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-07-12 11:44:59 EDT; 17s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 9230 (mongod)
   Memory: 66.1M
        CPU: 1.747s
     CGroup: /system.slice/mongod.service
             └─9230 /usr/bin/mongod --config /etc/mongod.conf

Jul 12 11:44:59 debian systemd[1]: Started MongoDB Database Server.

				
			

Verify MongoDB installation

You can verify your MongoDB’s successful installation with the following command.

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

Output:

				
					MongoDB shell version v5.0.9
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("cbd4c5af-29e6-4b8d-a14a-28702d74e0ff") }
MongoDB server version: 5.0.9
{
	"authInfo" : {
		"authenticatedUsers" : [ ],
		"authenticatedUserRoles" : [ ]
	},
	"ok" : 1
}


				
			

Configure MongoDB

Now that you have installed MongoDB, you should configure it further for security and convenience.

To do that, open the MongoDB configuration file with a nano editor.

				
					sudo nano /etc/mongod.conf
				
			

Go to the #security section and add the following lines of code.

				
					security:
  authorization: enabled

				
			

Next, go to the #network interfaces section and add your system IP in the bindIp field.

				
					net:
  port: 27017
  bindIp: 127.0.0.1,

				
			

Save and exit the configuration file.

Restart the MongoDB service for the changes to take effect.

				
					sudo systemctl restart mongod
				
			

Create MongoDB Admin User

Now please create an admin user to manage administrative tasks.

For that, launch the MongoDB shell using the following command.

				
					mongosh
				
			

Change to the admin database.

				
					use admin
				
			

 And run the following command to add the admin user.

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

Output:

				
					Enter password
****{ ok: 1 }

				
			

Setup the password on the prompt.

Now type exit to get out of the MongoDB shell.

Connect to the MongoDB

Now, you can use the admin user to connect to the MongoDB shell with the following command.

				
					mongo --port 27017 --authenticationDatabase "admin" -u "admin" -p
				
			

Output:

				
					MongoDB shell version v5.0.9
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c4b1833e-8080-4bb0-9e30-440bea5fca0c") }
MongoDB server version: 5.0.9
================
…
				
			

Similarly, you can use the following string format to connect to MongoDB.

				
					mongodb://:@:27017/
				
			

Great effort in reading How to Install MongoDB on Debian 11. Let’s conclude.

How to Install MongoDB on Debian 11 (Community Edition Tutorial) Conclusion

In this guide, you have learned how to install MongoDB Community Edition on Debian 11.

MongoDB is a NoSQL database that’s hundreds of thousands of people use worldwide to power some of the web’s most popular applications and services.

Here is our other content on MongoDB for more insight.

Avatar for Sobia Arshad
Sobia Arshad

Information Security professional with 4+ years of experience. I am interested in learning about new technologies and loves working with all kinds of infrastructures.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x