How to Install MongoDB on Ubuntu 20.04 LTS

In this tutorial, We are going to Perform How to Install MongoDB on Ubuntu 20.04 LTS or any other cloud platform like Amazon EC2, Azure VM, Google Compute Cloud, etc., with preinstalled ubuntu 20.04 LTS,

Introduction

MongoDB is a free and open source document-oriented NoSQL and cross-platform database server used for high volume data storage.It uses JSON like documents which makes the database very flexible and scalable. MongoDB is a high-performance database program, used by one of the biggest companies in the world like Facebook,Google, Adobe,SAP, TrendMicro, Verizon, BarClays .etc.

1. Import MongoDB Public GPG Key

First install the gnupg package using below command

$ sudo apt-get install gnupg

Download and import MongoDB public GPG key in your system.

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

2. Add MongoDB official APT Repository

Once GPG key imported, Add the MongoDB official APT repository in /etc/apt/sources.list.d/mongodb-org-4.2.list using below command as per version of Ubuntu

 $ 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

update the system package using below command,

$ sudo apt-get update

3. How to Install MongoDB on Ubuntu 20.04 LTS

Using above commands we have added apt repository for MongoDB 4.2 community Edition, below is command to install MongoDB,

$ sudo apt-get install -y mongodb-org

Start the MongoDB service

$ sudo systemctl start mongod

Now check to see if the service is running:

 $ sudo systemctl status mongod

Output:

● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2020-06-25 04:23:25 UTC; 9s ago
Docs: https://docs.mongodb.org/manual
Main PID: 1089445 (mongod)
Memory: 73.7M
CGroup: /system.slice/mongod.service
└─1089445 /usr/bin/mongod --config /etc/mongod.conf

Enable MongoDB at System startup

$ sudo systemctl enable mongod

Output:

Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.

Now, you’ve successfully covered  How to Install mongodb on Ubuntu 20.04 LTS.

3. Allow Remote Access

By default, MongoDB is listening on 127.0.0.1:27017 only:

Open MongoDB configuration file /etc/mongod.conf and change bindIp by adding required

$ sudo nano /etc/mongod.conf

By default MongoDB accepts connection from localhost only for security reason. Change the bindIp from 127.0.0.1 to 0.0.0.0 OR you can add any IP those you want to give access.

# network interfaces 
net: 
port: 27017 
bindIp: 127.0.0.1

TO

 # network interfaces 
    net:   
       port: 27017   
       bindIp: 0.0.0.0 # to bind to all interfaces

Reload the system daemon to apply changes

$ sudo systemctl daemon-reload

3. Configuring MongoDB administrator username

Next, Set up MongoDB administrator username and password first we need to open the MongoDB shell, type in:

 $ mongo

Inside the mongo shell type this command to switch to the admin database:

 > use admin

Now let’s create the administrator username and set a password for the username:

 > db.createUser({user:"fosstechnix", pwd:"[email protected]", roles:[{role:"root", db:"admin"}]})
Output:
 Successfully added user: {         
        "user" : "fosstechnix",         
         "roles" : [                 
                  {                         
          "role" : "root",                         
          "db" : "admin"                 
                  }         
          ]
 }

Type this command in the shell to exit the shell:

 > exit

4. Enable MongoDB authentication

Open /lib/systemd/system/mongod.service with nano:

 $ sudo nano /lib/systemd/system/mongod.service

On the ExecStart line add a new option argument –auth, the line should look like this:

 ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

press Ctrl+O to save and press Ctrl+X to close the file.

Restart MongoDB so the changes take effect:

 $ sudo systemctl restart mongod

Now connect to the MongoDB shell using this command:

 $ mongo -u fosstechnix -p --authenticationDatabase admin

You’ll get prompted for a password, enter the password you set above. Once you are inside the shell verify you’re authenticated with the administrator user we created by issuing this command:

 > db.runCommand({connectionStatus : 1})

Output:

 
{ 
        "authInfo" : {                 
       "authenticatedUsers" : [                         
                   {                                 
                       "user" : "admin",
                        "db" : "admin"                         
                    }                 
                    ],                 
                        "authenticatedUserRoles" : [                         
                     {                                 
                         "role" : "root",                                 
                         "db" : "admin"                         
                      }                 
                      ]         
                      },         
                      "ok" : 1
 }

Conclusion:

In this article we have performed, How to Install MongoDB on Ubuntu 20.04 LTS, creating admin user in mongoDB, allowing remote access and enabling mongodb authentication, commands to check mongodb services commands.

Related Articles:

How to Install MongoDB on Ubuntu 16.04 LTS

Uninstall MongoDB on Ubuntu in 3 Steps

How to Download and Install MongoDB on Windows

Reference

MongoDB Office Documentation

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link