PLXR \Blog
PLXR \Blog

A PLXR Media blog

Xar
Author

Share


How To Install MongoDB on Ubuntu 16.04

XarXar

MongoDB is a free and open-source NoSQL document database used commonly in modern web applications. This tutorial will help you set up MongoDB on your server for a production application environment.

Adding the MongoDB Repository

Ubuntu ensures the authenticity of software packages by verifying that they are signed with GPG keys, so we first have to import their key for the official MongoDB repository.

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

Issue the following command to create a list file for MongoDB.

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list  

After adding the repository details, we need to update the packages list.

sudo apt-get update  

Installing and Verifying MongoDB

Install MongoDB package by executing

sudo apt-get install -y mongodb-org  

In order to properly launch MongoDB as a service on Ubuntu 16.04, we additionally need to create a unit file describing the service.

We'll create a unit file to manage the MongoDB service. Create a configuration file named mongodb.service in the /etc/systemd/system directory.

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

Paste in the following content, then save and close the file.

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

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

[Install]
WantedBy=multi-user.target  

Start the newly created service with systemctl.

sudo systemctl start mongodb  

Check the status of the MongoDB service - the output should show Active : active (running)

sudo systemctl status mongodb  

You might also want to automatically start the service when the system start. To enable that :

sudo systemctl enable mongodb  

In case Failed global initialization error, execute the ff :
export LC_ALL=C

Creating User and Granting Permission

Open MongoDB by executing mongo and type in the following commands to create user.
Replace username and password with the desired values

use admin  
db.createUser( { user: "username",  
          pwd: "password",
          roles: [ "userAdminAnyDatabase",
                   "dbAdminAnyDatabase",
                   "readWriteAnyDatabase" 
] } )

Remote Access Configuration

Only do this if you want to access the MongoDB server remotely. When doing this, take extra precautionary measures for the security of your server like limiting the access to port 27017 to specific IP addresses

Open MongoDB configuration file

sudo nano /etc/mongod.conf  

Replace bindIp: 127.0.0.1 with bindIp: 0.0.0.0 or with specific IP address if you want to limit the connection to one IP address only.

After updating the configuration file, restart MongoDB service

sudo systemctl restart mongodb  

Lastly, open port 27017

sudo ufw allow 27017  
Xar
Author

Xar

Comments