How To Install MongoDB on Linux (Ubuntu) – Step By Step Guide

In nowadays, MongoDB getting to much popularity among the developers and database administrators. Unlike MySQL, MongoDB is a NoSQL database. It offers various rich features such as high performance, high availability, and automatic scaling enterprise database.  Since MongoDB is a NoSQL database, so you cannot use Structured Query Language to insert and retrieve data. In addition, unlike MySQL or Postgres, it does not store data in form of tables. With MongoDB, the data is stored in a “document” structure using special format called JSON. When it comes to open source, nothing is much preferred than using a Linux variants. The most popular Linux variant used by open source developers is Ubuntu. Here, we are going to discuss the step by step guide how to install MongoDB on Ubuntu Linux.

Installing MongoDB on Ubuntu/Linux

In order to install MongoDB on Ubuntu Linux, you need to perform the following steps:

1. Importing the Public Key

GPG keys are used to ensure consistency and authenticity of the packages you want to install. It is always recommended to use only authorized packages. In order to import hte GPG keys for MongoDb package, execute the following command on a Ubuntu machine:

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

2. Creating Source List File for MongoDB

After adding the GPG key, next create a MongoDB list file under the /etc/apt/sources.list.d/ directory. For this, you need to execute the following command:

echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Install MongoDB on Linux

3. Installing the MongoDB Packages

Now, you are ready to install the MongoDB packages. To install the MongoDB on Ubuntu server, execute the following commands:

sudo apt-get update
sudo apt-get install -y mongodb-org

Installing MongoDB Linux

4. Creating MongoDB Service Script File

Once the MongoDB packages are installed, you can start the MongoDB service. However, in the latest version of Ubuntu such as Ubuntu 16.04, you may get an daemon error. To avoid this, you need to create a new mongodb systemd service file under the “/lib/systemd/system” directory. For this, you need to execute the following command:

sudo vim /lib/systemd/system/mongod.service.

The above command will open a new file in which you need to insert the following script carefully.

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

Starting MongoDB Service on Ubuntu 16.04 error

5. Starting and Enabling MongoDB Service

In the next step, you need to update the systemd services using the following command:

sudo systemctl daemon-reload

Now you are ready to start the MongoDB service. Use the following commands to start the MongoDB server and enable it for starting at each boot time.

sudo systemctl start mongod
sudo systemctl enable mongod

Starting MongoDB service

6. Verifying MongoDB Ports

In the above figure, you can see the MongoDB service is started and running fine. Here you can also check whether the MongoDB has been started on port 27017 with the netstat command.

sudo netstat -plntu

7. Connecting MongoDB Terminal

Once the installation is done, you can connect to MongoDB terminal and execute the commands for the tasks you want to do. In the following figure, you can see the MongoDB terminal is connected and ready to be used.

sudo mongo

Connecting MongoDB Terminal

In this post, we have explained how to install MongoDB on Ubuntu Linux machine. Hope, you have enjoyed it. If you face any issue or have any query, please let us know.

Posted in ProTechGurus

Leave a Reply

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

*