How to Install MongoDB on Ubuntu 16.04 LTS

In this tutorial, We are going to Perform How to install MongoDB on Ubuntu 16.04 LTS or any other cloud platform like Amazon EC2, Azure VM, Google Compute Cloud, etc., with preinstalled Ubuntu 16.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.

1. Import MongoDB Public GPG Key

First download and import MongoDB public GPG key in your system.

$ 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,arm64 ] 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 16.04 LTS

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 Tue 2020-03-31 18:19:50 UTC; 2 weeks 0 days ago
Docs: https://docs.mongodb.org/manual
Main PID: 1703 (mongod)
Tasks: 28
Memory: 292.8M
CPU: 1h 6min 23.828s
CGroup: /system.slice/mongod.service
└─1703 /usr/bin/mongod --config /etc/mongod.conf

Enable MongoDB at System startup

$ sudo systemctl enable mongod

4: Add Firewall Rule

To allow traffic on port 27017 enter the below command:

$ sudo ufw allow 27017/tcp

Check the firewall status to verify if port is enables

$ sudo ufw status

5. Creating MongoDB Administrator User

Before creating user mongodb change the /etc/mongod.conf file to disable authorization.

security:
  authorization: disabled

Restart the mongodb service to take effect

$ sudo systemctl restart mongod.service

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: 

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

Type this command in the shell to exit the shell:

> exit

6. 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.

Reload the mongodb daemon to take effect

$ sudo systemctl daemon-reload

Restart MongoDB so the changes take effect:

$ sudo systemctl restart mongod.service

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.

Output:

MongoDB shell version v4.0.13
Enter password:
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("273d3eda-fff7-4fc8-8a1c-37d20cbe7216") }
MongoDB server version: 4.0.13

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:

> db.runCommand({connectionStatus : 1})
{
        "authInfo" : {
                "authenticatedUsers" : [
                        {
                                "user" : "fosstechnix",
                                "db" : "admin"
                        }
                ],
                "authenticatedUserRoles" : [
                        {
                                "role" : "root",
                                "db" : "admin"
                        }
                ]
        },
        "ok" : 1
}
>

Now, We have successfully covered How to install MongoDB on Ubuntu 16.04 LTS.

7. Start/Stop/Restart/Enable MongoDB Service using command line

To start mongodb service

$ sudo systemctl start mongod.service

To stop mongodb service

$ sudo systemctl stop mongod.service

To restart mongodb service

$ sudo systemctl restart mongod.service

To enable mongodb service at system startup

$ sudo systemctl enable mongod.service

Conclusion:

In this article we have performed, How to Install MongoDB on Ubuntu 16.04 LTS, Creating MongoDB Administrator User, Enable MongoDB authentication.

Related Articles:

How to Install MongoDB on Ubuntu 18.04/16.04 LTS

Uninstall MongoDB on Ubuntu in 3 Steps

How to Download and Install MongoDB on Windows

Reference

MongoDB Office Documentation

Shivdas Kanade

I am Shivdas Kanade working as DevOps Engineer. Passionate about Linux, Open Source and DevOps Tools. Believe in sharing knowledge.

Leave a Comment

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

0 Shares
Share via
Copy link