Programmers use different databases in web and application development. SQL or NoSQL-based databases have different uses according to system requirements and programmers’ preferences. MongoDB is one of these databases. MongoDB is one of the most popular No SQL databases, which has a flexible structure and is mostly used in projects with large volumes of data. This article will teach you How to Install and run MongoDB on Ubuntu 20.10, 21.04 step by step.

Learn Install and Run MongoDB on Ubuntu Linux

What is MongoDB?

MongoDB is a free, open-source platform that works with the Document-Oriented data model and can be used on Windows, Mac, and Linux. The data values stored in MongoDB are used with both Primary Key and Secondary Key. MongoDB contains a set of values. These values are in the form of documents that contain different types of data of different sizes. This allows MongoDB to store complex structured data such as hierarchical or array data.

MongoDB Features

Here are some features of MongoDB:

– MongoDB is flexible and scalable and meets many business needs.

– This database uses Sharding to divide data and better manage the system. Sharding means fragmentation And is done for top loading of the network. The database is divided into several subsections to facilitate the process of responding to requests from the server.

Data access and the processing time is very fast. Because the data can be accessed with two primary and secondary keys and each field can be keyed.

– Replication is another important feature of MongoDB. A copy of the original data is provided and other parts of the database system are stored in this technique. If this data is lost or corrupted, the copied data will be used as the original and alternative data.

How does MongoDB work?

In MongoDB, data is stored as a document. Each document is of type Binary JSON or BSON and has key and value fields. To execute the code written in MongoDB, you must proceed through Mongo Shell. MongoShell is an interactive database and programmer interface that allows query sending and update data.

How to Install MongoDB on Ubuntu 20.10

First, your Linux VPS Ubuntu 20.10 server must have a non-root executable user and a UFW configured firewall. You need to install the latest version of MongoDB software. To do this, you should put the dedicated MongoDB package repository in your APT resources. You can now install MongoDB.

Enter the following command to enter the public GPG key for the latest stable version of MongoDB:

curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

cURL is a command-line tool used in many operating systems to transfer data. cURL reads the data stored in the URL and prints the content to the system output. If the GPG server crashes or cURL is unable to connect to the GPG server, cURL will not add the resulting error code to your list of valid keys.

If the key is added successfully, you will see the following command key:

Output
OK

To double-check how to add the key correctly, you must enter the following command:

apt-key list

You can return the MongoDB key somewhere in the output by entering the following command:

Output
/etc/apt/trusted.gpg
--------------------
pub rsa4096 2019-05-28 [SC] [expires: 2024-05-26]
2069 1EEC 3521 6C63 CAF6 6CE1 6564 08E3 90CF B1F5
uid [ unknown] MongoDB 4.4 Release Signing Key <[email protected]>
. . .

The APT looks for online sources of packages for download and installation in either sources.list file or sources.list.d directory. The sources.list.d directory allows you to add sources.list them as separate files. sources.list Lists the active sources of APT data. So that your first preferred sources are at the top of the list and a source is mentioned in each line.

Creates a file in the sources.list.d directory named mongodb-org-4.4.4 by entering the https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

In this step, to find the mongodb-org package, enter the following command and then update the local server package index:

sudo apt update

After activating the repository, install the mongodb-org package by entering the following command:

sudo apt install mongodb-org

In this step, to confirm the installation of the package, you must select Y and then ENTER.

How to Run MongoDB on Ubuntu 20.10

MongoDB is now installed on your system but cannot be used. At this point, you start the MongoDB service and test the databases.

You can manage MongoDB with various systemctl commands. Start Daemon MongoDB and activate it by entering the following command:

sudo systemctl start mongod.service

Now you should check the status of the service. Note that this command does not include .service in the service file definition. It is not necessary to enter systemctl in the following command. Because if it does not already present, it will automatically add to any argument you pass:

sudo systemctl status mongod

The output will look something like the following and indicate that the service is active:

mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2020-06-09 12:57:06 UTC; 2s ago
Docs: https://docs.mongodb.org/manual
Main PID: 37128 (mongod)
Memory: 64.8M
CGroup: /system.slice/mongod.service
└─37128 /usr/bin/mongod --config /etc/mongod.conf

You must now confirm “the service is running as expected“.

Activate the MongoDB service by entering the following command to start it at startup:

sudo systemctl enable mongod

To check if the installation is successful, use the following command to connect to the MongoDB database server and extract the current version, server address and port:

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

In the output below, the value of 1 for the ok field in the response indicates that the server is working as expected:

Output
MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1dc7d67a-0af5-4394-b9c4-8a6db3ff7e64") }
MongoDB server version: 4.4.0
{
"authInfo" : {
"authenticatedUsers" : [ ],
"authenticatedUserRoles" : [ ]
},
"ok" : 1
}

How to Configure MongoDB on Ubuntu 20.10

Like other Ubuntu services, you can manage MongoDB using standard systemctl commands. The systemctl status command checks the status of the MongoDB service: 

sudo systemctl status mongod

You can stop the service with the following command:

sudo systemctl stop mongod

Run the following command to start the service again:

sudo systemctl start mongod

You can restart the running server with the following command:

sudo systemctl restart mongod

To disable automatic startup, enter the following command:

sudo systemctl disable mongod

Now enter the following command to re-enable it to startup:

sudo systemctl enable mongod

Conclusion

At the beginning of this article, we introduced MongoDB and listed its features. Then we tried to teach you how to add MongoDB to an APT instance and install MongoDB.