Provides all the steps required to install MongoDB 4 on Ubuntu 18.04 LTS. The same steps can be followed to install MongoDB 3. It also provides steps to access MongoDB from MongoDB Shell.

How To Install MongoDB on Ubuntu
How To Install MongoDB on Ubuntu
September 24, 2019

MongoDB is the most popular NoSQL database server and it can be used to store JSON documents. MongoDB is a document-oriented, open-source and platform-independent Database Management System (DBMS). This tutorial provides all the steps required to install and setup the MongoDB 4 Community Server on Ubuntu 18.04 LTS. The steps should be the same for other versions of MongoDB including MongoDB 3 and Linux systems.

This tutorial provides the steps required to install MongoDB on the server version of Ubuntu. It uses a package manager to install the latest MongoDB 4.2 Community Edition on Ubuntu 18.04 LTS. You can also follow the Ubuntu Desktop Tutorial published by Tutorials24x7 to install MongoDB on the desktop version for development purpose.

At the time of writing this tutorial, the most recent version of MongoDB i.e. 4.2 and onwards provides packages only for the Ubuntu 16.04 LTS and Ubuntu 18.04 LTS 64-bit version.

Remove the Previous Installation

We can check the previous installation and remove it in case it's already installed on the system using the commands as shown below.

# Check previous installation
sudo apt list --installed | grep mongodb

# Take the backup of existing databases

# Stop the server
sudo service mongod stop

# Completely remove previous installation
sudo apt remove mongodb
sudo apt purge mongodb

You can use the below mentioned commands to remove the MongoDB installed on the desktop version using the installer downloaded from the official website.

# Remove server
sudo apt remove mongodb-org-server

# Remove shell
sudo apt remove mongodb-org-shell

We can also clean the files of the previous installation using the commands as shown below.

# Remove residual files
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

Import Public Key

In this step, we will import the MongoDB public GPG Key using the command as shown below.

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

# Result
OK

The above command will show the OK message after adding the Key.

Create List File

Create the list file for Ubuntu 18.04 LTS using the command as shown below.

# Create list file using nano editor
sudo nano /etc/apt/sources.list.d/mongodb-org-4.2.list

# Copy and paste the content to the list file
deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse

You can also create the file directly as shown below.

# Direct method
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

Now refresh the packages index using the command as shown below.

# Refresh the packages index
sudo apt-get update

Install MongoDB

Now install the latest MongoDB server using the command as shown below.

# Install MongoDB
sudo apt-get install -y mongodb-org

# Installtion last few messages
Setting up mongodb-org-shell (4.2.0) ...
Setting up mongodb-org-mongos (4.2.0) ...
Setting up mongodb-org-tools (4.2.0) ...
Setting up mongodb-org-server (4.2.0) ...
Setting up mongodb-org (4.2.0) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

We can see that the above command installed the packages mongodb-org, mongodb-org-server, mongodb-org-mongos, mongodb-org-shell, and mongodb-org-tools. The command also starts the MongoDB database service. We can optionally avoid the auto-upgrade of the currently installed MongoDB packages using the commands as shown below.

# Keep the current version installed on system upgrade

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

Verify Installation

In this step, we will verify the installation using the commands as shown below.

# Check version
mongod -version

# Output
db version v4.2.0
git version: a4b751dcf51dd249c5865812b390cfd1c0129c30
OpenSSL version: OpenSSL 1.1.1 11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
distmod: ubuntu1804
distarch: x86_64
target_arch: x86_64

This confirms that we have successfully installed the MongoDB server.

Server Commands

We can start and stop the server as system service using the commands as shown below.

# Start the Service
sudo service mongod start

# Restart the Service
sudo service mongod restart

# Stop the Service
sudo service mongod stop

# Check status
sudo service mongod status

We can also enable or disable the MongoDB server to start automatically with the server. The default behavior is to start the server as part of the installation process.

# Disable auto-start
sudo service disable mongodb

# Enable auto-start
sudo systemctl enable mongodb

Getting Started With MongoDB

Start the MongoDB Shell as shown in Fig 1.

MongoDB Shell Launch

Fig 1

It shows the warning message indicating that the RBAC is not enabled. MongoDB provides options to create the Authentication Database to store user details and privileges across different databases. The same user having appropriate permissions can act on multiple databases using the Role-Based Access Control (RBAC). The RBAC provided by MongoDB governs access to a MongoDB system.

Now list the databases as shown below.

# List Databases
show databases

# Output
admin 0.000GB
config 0.000GB
local 0.000GB

We can see that MongoDB has already created three databases i.e. admin, config, and local. Now we will add the user admin with root role to the admin database using the commands as shown below.

# Switch to admin database
use admin

# Create the admin user with root role
db.createUser( { user: "admin", pwd: "<password>", roles: [ "root" ] } )

# Output
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }

# Exit the Shell
exit

This is how we can add admin users to the admin database and assign a role to the user. We can start creating the databases and add data either using the shell or programmatically using the preferred language.

Secure The Server

We can further tighten the security by enabling the MongoDB authentication using the commands as shown below.

# Open the config
sudo nano /lib/systemd/system/mongod.service

# Update the ExecStart value
ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf

Now save the file and reload the settings using the command as shown below.

# Reload system service
sudo systemctl daemon-reload

# Restart the server
sudo service mongod restart

Now we can connect with MongoDB using the shell as shown below.

# Connect securely
mongo -u admin -p <password> --authenticationDatabase admin

It won't show the warning message about Access Control as we saw in Fig 1.

MongoDB Shell Secured

Fig 2

Now we have secured the Shell and only the users with valid role and permissions can access the database.

Summary

We have successfully installed the MongoDB Server and MongoDB Shell. We have also connected with the server using shell and added an admin user with the root role.

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS