Site icon DesignLinux

How to Install MongoDB on Ubuntu 20.04

How to Install MongoDB on Ubuntu 20.04

MongoDB is an opensource, cross-platform NoSQL database server. It uses JSON document to store data and fields can be vary from compare to other. In this tutorial, we will explain how to install latest version of MongoDB Community Edition on Ubuntu 20.04.

MongoDB is popular for handling large amounts of data due to its performance, scalability and high availability. It doesn’t require a predefined schema or data structure, it might be different. It is different from the traditional table-based SQL databases like MySQL and PostgreSQL. For more details check official installation page of MongoDB’s documentation.

Install MongoDB on Ubuntu

MongoDB is not available in the default Ubuntu repositories. We need to enable manually the official MongoDB repository and then install the packages.

Perform the following steps as root or user with sudo privileges.

Step 1 – Install Dependencies

First of all, install additional packages which are require, run following command:

sudo apt update
sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Step 2 – Import GPG Key

After that, you need to add the MongoDB GPG key to your system using wget:

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'

Currently, at the time of writing this article, the latest version of MongoDB is 4.4. It may different while you are installing.

Step 3 – Install Meta Package

Now, update the packages list and install mongodb-org meta-package:

sudo apt update
sudo apt install mongodb-org

Below packages will be installed on the system as a part of the mongodb-org package:

Step 4 – Start MongoDB Service

Now, you should start the MongoDB service and enable it for start on boot:

sudo systemctl enable mongod --now

Step 5 – Verify Installation

At last, it’s time to verify that installation is completed successfully. Login to mongo tool and print connection status:

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

It should look like this:

MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("2af3ab0e-2197-4152-8bd0-e33efffe1464") }
MongoDB server version: 4.4.0
{
  "authInfo" : {
    "authenticatedUsers" : [ ],
    "authenticatedUserRoles" : [ ]
  },
  "ok" : 1
}

If in ok field value is 1 then it indicates success.

Configure MongoDB

Generally, default configuration settings are enough for most users. But it’s advised to change few settings for production environment, specially security section. The MongoDB configuration file mongod.conf is located in the /etc directory. Edit the mongod.conf file and uncomment the security section and enable authorization:

security:
   authorization: enabled

It will enables Role-Based Access Control (RBAC) that regulates users access to database resources and operations. If this option is disabled, each user can access all databases and perform any action.

After making changes you need to restart the mongod service for changes to take effect:

sudo systemctl restart mongod

You can get more information about the configuration options, visit the Configuration File Options documentation page.

Creating Administrative MongoDB User

You will need create an administrative user account to access and manage the MongoDB instance if you enabled the MongoDB authentication. You can access just typing:

mongo

From inside the MongoDB shell, type the following command to connect to the admin database:

use admin
switched to db admin

Issue the following command to create a new user named admin_user with the userAdminAnyDatabase role:

db.createUser(
   {
     user: "admin_user", 
     pwd: "your_password", 
     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
   }
 )
 Successfully added user: {
     "user" : "admin_user",
     "roles" : [
         {
             "role" : "userAdminAnyDatabase",
             "db" : "admin"
         }
     ]
 }

You can exit from mongo shell by typing:

quit()

Now, access the mongo shell using the administrative user which you have created previously and check the changes:

mongo -u admin_user -p --authenticationDatabase admin

It will prompt to enter password. On success connection select the admin database:

use admin
switched to db admin

You can print list of users by typing:

show users
{
     "_id" : "admin.admin_user",
     "userId" : UUID("dfd92e5s-ds69-3er4-a5c6-654sd0c32e8c"),
     "user" : "admin_user",
     "db" : "admin",
     "roles" : [
         {
             "role" : "userAdminAnyDatabase",
             "db" : "admin"
         }
     ],
     "mechanisms" : [
         "SCRAM-SHA-1",
         "SCRAM-SHA-256"
     ]
 }

Conclusion

In this guide, you learned how to install MongoDB 4.4 on Ubuntu 20.04. You can get more details about by visit MongoDB Manual.

Feel free to leave comment, if you have any question or suggestion.

Exit mobile version