How to install MongoDB on Ubuntu 20.04

0 Shares
0
0
0
0

Introduction

MongoDB is a document database used in many modern web applications. It is classified as a NoSQL database because it does not rely on the traditional table-based structure of relational databases.

Instead, it uses JSON-like documents with dynamic schemas, meaning that unlike relational databases, MongoDB does not require a predefined schema before adding data to the database. You can change the schema whenever and as many times as necessary, without having to create a new database with the updated schema.

In this tutorial, you will install MongoDB on an Ubuntu 20.04 server, test it, and learn how to manage it as a systemd service.

Prerequisites

To follow this tutorial, you will need the following:

  • An Ubuntu 20.04 server. This server should have a non-root administrative user and a firewall configured with UFW. Set this up by following our initial server setup guide for Ubuntu 20.04.
  • Quickly set up a MongoDB database using DigitalOcean Managed Databases. Let DigitalOcean focus on scaling, maintaining, and updating your database.

Step 1 — Installing MongoDB

The official Ubuntu package repositories include a stable version of MongoDB. However, at the time of writing this tutorial, the version of MongoDB available in the default Ubuntu repositories is 3.6, while the latest stable version is 4.4.

To get the latest version of this software, you need to add the MongoDB package repository to your APT sources. Then you can mongodb-org, install a meta package that always points to the latest version of MongoDB.

To get started, import the GPG public key of the stable version of MongoDB by running the following command:

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

Note: If you plan to use a version of MongoDB other than 4.4, make sure that in the URL part of this command, 4.4 Change it to your desired version.

This command returns OK if successful:

Output OK

To ensure that the key was added correctly, you can use the following command:

apt-key list

This command will show the MongoDB key 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 <packaging@mongodb.com>
. . .

At this point, your APT installer still doesn't know where to install the package. mongodb-org Find.

Run the following command to create a file in the directory sources.list.d By name mongodb-org-4.4.list To be created:

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 line gives APT all the necessary information about the source.

After running this command, update your server's local package list:

sudo apt update

Then install MongoDB:

sudo apt install mongodb-org

Upon request, Y Press and then ENTER Tap to confirm.

Step 2 — Start the MongoDB service and test the database

The process of installing MongoDB as a managed service systemd However, this process does not automatically start the service.

Run the following command to start MongoDB:

sudo systemctl start mongod.service

Check the service status:

sudo systemctl status mongod

To enable the MongoDB service at boot time:

sudo systemctl enable mongod

To further verify database operations, connect and run a diagnostic command:

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

The output confirms that the server is working properly.

Step 3 — Managing the MongoDB Service

You can use standard commands systemctl Manage the MongoDB service:

  • Check the status: sudo systemctl status mongod
  • Service stop: sudo systemctl stop mongod
  • Service start: sudo systemctl start mongod
  • Restart the service: sudo systemctl restart mongod
  • Disable autostart: sudo systemctl disable mongod
  • Re-enable autostart: sudo systemctl enable mongod

Result

In this tutorial, you added the official MongoDB repository to APT, installed the latest version of MongoDB, tested its performance, and ran a few commands. systemctl You practiced.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like