How to install MongoDB on Debian 10?

0
811

Hi, friends in this post I’ll show you how to install MongoDB on Debian 10.

Nowadays many projects are developed using SQL type databases, but it’s more and more common to see NoSQL type databases becoming more and more important. From this type of database, MongoDB stands out a lot.

MongoDB is one of the most popular NoSQL database managers in the world. Unlike managers like MySQL or PostgreSQL, it saves your records in documents so they are more understandable and operations faster to do. As I said, it is incredibly popular with developers.

So let’s get to work.

Install MongoDB on Debian 10

MongoDB has a special repository for Debian 10 that has recently been updated. This will help us to have the latest version available and keep it updated using APT.

So, open a terminal or connect to your server using SSH.

Then, install gnupg which is a package that is necessary to add the MongoDB repository.

:~$ sudo apt install gnupg
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  dirmngr gnupg-l10n gnupg-utils gpg gpg-agent gpg-wks-client gpg-wks-server gpgconf gpgsm libassuan0 libksba8 libnpth0 pinentry-curses
Suggested packages:
  pinentry-gnome3 tor parcimonie xloadimage scdaemon pinentry-doc
The following NEW packages will be installed:
  dirmngr gnupg gnupg-l10n gnupg-utils gpg gpg-agent gpg-wks-client gpg-wks-server gpgconf gpgsm libassuan0 libksba8 libnpth0 pinentry-curses
0 upgraded, 14 newly installed, 0 to remove and 0 not upgraded.
Need to get 7089 kB of archives.
After this operation, 14.9 MB of additional disk space will be used.
Do you want to continue? [Y/n]
1.- Installing GNUpg on Debian 10
1.- Installing GNUpg on Debian 10

Once the program is installed, we can import the GPG key from the MongoDB repository. To do this, use the following command:

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

Once the GPG key is added, you can add the MongoDB repository itself. This is done by running the following command:

:~$ 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
2.- Adding the MongoDB repository
2.- Adding the MongoDB repository

Aftert that, refresh APT software sources.

:~$ sudo apt update

Finally, install MongoDB on Debian 10 using this command:

:~$ sudo apt install mongodb-org
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libcurl4 mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  libcurl4 mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
0 upgraded, 6 newly installed, 0 to remove and 51 not upgraded.
Need to get 97.7 MB of archives.
After this operation, 295 MB of additional disk space will be used.
Do you want to continue? [Y/n]
3.- Installing MongoDB on Debian 10
3.- Installing MongoDB on Debian 10

Now we work a little with the application.

Using MongoDB

First, you have to start the MongoDB service. Because at this point, the program is installed but not running. To do this, run the following command:

:~$ sudo systemctl start mongod

Once it’s started, you can check the status of the service:

:~$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-03-06 07:34:05 EST; 22s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 1882 (mongod)
   Memory: 82.3M
   CGroup: /system.slice/mongod.service
           └─1882 /usr/bin/mongod --config /etc/mongod.conf
Mar 06 07:34:05 osradar systemd[1]: Started MongoDB Database Server.
4.- Working with the MongoDB services
4.- Working with the MongoDB services

It’s best to get MongoDB started with the system:

:~$ sudo systemctl enable mongod

Now we can really use it.

Once MongoDB is running, we have to access the shell where we work most of the time. You access it with the following command:

:~$ mongo
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("64377c80-4b9d-4909-93d2-374023c98ee9") }
MongoDB server version: 4.2.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2020-03-06T07:34:06.314-0500 I  STORAGE  [initandlisten] 
2020-03-06T07:34:06.314-0500 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-03-06T07:34:06.314-0500 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-03-06T07:34:07.288-0500 I  CONTROL  [initandlisten] 
2020-03-06T07:34:07.289-0500 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-03-06T07:34:07.289-0500 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-03-06T07:34:07.289-0500 I  CONTROL  [initandlisten] 
2020-03-06T07:34:07.289-0500 I  CONTROL  [initandlisten] 
2020-03-06T07:34:07.289-0500 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-03-06T07:34:07.289-0500 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-03-06T07:34:07.289-0500 I  CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

>
5.- Access to the MongoDB shell
5.- Access to the MongoDB shell

Once inside the shell, the first thing we have to do is create a new database which is where you will work.

To do this there is no create command but use that serves to switch between databases. If the database does not exist, it will be created automatically.

:~$ use Example

You can check existing databases with the following command:

> db
Example

And then, create the user who will own the database.

> db.createUser(
... {
... user: "angelo",
... pwd: "1234",
... roles: ["dbOwner"]
... }
... 
... )
6.- Creating a new user for the database
6.- Creating a new user for the database

Remember to replace “1234” with a much stronger and more secure password. Just like the user can call himself as you want.

The main advantage of this is that we can now access MongoDB by setting a password and over a database. This increases privacy and security.

Now log out using:

> exit

And now we’re going to prove what we’ve done.

To connect to the database with the newly created username and password, you can use the following command:

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

Do not forget to replace the fields with your assigned values.

7.- Connecting to a MongoDB database
7.- Connecting to a MongoDB database

So, you can start with MongoDB.

Conclusion

MongoDB is a solid alternative to SQL databases. It is quite popular and is used in many mobile and mass deployment projects. Today you have learned to install it on Debian 10 using the official repository they provide.

Please share this post and join our Telegram channel.

LEAVE A REPLY

Please enter your comment!
Please enter your name here