MongoDB is a free and open-source, NoSQL document database mostly used in modern web applications. It is mainly structured for great performance data persistence, more available and automatic scaling, Under MongoDB, a record refers to a document, which is a data design that has field and value pairs (MongoDB documents are comparable to JSON objects).
It offers amazing performance and quality scalability features making it useful in creating modern applications that need powerful, high-availability and mission-critical databases.
In this tutorial, we’ll show you how to install MongoDB, its service management and installing basic authentication on Ubuntu 18.04. To install MongoDB, follow the steps below
1. Add the MongoDB repository
Although MongoDB comes already included in Ubuntu package repositories, the official repository provides an up-to-date version and comes highly recommended as the best way of installing MongoDB.
We’ll first import the GPG key to verify the authenticity of MongoDB repository.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
After adding the key you’ll get the following output
Executing: /tmp/tmp.c09MuVaMkG/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 gpg: requesting key EA312927 from hkp server keyserver.ubuntu.com gpg: key EA312927: public key "MongoDB 3.2 Release Signing Key <packaging@mongodb.com>" imported gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1)
Next, we’ll add the MongoDB repository so that apt will know where to get the MongoDB packages from
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 appending the repository packages, update the packages list
apt-get update
2. Install and verify MongoDB
Install MongoDB package by running the following command
sudo apt-get install -y mongodb-org
This will install many packages contanining the latest version of MongoDB alongside other useful management tools
Output
Preparing to unpack .../mongodb-org_3.2.20_amd64.deb ... Unpacking mongodb-org (3.2.20) ... Processing triggers for man-db (2.7.5-1) ... Setting up mongodb-org-shell (3.2.20) ... Setting up mongodb-org-server (3.2.20) ... Adding system user `mongodb' (UID 112) ... Adding new user `mongodb' (UID 112) with group `nogroup' ... Not creating home directory `/home/mongodb'. Adding group `mongodb' (GID 116) ... Done. Adding user `mongodb' to group `mongodb' ... Adding user mongodb to group mongodb Done. Setting up mongodb-org-mongos (3.2.20) ... Setting up mongodb-org-tools (3.2.20) ... Setting up mongodb-org (3.2.20) ...
Start MongoDB service using systemd
systemctl start mongodb
To check the status on MongoDB, run
systemctl status mongod
Output
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Mon 2018-07-23 20:09:15 UTC; 14s ago
Docs: https://docs.mongodb.org/manual
Main PID: 5171 (mongod)
Tasks: 19
Memory: 33.4M
CPU: 106ms
CGroup: /system.slice/mongod.service
└─5171 /usr/bin/mongod --quiet --config /etc/mongod.conf
Jul 23 20:09:15 ip-172-31-21-90 systemd[1]: Started High-performance, schema-free document-oriented database.
To enable MongoDB to start automatically on boot run
systemctl enable mongod
If you wish to stop the service , run
systemctl stop mongod
3. Configure Firewall (Optional)
By default MongoDB listens on port 27017 permitting access from everywhere you can use.
sudo ufw allow 27017
Allowing access to MongoDB from everywhere provides non-restricted access to the database data. So, it’s good to offer access to particular IP address location to default MongoDB’s port using following command.
sudo ufw allow from your_server_IP/32 to any port 27017
Verify the changes using ufw as shown
sudo ufw status
It’s only by default that the port 27017 listens on the local address 127.0.0.1. To permit remote MongoDB links, you need to include your server IP address in /etc/mongodb.conf configuration file as shown.
bind_ip = 127.0.0.1,your_server_ip #port = 27017
Save the file, close the editor, and start again MongoDB.
sudo systemctl restart mongodb
That’s all we had for today. Feel free and welcome to install MongoDB and give us your thoughts.