How to Install MongoDB on Ubuntu 20.04

MongoDB is a source-available cross-platform. It is a modern-day document-oriented database program. The utility is classified as a NoSQL database that employs JSON-like documents alongside schemas. You can install MongoDB on Ubuntu 20.04 and seamlessly deal with several tasks.

The tool MongoDB is developed and maintained by the well-known MongoDB Inc. and licensed under Server Side Public License (SSPL). It was developed in early 2007 by 10gen as a platform and service product (which provides a cloud platform for developers).

MongoDB: The Basics

To understand how MongoDB works, you need to have a clear vision of a NoSQL database. You should also know how it is different from a SQL database.

First, SQL databases were relational, which would store data in the form of tables (row and tables). Each table would have a unique primary key or ID to distinguish one row from another. SQL database uses SQL queries to help edit information on the database. They have a fixed schema, defined in the database’s early design, which is usually rigid.

NoSQL stores data in document form, just like JSON (key-value or node and edges pair instead of row and columns). Their primary focus is indeed a dynamic schema. It is excellent for unstructured data, and they are highly scalable.

The Advantages

  • Flexible data models
  • Horizontal scaling
  • Fast queries
  • Easy for developers

Main features of MongoDB

  • Ad-hoc queries
  • Indexing
  • Replication
  • Load balancing
  • File storage
  • Transactions

How to Install MongoDB on Ubuntu 20.04

Now that you’ve got through all the information you need, let’s find out how you can install MongoDB on Ubuntu 20.04 and configure it.

Pre Requisites

Checking for updates in your system is always a good practice. Launch the Terminal using the “Ctrl+Alt+T” command and invoke the following pair of commands:

$ sudo apt update
$ sudo apt upgrade

Also, we would like to add that having your sudo privileges sorted is crucial before installing MongoDB.

Installing MongoDB Using the Official Repository

There are mainly two brilliant ways to install MongoDB on your system. The first one involves the default Ubuntu repository. While the second is by getting yourself the dedicated package repository of MongoDB. You can add it to the apt resources in order to get the job done.

Step 1: Import the Public GPG Key

The first task is importing the public GPG key of the latest version of MongoDB. Remember, it can be different by the time you read this tutorial. Therefore, we highly suggest you check their official website, so you can find the appropriate key.

On their website, look for the serve-x.x.asc file with the corresponding current latest version.

Step 2: Grab the Latest MongoDB

At this time, the newest version of MongoDB is 6.0. So we will select that and use the command below to import the GPG key for 6.0:

install MongoDB

 Hit ”Enter” and the execution of the command will give you an ok output, which is what we need here. 

Step 3: Grabbing the Mongo Org Package

Now, we need the mongodb-org package to install the latest version of MongoDB. To do this, you have to add the official MongoDB repository to your system using the command below.

Input:

getting mongo package

Once done, hit the “Enter key” and continue.

Step 4: Update the Local Package Database

Now update the local package database and install the mongodb-or package by running the commands mentioned below one at a time.

Input:

update local package

Step 5: Enable Mongo DB Service

Once the MongoDB utility is installed, start and enable the MongoDB service on your system using the following command:

enable mongodb service

If these commands are not working as expected, use the sudo keyword and invoke them with administrative privileges.

Step 6: Verify the Service Status

Once this is done, you can verify whether MongoDB is running or not by using the following command.

Input:

install mongodb

You should get the output as seen below if everything goes well.

verify the status

You can see the default port of MongoDB is 27017 and is running on the local host 127.0.0.1

Configuring MongoDB

You can work with the default configuration, and the same will work fine if it satisfies your needs. However, as a developer, you should be well aware of some extra features and prepare for more.

The configuration file of MongoDB is located in the /etc directory. To bring the necessary edits, invoke the following command. Here we will use nano editor for this purpose.

Input:

configure mongodb

Enable Authorization

Once the .conf file is open, you can go to the bottom of the file and uncomment the security option by removing the # symbol. After that, type “authorization: enables” like this:

install mongodb and authorize

Now save it and restart the MongoDB with the following command. 

Input:

install mongodb and restart

What it will do is make sure the changes are successfully applied.

Cheating MongoDB Admin

Now we will create an admin user with access to the MongoDB database. To access the MongoDB shell, use the mongo command.

Input:

$ mongo

It will launch the MongoDB shell. Now, to access the admin database, use the following command.

Input:

$ use admin

Now use the following command to create a new user and password with the role “userAdminAnyDatabase.”

install mongodb

In the “user” section, you must type what you want (here UserName). The same goes for the “pwd” section. Remember, you should use a solid and secure password for this purpose. Once done, type quit() to exit the MongoDB shell.

Verify Admin Authentication

To verify the Admin authentication you provided earlier, access the admin account with the command mentioned below.

Syntax:

$ mongo -u [username] -p - - authenticationDatabase admin

Hit the “Enter” key and the MongoDB shell will ask for the password. Input the password (the one you set above) and access the admin database. You can sort the access by invoking the following command.

Input:

$ use admin

Managing MongoDB

You can start the MongoDB service by running the following command:

manage mongodb

To restart the service when you change settings, type in the following command:

restart mongodb

And that’s it for this guide. We hope you were able to successfully install the latest version of the MongoDB on your Ubuntu system, and that you learned how to manage different services.

If this guide helped you, please share it.

Leave a Reply
Related Posts