How to Install MongoDB on Ubuntu 16.04

How to Install MongoDB on Ubuntu 16.04
How to Install MongoDB on Ubuntu 16.04

Installing MongoDB on Ubuntu 16.04Today we are going to discuss how to install MongoDB on Ubuntu 16.04. MongoDB is a free and open source document-oriented NoSQL and cross-platform database server. It is a high-performance database program, used by one of the biggest companies in the world like Google, Adobe, SAP, EA, eBay, Verizon. The process of Installing MongoDB on Ubuntu 16.04 should take approximately  10 minutes. In this tutorial, we will install the MongoDB community version. Let’s get started.

1. Requirements:

  • Ubuntu Server 16.04 – 64 bit
  • Root user or sudo non-root user

Note: In this tutorial, run all commands without sudo if you execute them from the root user.

2. Adding MongoDB’s official repository

To ensure the credibility of the packages Ubuntu verifies if 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 on Ubuntu 16.04

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

 # sudo apt-get install -y mongodb-org

Install MongoDB Ubuntu 16.04Although 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 your Ubuntu-based server. You can now start developing your favorite database.


Install MongoD on Ubuntu 16.04Of course, if you are one of our MongoDB 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.

2 thoughts on “How to Install MongoDB on Ubuntu 16.04”

  1. Hi, I followed your instruction until Step 3 systemctl status. The daemon is active but when I try to access the database using “mongo”, the request connection is hanging. It says, connecting to: test. I believe this has something to do with Permissions.

    Reply

Leave a Comment