How to Install MongoDB on Ubuntu 18.04/16.04 LTS

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

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.Here, we are installing MongoDB 4.0 Community Edition on Ubuntu 18.04 and 16.04.

1. Import MongoDB Public GPG Key

First 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

Ubuntu 18.04 Bionic

$ 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

Ubuntu 16.04 Xenial

$ 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

update the system package using below command,

$ sudo apt-get update

3. Install MongoDB on Ubuntu 18.04/16.04

Using above commands we have added apt repository for MongoDB 4.0 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; enabled; vendor preset: enabled)
Active: active (running) since Sat 2019-02-23 16:55:07 UTC; 1 weeks 2 days ago
Docs: https://docs.mongodb.org/manual
Main PID: 28327 (mongod)
Tasks: 27
Memory: 223.2M
CPU: 24min 52.106s
CGroup: /system.slice/mongod.service
└─28327 /usr/bin/mongod --quiet --auth --config /etc/mongod.conf

Enable MongoDB at System startup

$ sudo systemctl enable mongod

Output:

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

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:"fosstechnix@123", 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
}

Now, you’ve successfully covered install mongodb on Ubuntu.

Conclusion:

In this article we have performed, How to Install MongoDB on Ubuntu 18.04/16.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 20.04 LTS

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
Powered by Social Snap