How to install MongoDB on ubuntu 16.04 and 18.04

Introduction

MongoDB is a document-based database design ease for developing and scaling. Data stored in MongoDB are composed of field and value pairs. It is similar to JSON objects. We can also store the nested values of the field that can be documents, arrays, and arrays of documents.

In this tutorial, we will learn how to install MongoDB via terminal. We will also learn how to install a stable or specific version of MongoDB.

Note: The mongodb-org package is officially maintained by the MongoDB Inc and keeps you up to date with the latest updates. The installation procedure uses a mongodb-org package.

MongoDB that comes by default with the ubuntu package is not supported by MongoDB Inc. If you have already installed MongoDB from the ubuntu package in your machine. It can conflict with mongodb-org which we are going to install in this tutorial.

To check if MongoDB is already installed on your machine. You can use sudo apt list --installed | grep mongodb. if it shows already installed you can purge and remove the MongoDB before start installing the mongodb-org.

You can completely remove and purge MongoDB with the following command before starting to install mongodb-org

sudo apt remove mongodb
sudo apt purge mongodb

We will start with Installing the latest version of MongoDB. So let's get started then.

Step 1. Import the MongoDB repository

Import the public key to use the package management system. Enter the following command in your terminal to add

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

The above command response will be OK.

Once done with importing public key. Add the following command to create the repository /etc/apt/sources.list.d/mongodb-enterprise.list for MongoDB. 

For Ubuntu 16.04:

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

For Ubuntu 18.04:

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

Once you are done with adding the repository. Now update your local repository with the following command.

sudo apt update

Step 2. Install the MongoDB package

In this section, we will install a stable and specific version of MongoDB.

# Install the stable version of MongoDB

To install the latest stable version of MongoDB enter the following command.

sudo apt-get install -y mongodb-org

# Install a specific version of MongoDB

To install specific version of MongoDB you have to specify each component version number individually. Enter the following command to install.

sudo apt-get install -y mongodb-org=4.2.0 mongodb-org-server=4.2.0 mongodb-org-shell=4.2.0 mongodb-org-mongos=4.2.0 mongodb-org-tools=4.2.0

Step 3. Test the MongoDB installation

By default, MongoDB port is 27017. To verify if MongoDB is running on port 2707 use the below command.

netstat -plntu | grep 27017

You will get the following response from the above command.

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      -

To verify MongoDB status enter the following command

sudo systemctl status mongod.service

The above code will output the following response.

To enable MongoDB to autostart when the system starts. Use the following command.

sudo systemctl enable mongodb

Stop MongoDB

sudo systemctl stop mongodb

Restart MongoDB

sudo systemctl restart mongodb

Step 4. Configure and connect to MongoDB

Next, we will set up the and create a root user and connect the MongoDB using shell

Enter the following command to connect to MongoDB

mongo

Once you enter the mongo shell. use the admin collection by typing the following command.

use admin

Now create a root user with the following command

db.createUser({user:"admin", pwd:”somepwd", roles:[{role:"root", db:"admin"}]})

A user with the role root can manage the users of all the databases.

Now enable auth and restart the service. Edit the MongoDB config file under /etc. Find the commented security keyword and replace it with the following code.

#/etc/mongod.conf

security:
 authorization: "enabled"

Note: After making the above changes restart the server with sudo systemctl restart mongod.service command.

The above code will enable auth. To log in with created username and password. Enter the following command

mongo -u root -p somepwd

done :)