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.

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

Now we need to create a systemd unit file for MongoDB. Unit files keep information about services, sockets, devices, basically, any resource managed by systemd which is an init system used by a large number of Linux distributions

Create the file in the /etc/systemd/system/ directory using nano editor:

 $ sudo nano /etc/systemd/system/mongodb.service

Paste the following text below:

 [Unit] 
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

Press Ctrl+O and close press Ctrl+X the file.

Now we have to update systemd to include our newly created service and we enable and start the 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 

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. 

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

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.

Update systemd to include the new version of our modified service file:

 $ sudo systemctl daemon-reload 

next 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 installed MongoDB on your Ubuntu server.

Conclusion:

In this article we have performed, How to Install MongoDB on Ubuntu 18.04/16.04 LTS.

Related Articles:

Uninstall MongoDB on Ubuntu in 3 Steps

How to Download and Install MongoDB on Windows

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.

27 Shares
Share via
Copy link