How to Install MongoDB on Ubuntu 20.04 Linux

MongoDB is an open-source and free NoSQL document based database system. Nowadays, it is prevalent as most application developers are using MongoDB, which can handle big data.

As a MongoDB is a NoSQL database, It stores data in JSON-like documents where fields can vary. You also get benefits with MongoDB like it doesn’t require any predefined database schema or data structure; it can be changed over time.

In this tutorial, we will explain the process of MongoDB installation step-by-step on Ubuntu 20.04 Linux using MongoDB’s official repositories.

Prerequisites

Before starting the installation and configuration process, make sure you have Ubuntu 20.04 installed system with sudo privileged user access to execute administrative commands.

Installing MongoDB

Here we follow a few steps to install MongoDB on the Ubuntu system.

Step 1 – MongoDB official repositories enable

First, need to add the MongoDB GPG key to your system; you can use the below command to add the GPG key:

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

After importing the GPG key, need to add MongoDB repository using the following command:

$ sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse'

If the above command gives an error like the add-apt-repository command not found, then you need to install another package in your system named software-properties-common.

I will install MongoDB 4.0, so I have added repositories for MongoDB 4.0 version; you can also choose your version from the MongoDB repository page.

Step 2 – Install MongoDB

After enabling the MongoDB repository, you can update the apt package list and install the mongodb-org package like any other package we are installing on Ubuntu, as shown below:

$ sudo apt update
$ sudo apt install mongodb-org

The mongodb-org package will install all below package related to mongodb:

  • Mongodb-org-server – It is for MongoDB daemon (mongod) and init script along with configuration
  • Mongodb-org-mongos – It is for mongos daemon or service.
  • Mongodb-org-shell – The mongo shell provides a JavaScript interface to MongoDB, which is used to perform administrative tasks using the command-line.
  • Mongodb-org-tools provides several tools for mongodb to import and export data, check the data statistics, and have various other utilities.

Step 3 – Start MongoDB service

After completion of MongoDB installation, you can use the following command to start MongoDB service and enable the service to auto-start on system boot:

$ sudo systemctl start mongod
$ sudo systemctl enable mongod

Step 4 – Verify the Installation

To verify the MongoDB has successfully installed in the system, you should connect to the mongoDB database server using the mongo command, which will print the connection status where you can check the version of MongoDB, as shown below:

$ mongo --eval 'db.runCommand({ connectionStatus: 1 })'
Output:
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.10
{
  "authInfo" : {
    "authenticatedUsers" : [ ],
    "authenticatedUserRoles" : [ ]
  },
  "ok" : 1
}

In the above output message, the value of 1 for the ok field indicates success.

Configuring MongoDB

You can edit /etc/mongod.conf file to configure MongoDB instance. It is a YAML formatted configuration file.

The preconfigured or default settings is enough for most of the users. Still, to run MongoDB in production, it is recommended to uncomment the security configuration and enable settings for authorization, as shown below:

security:
  authorization: enabled

The authorization option provides Role-Based Access Control (RBAC) to provide access control for users on database resources and operations. Ig authorization is disabled, means each user having access to all database for performing any action.

After changing the configuration of the MongoDB server on the configuration file, need to restart mongod service to take new settings using the following command:

$ sudo systemctl restart mongod

You can find more option related to MongoDB configuration at Configuration file options on the documentation page.

Creating MongoDB Administrative User

If you have enabled the authentication in your MongoDB instance, you should create an administrative user to access and manage your MongoDB instance.

To create a user, first, you need to access the mongodb shell using the following command:

$ mongo

Once you get the MongoDB shell, use the following command to connect with the admin database:

use admin
Output:
switched to db admin

To create a user in MongoDB, you need to use the below command, where mongoAdmin is the username, and userAdminAnyDatabase is for User role:

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

You can exit the mongodb shell using the following command:

quit()

You can test new user credentials and access by using mongo shell to log in for the new user, as shown below:

$ mongo -u mongoAdmin -p --authenticationDatabase admin
use admin
Output:
switched to db admin

To get the information about users in the mongo shell, use the following command:

show users
Output:
{
	"_id" : "admin.mongoAdmin",
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

Conclusion

You have learned MongoDB installation and configuration in your Ubuntu 20.04 machine. To learn more about MongoDB, you can visit the MongoDB manual page.

If you have any problem with the installation or having any feedback, please comment below.

0 Comments

Submit a Comment

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

Related Articles