Introduction to the

MongoDB is a free open source NoSQL document database that is often used in modern web applications.

In this tutorial module, we will install MongoDB, manage its services, and enable remote access options.

Please pay attention to. At the time of this writing, the tutorial module is installed with version MongoDB3.6, which is available in the Ubuntu library by default. However, we generally recommend installing the latest version of MongoDB instead. As of this writing, this is version 4.4. If you want to install the latest version of MongoDB, we recommend following the instructions in this guide and installing MongoDB from the source in Ubuntu 20.04.

A prerequisite for

You will need the following to implement this tutorial module.

  • An Ubuntu 20.04 server, configured according to the instructions in the initial Server Setup tutorial module, has one user without root permission, has administrative permission, and has a firewall configured as UFW.

Step 1 – Install MongoDB

MongoDB is included with Ubuntu’s official package library, which means we can use APT to install the necessary packages. As mentioned in the introduction, the default version in the repository is not the latest version. To install the latest version of Mongo, use this tutorial module.

You should first update the package to get the latest repository list.

sudo apt update
Copy the code

Then you need to install the MongoDB package itself.

sudo apt install mongodb
Copy the code

This command will ask you to confirm that you want to install the mongodb package and its dependencies. To do this, click Y and then ENTER.

This command installs several packages containing stable versions of MongoDB, as well as useful tools for managing MongoDB servers. The database server starts automatically after installation.

Then, you need to make sure the server is up and running correctly.

Step 2 – Check services and databases

MongoDB is automatically started during installation, but now we need to make sure the service is running and the database is running.

First, let’s check the status of the service.

sudo systemctl status mongodb
Copy the code

Here’s what you’ll see.

The Output low mongo. Service - An object/document - oriented database the Loaded: the Loaded (/ lib/systemd/system/mongo. Service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-10-08 14:23:22 UTC; 49s ago Docs: man: Mongod (1) Main PID: 2790 (Mongod) Tasks: 23 (LIMIT: 2344) Memory: 42.2M CGroup: / system. Slice/mongo. Service └ ─ 2790 / usr/bin/mongod - unixSocketPrefix = / run/mongo - config/etc/mongo. ConfCopy the code

Based on this output, the MongoDB server is up and running.

We can further confirm this by actually connecting to the database server and running the following diagnostic commands. This command displays the current database version, server address and port, and the result of the status command.

mongo --eval 'db.runCommand({ connectionStatus: 1 })'
Copy the code
OutputMongoDB shell version v3.6.8 connecting to: mongo: / / 127.0.0.1: Implicit 27017 session, the session {" id ": UUID(" e3C1f2A1-a426-4366-b5F8-c8b8e7813135 ")} MongoDB server version: 3.6.8 {"authInfo" : {"authenticatedUsers" : [ ], "authenticatedUserRoles" : [ ] }, "ok" : 1 }Copy the code

The value of field 1 in the response is OK, which means that the server is running properly.

Now we’ll look at how to manage the server instance.

Step 3 – Manage the MongoDB service

The installation described in Step 1 configates MongoDB as a Systemd service, which means you can manage it with all the other system services in Ubuntu using the standard systemctl commands.

To check the status of the service, enter.

sudo systemctl status mongodb
Copy the code

You can stop the server at any time using the following command.

sudo systemctl stop mongodb
Copy the code

To start a stopped server, type.

sudo systemctl start mongodb
Copy the code

You can also restart the server using the following command.

sudo systemctl restart mongodb
Copy the code

By default, MongoDB is configured to start automatically with the server. Enter if you want to disable auto start.

sudo systemctl disable mongodb
Copy the code

You can reactivate autostart at any time with the following command.

sudo systemctl enable mongodb
Copy the code

Now let’s change the firewall Settings for our MongoDB system.

Step 4 – Configure a firewall (optional)

If you have enabled a firewall on your server, following the initial server setup tutorial, the MongoDB server will not be accessible from the Internet.

If you plan to use the MongoDB server locally and run your application on the same server, it is recommended to keep this security setting. However, if you want to be able to connect to MongoDB servers from the Internet, you need to allow inbound connections by adding the appropriate UFW rules.

To allow access to MongoDB from anywhere through the default port 27017, use the command sudo ufw allow 27017. However, enabling default Settings for access to MongoDB servers over the Internet will make the database server and its contents accessible to anyone.

In most cases, you should only allow access to MongoDB from some trusted place, such as another application hosting server. To allow access only to the default MongoDB port from another trusted server, you can specify the IP address of the remote server in the Ufw command. In this way, the connection will be explicitly allowed for that machine only.

sudo ufw allow from trusted_server_ip/32 to any port 27017
Copy the code

You can check and change firewall Settings by using the UFW.

sudo ufw status
Copy the code

The output should show that traffic to port 27017 is allowed. If you choose to allow only one IP address to connect to the MongoDB server, you should list that address instead of Anywhere in the output list of this command.

OutputStatus: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
27017                      ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
27017 (v6)                 ALLOW       Anywhere (v6)
Copy the code

For details about firewall Settings that restrict service access, see UFW Basics: Common Firewall Rules and Commands.

While the port is open, MongoDB will still only listen on the local address 127.0.0.1. To allow remote connections, add your server’s public routable IP address to mongodb.conf.

Open the MongoDB configuration file in your favorite text editor. The command in this example uses nano.

sudo nano /etc/mongodb.conf
Copy the code

Add the IP address of your MongoDB server to blindIP. Make sure to put a comma between the IP address you already wrote and the one you added.

/etc/mongodb.conf

. logappend=trueYour_server_ip bind_ip = 127.0.0.1#port = 27017.Copy the code

Save the file and exit the editor. If you are using Nano to edit the file, press CTRL + X, Y, and then ENTER.

Then restart the MongoDB service.

sudo systemctl restart mongodb
Copy the code

MongoDB is now listening for remote connections, but it is accessible to everyone. Add an administrative user and other restrictions as per the install and Secure MongoDB in Ubuntu 20.04 guide.

conclusion

A more detailed tutorial on how to setup and use MongoDB can be found in the DigitalOcean community article below. The official MongoDB documentation also contains a lot of information about what MongoDB provides.