Airzero Cloud

Next Generation Cloud !

How To Install MongoDB on Ubuntu 20.04?

- Posted in Database by

Introduction

MongoDB is a document database that is widely used in modern web applications. It has a classification.

Because it does not rely on a traditional table-based relational database, it is referred to as a NoSQL database structure.

Instead, it uses JSON-like documents with vibrant schemes, which suggests that, unlike relational databases, MongoDB, unlike other databases, does not require a predefined schema before adding data to a database. You can change the schema at any time and as often as you like without having to set it up. A new database with a new schema. In this blog, you will install, test, and learn about MongoDB on an Ubuntu 20.04 server. how it should be managed as a system service.

Prerequisites

You will need the following items to follow this blog:

There is one Ubuntu 20.04 server. This server should have a non-root administrative user and a UFW-enabled firewall.

Step 1 — Installing MongoDB

A stable version of MongoDB is available in Ubuntu's official package repositories. However, as of this writing, the latest stable release of MongoDB is 4.4, and the version available from the default Ubuntu repositories is 3.6. To get the most up-to-date version of this software, add MongoDB's dedicated package repository to your APT sources. Then you can install mongodb-org, a meta-package that always points to the most recent version of MongoDB. To begin, run the following command to import the public GPG key for the most recent stable version of MongoDB. If you like to use various versions of MongoDB than 4.4, make certain to change 4.4 in the URL portion of this command to match the version you want to install:

curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
  • A cURL is a command-line tool that can be found on many operating systems and is used to transfer data.
  • It reads the data stored at the URL passed to it and prints it to thesystem's output. It's also worth noting that this curl command includes the -fsSL flag, which tells cURL to fail silently. This means that if cURL is unable to contact the GPG server or the GPG server is unavailable, it will not inadvertently add the resulting error code to your list of trusted keys.

If the key was successfully added, this command will return OK:

Output
OK
  • If you want to double-check that the key was correctly added, use the following command:

apt-key list

  • The MongoDB key will be returned somewhere in the output:

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 
. . .

At this point, your APT installation is still unsure where to look for the mongodb-org package. The package you must install is the most recent version of MongoDB.APT looks for online sources of packages to download and install on your server in two places: the sources. list file and the sources.list.d directory. sources.list is a file that lists active APT data sources, one per line, with the most preferred sources listed first. You can add such sources to the sources.list.d directory. Entries should be listed as separate files. Run the following command to create a file called mongodb-org-4.4.list in the sources.list.d directory. This file contains only one line of text.

 deb [ arch=amd64,arm64 ] 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

This single line informs APT of everything it needs to know about the source and where to look for it: deb: This indicates that the source entry refers to a standard Debian architecture.

In other cases, this part of the line may read deb-src, indicating that the source entry represents the source code of a Debian distribution

[arch=amd64,arm64]: [arch=amd64,arm64]: 

This specifies which architectures should receive APT data. It specifies the amd64 and arm64 architectures in this case.

https://repo.mongodb.org/apt/ubuntu:

This is a URI that represents the location of the APT data.

In this case, the URI refers to the HTTPS address of the official MongoDB repository.

focal/mongodb-org/4.4: 

Ubuntu repositories may include multiple releases. This tells Ubuntu that you only require version 4.4 of the mongodb-org package to be required for the focal release.

sudo apt update

Following that, you can enable MongoDB:

sudo apt install mongodb-org

When prompted, enter Y followed by ENTER to confirm that you want to install the package. MongoDB will be installed on your system once the command is completed. It is, however, not yet ready for use. After that, you'll start MongoDB and verify that it's operational.

Step 2: Launch the MongoDB Service and Test the Database

The previous step's installation automatically configures MongoDB to run as a daemon controlled by systemd, which means you can manage MongoDB using the various systemctl commands. This installation procedure, however, does not start the service automatically. use the systemctl command:

sudo systemctl start mongod.service

Then, check the status of the service. It's worth noting that this command excludes. service from the service file definition. so it's not necessary to include it:

sudo systemctl status mongod

This command will produce the following output, indicating that the service is operational:

Output

● 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

enable the MongoDB service to start automatically at boot:

sudo systemctl enable mongod

You can confirm that the database is operational further by connecting to the database server and running a diagnostic command. The command given will connect to the database and output its current version, server address, and port. It will also return the outcome of the MongoDB internal connectionStatus command:

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

will check the database connection and return it

A value of 1 in the response's ok field indicates that the server is functioning normally:

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
}

Also, keep in mind that the database is running on port 27017 on 127.0.0.1, which is the local loopback address for localhost. Following that, we'll look at how to use systemd to manage the MongoDB server instance.

Step 3: Overseeing the MongoDB Service

you can manage services using standard systemctl commands, just like you would other Ubuntu system services.

The systemctl status command, as previously mentioned, checks the status of the MongoDB service:

sudo systemctl status mongod
  • You can cancel the service at any time by typing:
sudo systemctl stop mongod
  • To restart the service after it has been stopped, type:
sudo systemctl start mongod
  • When the server is already running, you can restart it:
sudo systemctl restart mongod

In Step 2, you enabled MongoDB to start with the server automatically. If you ever want to turn off the automatic startup, type:

sudo systemctl disable mongod

Then, to re-enable it to start up at boot, issue the following command: enable

sudo systemctl enable mongod

Systemd Essentials: Working with Services, Units, and the Journal contains more information on how to manage systemd services.

Conclusion

You added the official MongoDB repository to your APT instance and installed the latest version of MongoDB in this blog. You then practised some systemctl commands and tested Mongo's functionality.

If you have any questions about installing MongoDB on Ubuntu 20.04. Please do not hesitate to contact us. Your digital partner will be Airzero cloud.

Airzero Cloud is a leading web hosting company with a variety of useful tools. We will help you expand your business.

Email id: [email protected] enter image description here

Author - Johnson Augustine
Cloud Architect, Ethical hacker
Founder: Airo Global Software Inc
LinkedIn Profile: www.linkedin.com/in/johnsontaugustine/