How to Install MongoDB on Ubuntu 16.04


How to Install MongoDB on Ubuntu 16.04

Installing MongoDB on Ubuntu 16.04 is an easy task, just follow the steps below carefully, and you will have MongoDB installed on Ubuntu 16.04 in less then 10 min.  MongoDB is a free, open source document-oriented NoSQL database program that is renowned for its high performance. MongoDB is written in C++ and stores it’s data in a JSON style format called BSON or Binary JSON.It was introduced in 2009 and is being developed by MongoDB Inc.

In this tutorial, we will install MongoDB community version on an Ubuntu 16.04 based VPS.

1. Requirements:

Note: Run all commands in this tutorial without sudo if you execute them from the root user.

2. Adding MongoDB’s official repository

To ensure the credibility of the packages Ubuntu makes sure they are signed with GPG keys.
Let’s begin by importing the GPG keys we need for the official MongoDB repository:

 # sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

Next add the MongoDB repository in /etc/apt/sources.list.d using this command:

 # echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

And then issue the update command so Ubuntu can read the packages from the newly added repository:

 # sudo apt-get update

3. Installing MongoDB

To start installing MongoDB from the repository we just added, we issue the command:

 # sudo apt-get install -y mongodb-org

Although the MongoDB repository now provides the unit file in the package, we left this part of the tutorial for educational purposes as it can be used to install other services.

Now we need to create a systemd unit file for MongoDB. First, let us explain briefly what systemd unit files are. 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:

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

Paste the following text below:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

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

[Install]
WantedBy=multi-user.target

Make sure to save (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:

 # sudo systemctl daemon-reload
 # sudo systemctl enable mongod
 # sudo systemctl start mongod

Check to see if the service is running:

 # systemctl status mongod

The output should look something like this:

● mongodb.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-06-29 07:13:54 CDT; 8s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 4734 (mongod)
   CGroup: /system.slice/mongodb.service
           └─4734 /usr/bin/mongod --quiet --config /etc/mongod.conf

Jun 29 07:13:54 test systemd[1]: Started High-performance, schema-free document-oriented database.

4. Configuring MongoDB administrator username

To set up the 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:"admin", pwd:"admin54321-", roles:[{role:"root", db:"admin"}]})

Note: You can substitute the value in pwd for your own password, like this: pwd:”mypassword”.
The output from the command above should look like this:

Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}

Type this command in the shell to exit the shell:

 > exit

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

Make sure to save (press Ctrl+O) and close (press Ctrl+X) the file.
Update systemd to include the new version of our modified service file:

 # sudo systemctl daemon-reload

Then restart MongoDB so the changes take effect:

 # sudo systemctl restart mongod

Now connect to the MongoDB shell using this command:

 # mongo -u admin -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})

The output should look like this:

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

That’s it, you’ve successfully installed MongoDB on Ubuntu 16.04.

Of course, if you are one of our Linux VPS Hosting customers, you don’t have to Install MongoDB on Ubuntu 16.04, simply ask our admins, sit back and relax. Our admins will Install MongoDB on Ubuntu 16.04 for you immediately.

PS. If you liked this post about How to Install MongoDB on Ubuntu 16.04, please share it with your friends on the social networks using the buttons below or simply leave a comment in the comments section. Thanks.

Tutorials
Set up a mail server with PostfixAdmin and MariaDB on CentOS 7
Tutorials
Install and use MySQLTuner on Ubuntu 14.04
Tutorials
How to install and configure AVG free anti-virus on an Ubuntu VPS
There are currently no comments.

 
Free Migration to RoseHosting. Award Winning Linux VPS Hosting with Fully Managed Support. Coupon RHBLOG50 for 50% offOrder Now
+