How to Install and Configure MongoDB on Ubuntu 18.04 LTS

MongoDB is a NoSQL database that offers a high performance, high availability, and automatic scaling enterprise database. MongoDB is a NoSQL database, so you can’t use SQL (Structured Query Language) to insert and retrieve data, and it does not store data in tables like MySQL or Postgres. Data is stored in a “document” structure in JSON format (in MongoDB called BSON). MongoDB was first introduced in 2009 and is currently developed by the company MongoDB Inc.

MongoDB only provides packages for 64-bit LTS (long-term support) Ubuntu releases. For example, 14.04 LTS (trusty), 16.04 LTS (xenial), 18.04 LTS (bionic) and so on.

In this tutorial, I will install MongoDB 3.6 on Ubuntu 18.04 LTS.

Prerequisites

  • Ubuntu Server 18.04 – 64 bit
  • Root privileges

What we will do in this tutorial:

  1. Install MongoDB
  2. Configure MongoDB
  3. Conclusion

Install MongoDB on Ubuntu 18.04

Step 1 – Importing the Public Key

GPG keys of the software distributor are required by the Ubuntu package manager apt (Advanced Package Tool) to ensure package consistency and authenticity. Run this command to import MongoDB keys to your server.

sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

Step 2 – Create source list file MongoDB

Create a MongoDB list file in /etc/apt/sources.list.d/ with this command:

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

MongoDB has not released packages for Bionic Beaver yet, but the Xenial packages are working fine on Ubuntu 18.04 LTS. You might check out the URL http://repo.mongodb.org/apt/ubuntu/dists/ though before you execute the above command. If you see a directory ‘bionic’ on that web page, then replace the word ‘xenial’ in the above command with the word ‘bionic’.

Step 3 – Update the repository

update the repository with the apt command:

sudo apt-get update

Step 4 – Install MongoDB

Now you can install MongoDB by typing this command:

sudo apt-get install -y mongodb-org

The MongoDB apt installer created a mongod.service file for Systemd automatically, so there is no need to create it manually anymore.

Start MongoDB and add it as a service to be started at boot time:

systemctl start mongod
systemctl enable mongod

Now check that MongoDB has been started on port 27017 with the netstat command.

netstat -plntu

Check if MongoDB is running.

Configure MongoDB username and password

When the MongoDB packages are installed you can configure username and password for the database server:

Step 1 – Open mongo shell

Before you set up a username and password for MongoDB, you need to open the MongoDB shell on your server. You can login by typing:

mongo

If you get error Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly, try the command:

export LC_ALL=C
mongo

Step 2 – Switch to the database admin

Once you`re in the MongoDB shell, switch to the database named admin:

use admin

Step 3 – Create the root user

Create the root user with this command :

db.createUser({user:”admin”, pwd:”admin123″, roles:[{role:”root”, db:”admin”}]})

Desc: Create user admin with password admin123 and have the permission/role as root and the database is admin.

Create a admin user in MongoDB

Now type exit to exit from MongoDB shell.

Step 4 – Enable mongodb authentication

Edit the mongodb service file ‘/lib/systemd/system/mongod.service’ with your editor.

nano /lib/systemd/system/mongod.service

On the ‘ExecStart‘ line 9, add the new option ‘–auth‘.

ExecStart=/usr/bin/mongod –auth –config /etc/mongod.conf

Save the service file and exit nano.

Reload the systemd service:

systemctl daemon-reload

Step 5 – Restart MongoDB and try to connect

Now restart MongoDB and connect with the user created.

sudo service mongod restart

and connect to the MongoDB shell with this command:

mongo -u admin -p admin123 –authenticationDatabase admin

and you will see the output like this:

Test MongoDB Authentication.

Virtual Machine image

This tutorial is available as ready to use virtual machine in OVA / OVF format for Howtoforge subscribers. The VM format is compatible with VMWare and Virtualbox and other tools that can import the ova or ovf format. You can find the download link in the right menu on the top. Click on the filename to start the download.

The login details of the VM are:

SSH Login

Username: administrator
Password: howtoforge

The administrator user has sudo permissions.

Please change the passwords after the first boot.

The VM is configured for the static IP 192.168.1.100. Instructions on how to change the static IP can be found here.

Conclusion

A well-known NoSQL database that offers high performance, high availability, and automatic scaling is MongoDB. It is different from the RDBMS such as MySQL, PostgreSQL, and SQLite as it does not use SQL to set and retrieve data. MongoDB stores data in `documents` called BSON (binary representation of JSON with additional types of information). MongoDB is only available for 64-bit Long Term Support Ubuntu Release.

Share this page:

how to install and configure mongodb on ubuntu 18 04 lts 3
how to install and configure mongodb on ubuntu 18 04 lts 4
how to install and configure mongodb on ubuntu 18 04 lts 5
how to install and configure mongodb on ubuntu 18 04 lts 6

نوشته های مشابه