How to install MongoDB on Debian 10

The database field has been dominated in the last decades by MySQL, PostgreSQL, Oracle and SQL Server. All these applications are quite good and have in common that they are of the relational and SQL types. But more and more the SQL relational database model is losing popularity in favor of NoSQL and document-based database managers. Of these, MongoDB is perhaps the most popular. So, this post is about installing MongoDB on Debian 10.

When you talk about the NoSQL databases, you usually talk about MongoDB too. MongoDB is a document-oriented database manager that does not use SQL.

Therefore, while it is true that many database concepts are not dependent on a program, it is also true that MongoDB is quite different from MySQL or PostgreSQL. However this is not a problem either, a person who works with relational databases can learn to use MongoDB in a short time.

The main advantages of using MongoDB are the ease of data transfer, stability, high availability and above all the scalability. All this without affecting the performance of the database itself.

This is why MongoDB is being used more and more in various projects around the world. And in this post, we will help you to install it on a Debian 10 machine.

Installing MongoDB on Debian 10

Of course, MongoDB is cross-platform and includes support for most server-oriented Linux distributions. For example, Debian, Ubuntu, CentOS, RHEL, and SUSE.

On the other hand, although it is open source it is not included by default in their official repositories. But, the team of developers enables a repository so that we can install it without problems.

So, open a terminal and install some packages needed to add the MongoDB repository.

:~$ sudo apt install gnupg gnupg-l10n gnupg-utils gpg gpg-agent
Install required packages
Install the required packages

These packages are required for the repository to use the GPG key. This ensures that the packages are signed and do not compromise the system.

Once the installation is complete, the repository’s GPG key must be added:

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

Then, add the MongoDB repository to the system’s software sources.

:~$ echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

Now proceed to install MongoDB using these two commands:

:~$ sudo apt update
:~$ sudo apt install mongodb-org
Install MongoDB on Debian10
Install MongoDB on Debian10

After confirming the process, the installation will start.

By default, MongoDB is not started when it is installed. This can be checked with the following command:

:~$ sudo systemctl status mongod
MongoDB not running
MongoDB not running

As you can see the program is installed, but it is not running. Therefore, it has to be started so that you can use it.

:~$ sudo systemctl start mongod

Also, you can have MongoDB start-up along with the system. In production environments, this can be quite useful.

:~$ sudo systemctl enable mongod

Now, check the status of the service again.

:~$ sudo systemctl status mongod
MongoDB running on Debian 10

As you can see in the image, MongoDB is already running.

Using MongoDB on Debian for the first time

The fastest and easiest way to access the MongoDB console where the work is done is by using the mongo command

:~$ mongo
MongoDB shell
MongoDB shell

One of the first things to do is to create a new database and a user to own it.

In MongoDB, there is no CREATE command as you find in MySQL or MariaDB, but the use command allows you to change the database and in case the database does not exist it creates it.

In this case, I will create a database called firstdatabase.

> use firstdatabase;

Now, it’s time to create a new owner for the database. This is to further protect access to the data.

db.createUser(
... {
... user:"user",
... pwd:"imaginelinux",
... roles:["dbOwner"]
... }
... )
Successfully added user: { "user" : "user", "roles" : [ "dbOwner" ] }
Create a new database and user
Create a new database and user

These are test data, you can change them to whatever you like. Remember to assign a fairly strong password for the user.

Then, you can log out and log in exclusively to that database using the newly created user. This significantly increases security.

> exit

To log in, use the following command:

:~$ mongo -u [user] -p [password] 127.0.0.1:27017/[database]

By default, MongoDB connects using port 27017. This can be changed.

In this case, the command would look like this

:~$ mongo -u user -p "imaginelinux" 127.0.0.1:27017/firstdatabase
Using MongoDB on Debian
Using MongoDB on Debian

Create a new Collection and Document with MongoDB on Debian

The next step will be to create a new collection. A collection is something like a folder that is where the records in MongoDB are called documents will be stored.

To create one with the name firstcollection and that supports 10000 documents, just execute the following command:

> db.createCollection("firstcollection", {capped:true,size:6142800,max:10000})
{ "ok" : 1 }

And then, you can add a record with the structure you want. That’s the brilliant thing about MongoDB.

> db.firstcollection.insertOne({Id: "1",name: "luke",age: "22",lastname: "simpson"})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("5e8a3f46fa840ebb118648a3")
}

If you wish to consult the data of a document, the function find is the right option. This is how it is used:

:~$ db.firstcollection.find({Id: "1"})

In this case, all the data with the value “1” in the Id will be searched.

The previous command generates the following screen output:

{ "_id" : ObjectId("5e8a3f46fa840ebb118648a3"), "Id" : "1", "name" "luke", "age" : "22", "lastname" : "simpson" }

Finally, you can check the installed version of MongoDB

:~$ mongo --version
MongoDB version
MongoDB version

In this case, version 4.2.5 has been installed.

Conclusion

In order to carry out a programming project, all the necessary tools must be known. The database manager is a very important component of an application since it will manage the data of the application.

As time goes by, new technologies and applications are discovered that can improve the ones that currently exist. This is the case of MongoDB that in many cases achieves things in a better way than traditional solutions such as MySQL or MariaDB.

MongoDB combines ease of use with robustness and something very important in our days as it is the high availability. All this combined with an extraordinary performance, gives us a very competitive solution to develop applications.

Have you used MongoDB in your projects? Have you used a NoSQL database?

Scroll to Top