How to Install MongoDB on Ubuntu 18.04/16.04 LTS

In this tutorial, we are going to discuss 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. Add official Apt Repository

Here, we are adding mongoDB official apt package and this is signed with GPG keys.To check latest apt repository for ubuntu go through this link,

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

Next add the repository in /etc/apt/sources.list.d/mongodb-org-4.0.list using below command as per version of Ubuntu

Ubuntu 18.04

$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Ubuntu 16.04

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

update the system package using below command,

$ sudo apt-get update

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

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

Reload,Enable and Restart mongod to apply mongod service and allow remote access.

$ sudo systemctl daemon-reload 
$ sudo systemctl enable mongod

Output:

Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /lib/systemd/system/mongod.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, 1.Installed MongoDB on Ubuntu using apt repository 2.Allowed remote access 3.Configured mongodb administrator user 4.Enabled mongodb authentication.

Related Articles:

How to Download and Install MongoDB on Windows

Leave a Comment

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

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

We promise not to spam you, and we don't usually send more than one email a week.

 

You have Successfully Subscribed!

27 Shares
Share via
Copy link