Introduction
MongoDB is a robust, open-source, document-oriented database management system that is based on NoSQL’s state-of-the-art technology. It is a free database system that delivers the flexibility and scalability required to store data in JSON-like documents. The application offers high-performance data persistence, automatic scaling, and high availability capabilities. This makes MongoDB an incredible solution for developing modern applications that demand powerful, mission-critical databases.
This tutorial will help you install the latest version of MongoDB, set up basic authentication, and manage its service on your Ubuntu 18.04 server.
Ready? Let’s go!
Prerequisites
Before we proceed with the installation, the following conditions must be met:
- The tutorial requires Ubuntu 18.04 server
- A sudo non-root user
- A fully-configured firewall
Step 1 – Installing MongoDB
All the required MongoDB packages can be installed using apt. This is because Ubuntu’s package repositories come with an up-to-date MongoDB version.
The first thing when installing MongoDB is to update your server package list to the latest version. Run the command below to accomplish this:
$ sudo apt update
Once the update is complete, install the MongoDB package using the command below:
$ sudo apt install -y mongodb
The command above installs multiple packages that feature the most recent stable MongoDB version together with the required MongoDB server management tools. Once the installation is complete, the MongoDB server should start automatically.
Step 2 – Verifying The Database And Server
The next step is verifying that MongoDB service is running and that MongoDB database is working correctly. First, run the following command to check the status of the service:
$ sudo systemctl status mongodb
This should give you an output like the one below:
●mongodb.service-Anobject/document-orienteddatabase Loaded: loaded(/lib/systemd/system/mongodb.service;enabled;vendor preset: enabled) Active: active(running)sinceSat2018-05-2607:48:04UTC;2min17sago Docs: man:mongod(1) Main PID: 2312(mongod) Tasks: 23(limit:1153) CGroup: /system.slice/mongodb.service └─2312/usr/bin/mongod--unixSocketPrefix=/run/mongodb--config/etc/mongodb.conf
Based on the systemd, it’s clear that the server is running flawlessly. To further check the status of the service, connect to a database server and run a diagnostic command:
$ mongo --eval'db.runCommand({ connectionStatus: 1 })'
You will get an output with details such as the server port and address, current database version, and the status command.
MongoDB shell version v3.6.3 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.6.3 { "authInfo" : { "authenticatedUsers" : [ ], "authenticatedUserRoles" : [ ] }, "ok" : 1 }
The value “ok” : 1 shows that the server is running precisely.
Step 3 – Managing The Newly Installed MongoDB Service
Once MongoDB has fully installed it manifests as a systemd service which means it can be managed using the systemd command. The following are the systemd commands to use to manage MongoDB:
- To check the status of MongoDB service, run the command below:
$ sudo systemctl status mongodb
- MongoDB database server can be stopped anytime, by running the following command:
$ sudo systemctl stop mongodb
- On the other hand, the service can be started by running the command below:
$ sudo systemctl start mongodb
- Additionally, MongoDB database server can be restarted with a single command:
$ sudo systemctl disable mongodb
- And to enable MongoDB, you can run the command below:
$ sudo systemctl enable mongodb
Step 4 Adjusting The Firewall Settings
In some cases when the firewall is enabled on Ubuntu Server, the access to MongoDB server may be limited. If you are planning to use the MongoDB server locally with apps operating on the same server, this setting is ideal and secure.
However, if you intend to connect to the MongoDB database server from the internet, its recommended you enable inbound connections in the Uncomplicated Firewall (UFW). By default, MongoDB server runs on the port 27017, and to permit access to MongoDB via this port you could run the command sudo ufw allow 27017. However, permitting access to MongoDB database server via its default port provides unrestricted access to data and the server itself.
For this reason, it’s always wise to allow access only on specific locations. To achieve this, you enable access on the default port but specify the IP address of the other server with explicit rights to connect to the database server. Run the command below:
$ sudo ufw allow from your_other_server_ip/32 to any port 27017
Next, run the command below to check the status of the firewall:
$ sudo ufw status
This will give you an output like the one below:
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere 27017 ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) 27017 (v6) ALLOW Anywhere (v6)
The output clearly shows that port 27017 is allowed anywhere. If you have permitted a specific IP address to access the database server, you should see the allowed IP address listed and not Anywhere in the above output.
Now, even though the default port is open, the database server is currently listening on 127.0.0.1. To permit remote connections, you must include a publicly-routed IP address for your server to Mongo.conf file.
Run the command below to open the Mongo.conf file:
$ sudo nano /etc/mongodb.conf
Locate the bind_ip value and add the desired IP address:
... logappend=true bind_ip = 127.0.0.1,your_server_ip #port = 27017 ...
A comma should be placed before adding the IP address you want to allow. Once that is done, Save the changes and exit the editor.
Next, restart MongoDB:
$ sudo systemctl restart mongodb
That’s it! You have successfully modified the firewall settings.
Conclusion
Congratulations! You have successfully installed MongoDB on your Ubuntu 18.04 server and adjusted the Firewall settings to manage access to the Database server. You can explore more MongoDB options and learn how to create a root user and password for MongoDB.
Check out these top 3 MySQL hosting services:
- Click this link and all your queries to best hosting will end.