Cloud & DevOps

How to Install MongoDB in ubuntu

What is MongoDB?

MongoDB is a cross-platform document-oriented database program. It is issued under the Server Side Public License (SSPL) version 1, which was submitted for certification to the Open Source Initiative but later withdrawn in lieu of SSPL version 2. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemata. MongoDB is developed by MongoDB Inc.

NoSQL (originally referring to “non SQL” or “non relational”) database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases.

Features

  • MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time
  • The document model maps to the objects in your application code, making data easy to work with
  • Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data
  • MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use
  • MongoDB is free and open-source. Versions released prior to October 16, 2018 are published under the AGPL. All versions released after October 16, 2018, including patch fixes for prior versions, are published under the Server Side Public
    License (SSPL) v1
    .

How to Install MongoDB in ubuntu

MongoDB Version

This tutorial installs MongoDB 4.0 Community Edition . For other versions of MongoDB, refer to the corresponding version of the manual.

Platform Support

MongoDB only provides packages for the following 64-bit LTS (long-term support) Ubuntu releases:

  • 14.04 LTS (trusty)
  • 16.04 LTS (xenial)
  • 18.04 LTS (bionic)

Step 1 — Adding the MongoDB Repository

MongoDB is already included in Ubuntu package repositories, but the official MongoDB repository provides most up-to-date version and is the recommended way of installing the software. In this step, we will add this official repository to our server.

Ubuntu ensures the authenticity of software packages by verifying that they are signed with GPG keys, so we first have to import they key for the official MongoDB repository.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

Next, we have to add the MongoDB repository details so apt will know where to download the packages from.

Run the following command to create a list file for MongoDB.

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

After adding the repository details, Reload local package database.

sudo apt-get update

Step 2 —Install the MongoDB packages

To install the latest stable version, issue the following

sudo apt-get install -y mongodb-org

This command will install several packages containing latest stable version of MongoDB along with helpful management tools for the MongoDB server.

Next, start MongoDB with systemctl.

sudo systemctl start mongod

You can also use systemctl to check that the service has started properly.

sudo systemctl status mongod

The last step is to enable automatically starting MongoDB when the system starts.

sudo systemctl enable mongod

The MongoDB server is now configured and running, and you can manage the MongoDB service using the systemctl command (e.g. sudo systemctl stop mongodsudo systemctl start mongod).

Step 3 — Adjusting the Firewall (Optional)

Assuming you have followed the initial server setup tutorial instructions to enable the firewall on your server, MongoDB server will be inaccessible from the internet.

To allow access to MongoDB on its default port 27017 from everywhere, you could use sudo ufw allow 27017.

sudo ufw allow from your_other_server_ip/32 to any port 27017

You can verify the change in firewall settings with ufw.

sudo ufw status

You should see traffic to 27017 port allowed in the output

Output
Status: active

To                         Action      From
--                         ------      ----
27017                      ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
27017 (v6)                 ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)

Install MongoDB in Another System, Visit official Website.

Rohit Kumar Singh

Technical writer, enthusiastic to learn new technologies and exploring the things.

Leave a Reply

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