Advance

How To Install MongoDB On Ubuntu 20.04

How to install MongoDB on Ubuntu 20.04
0
(0)

MongoDB, also known as Mongo, is an open-source document database used in many modern web applications. It is classified as a NoSQL database because it does not rely on a traditional table-based relational database structure.

Instead, it uses JSON-like documents with dynamic schemas, meaning that, unlike relational databases, MongoDB does not require a predefined schema before you add data to a database. You can alter the schema at any time and as often as is necessary without having to set up a new database with an updated schema. Contact the Eldernode team as a VPS provider to have your own Linux Virtual Server.

 

 

To let this tutorial work better, please consider the below Prerequisites:

A non-root user with sudo privileges.

One Ubuntu 20.04 server, to set up, follow our Initial server setup on Ubuntu 20.04.

 

How to install MongoDB on Ubuntu 20.04

Join us to show you how to install MongoDB on Ubuntu 20.04. By this, you will install MongoDB on an Ubuntu 20.04 server, test it, and learn how to manage it as a systemd service.

Step 1 – How To Install MongoDB

Firstly, to obtain the most recent version of this software, you must include MongoDB’s dedicated package repository to your APT sources. Then, you’ll be able to install mongodb-org, a meta-package that always points to the latest version of MongoDB.
To start, import the public GPG key for the latest stable version of MongoDB. You can find the appropriate key file by navigating to the MongoDB key server and finding the file that includes the latest stable version number and ends in .asc. For example, if you want to install version 4.4 of MongoDB, you’d look for the file named server-4.4.asc.

To continue, right-click on the file, and select Copy link address. Then, paste that link into the following curl command.

Note: Do not forget to replace the URL of ”https://www.mongodb.org/static/pgp/server-4.4.asc”

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

To explain more, cURL is a command-line tool available on many operating systems used to transfer data. It reads whatever data is stored at the URL passed to it and prints the content to the system’s output. In the following example, cURL prints the content of the GPG key file and then pipes it into the following sudo apt-key add – command, thereby adding the GPG key to your list of trusted keys.

Please consider that this curl the command uses the options -fsSL which, together, essentially tell cURL to fail silently. This means that if for some reason cURL isn’t able to contact the GPG server or the GPG server is down, it won’t accidentally add the resulting error code to your list of trusted keys.

This command will return OK if the key was added successfully:

Output
OK

In case you need to double-check that the key was added correctly, you can do so with the following command:

apt-key list
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]>  . . .    

While your APT installation still doesn’t know where to find the mongodb-org package you need to install the latest version of MongoDB.

But there are two places on your server where APT looks for online sources of packages to download and install:

the sources.list file and the sources.list.d directory. sources.list is a file that lists active sources of APT data, with one source per line and the most preferred sources listed first. The sources.list.d directory allows you to add such sources.list entries as separate files.

Use the command below, which creates a file in the sources.list.d directory named mongodb-org-4.4.list. The only content in this file is a single line reading 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
Now attention to this single line, which tells APT everything it needs to know about what the source is and where to find it:

deb: This means that the source entry references a regular Debian architecture. In other cases, this part of the line might read deb-src, which means the source entry represents a Debian distribution’s source code.

[ arch=amd64,arm64 ]: This specifies which architectures the APT data should be downloaded to. In this case, it specifies the amd64 and arm64 architectures.

https://repo.mongodb.org/apt/ubuntu: This is a URI representing the location where the APT data can be found. In this case, the URI points to the HTTPS address where the official MongoDB repository is located.

focal/mongodb-org/4.4: Ubuntu repositories can contain several different releases. This specifies that you only want version 4.4 of the mongodb-org package available for the focal release of Ubuntu (“Focal Fossa” being the code name of Ubuntu 20.04).

multiverse: This part points APT to one of the four main Ubuntu repositories. In this case, it’s pointing to the multiverse repository.

After running this command, update your server’s local package index so APT knows where to find the mongodb-org package:

sudo apt update

Also then, you can install MongoDB:

sudo apt install mongodb-org

To confirm that you want to install the package, press Y and then ENTER

When the command finishes, MongoDB will be installed on your system. However, it isn’t yet ready to use. Next, you’ll start MongoDB and confirm that it’s working correctly.

Step 2 – How To Start The MongoDB Service And Test The Database

Till here, you learned the installation process described in the previous step automatically configures MongoDB to run as a daemon controlled by systemd, meaning you can manage MongoDB using the various systemctl commands. However, this installation procedure doesn’t automatically start the service. Run the following systemctl command to start the MongoDB service:

sudo systemctl start mongod.service

Next, check the service’s status. Notice that this command doesn’t include .service in the service file definition. systemctl will append this suffix to whatever argument you pass automatically if it isn’t already present, so it isn’t necessary to include it:

sudo systemctl status mongod
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

After confirming that the service is running as expected, enable the MongoDB service to start up at boot:

sudo systemctl enable mongod

You can further verify that the database is operational by connecting to the database server and executing a diagnostic command. The following command will connect to the database and output its current version, server address, and port. It will also return the result of MongoDB’s internal connection status command:

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

What does the connection status do? it will check and return the status of the database connection. A 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  }

Note: The database is running on port 27017 on 127.0.0.1, the local loopback address representing the localhost. This is MongoDB’s default port number.

Step 3 – How To Manage The MongoDB Service

As mentioned previously, the installation process described in Step 1 configures MongoDB to run as a systemd service. This means that you can manage it using standard systemctl commands as you would with other Ubuntu system services.

As you know, the systemctl status command checks the status of the MongoDB service.

sudo systemctl status mongod

You can use the following command to stop the service anytime you wish.

sudo systemctl stop mongod

Also, to start the service when it’s stopped, use the command below.

sudo systemctl start mongod

And in case you need to restart the server when it’s already running:

sudo systemctl restart mongod

Note: In Step 2, you enabled MongoDB to start automatically with the server. If you ever wish to disable this automatic startup, type:

sudo systemctl disable mongod

Then to re-enable it to start up at boot, run the enable command again:

sudo systemctl enable mongod
Recommended Article: How To Install MongoDB On Ubuntu 20.04

 

 

Conclusion

In this article, you learned How to Install MongoDB on Ubuntu 20.04 by finishing the steps of this guide and now you added the official MongoDB repository to your APT instance and installed the latest version of MongoDB. Then you tested Mongo’s functionality and practiced some systemctl commands. In case you are interested in learning more about this subject, follow our related articles on How to install MongoDB 4 on Debian 10 AND How to install MongoDB 4 in CentOS 8.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

View More Posts
Marilyn Bisson
Content Writer
Eldernode Writer
We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

10 thoughts on “How To Install MongoDB On Ubuntu 20.04

    1. The default location for the MongoDB data directory is c:\data\db. So you need to create this folder using the Command Prompt.

    1. Yes, sure. Have a look at the below names:
      Amazon DynamoDB.
      Redis.
      Couchbase Server.
      CouchDB.
      ArangoDB.
      MarkLogic.
      RethinkDB.
      OrientDB.

    1. You can see the answer (long term support) respectively below:
      64-bit LTS (‘’Ubuntu’’)
      20.04 LTS (“Focal”)
      18.04 LTS (“Bionic”)

Leave a Reply

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

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+8595670151

7 days a week, 24 hours a day