// Tutorial //

How To Install MongoDB from the Default APT Repositories on Ubuntu 18.04

Published on June 7, 2018 · Updated on October 8, 2020
Default avatar
By Mateusz Papiernik
Software Engineer, CTO @Makimo
English
How To Install MongoDB from the Default APT Repositories on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04

The author selected the Creative Commons Corporation to receive a $100 donation as part of the Write for DOnations program.

Introduction

MongoDB, also known as Mongo is a free and open-source NoSQL document database used commonly in modern web applications.

In this tutorial you’ll install MongoDB, manage its service, and optionally enable remote access.

Note: As of this writing, this tutorial installs version 3.6 of MongoDB, which is the version available from the default Ubuntu repositories. However, we generally recommend installing the latest version of MongoDB — version 4.4 as of this writing — instead. If you’d like to install the latest version of MongoDB, we encourage you to follow this guide on How To Install MongoDB on Ubuntu 18.04 from source.

Prerequisites

To follow this tutorial, you will need:

Step 1 — Installing MongoDB

Ubuntu’s official package repositories include MongoDB, which means we can install the necessary packages using apt. As mentioned in the introduction, the version available from the default repositories is not the latest one. To install the latest version of Mongo, please follow this tutorial instead.

First, update the packages list to have the most recent version of the repository listings:

  1. sudo apt update

Now install the MongoDB package itself:

  1. sudo apt install -y mongodb

This command installs several packages containing a stable version of MongoDB, along with helpful management tools for the MongoDB server. The database server is automatically started after installation.

Next, let’s verify that the server is running and works correctly.

Step 2 — Checking the Service and Database

The installation process started MongoDB automatically, but let’s verify that the service is started and that the database is working.

First, check the service’s status:

  1. sudo systemctl status mongodb

You’ll see this output:

Output
mongodb.service - An object/document-oriented database Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-05-26 07:48:04 UTC; 2min 17s ago 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

According to systemd, the MongoDB server is up and running.

We can verify this further by actually connecting to the database server and executing a diagnostic command

Execute this command:

  1. mongo --eval 'db.runCommand({ connectionStatus: 1 })'

This will output the current database version, the server address and port, and the output of the status command:

Output
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 }

A value of 1 for the ok field in the response indicates that the server is working properly.

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

Step 3 — Managing the MongoDB Service

MongoDB installs as a systemd service, which means that you can manage it using standard systemd commands alongside all other sytem services in Ubuntu.

To verify the status of the service, type:

  1. sudo systemctl status mongodb

You can stop the server anytime by typing:

  1. sudo systemctl stop mongodb

To start the server when it is stopped, type:

  1. sudo systemctl start mongodb

You can also restart the server with a single command:

  1. sudo systemctl restart mongodb

By default, MongoDB is configured to start automatically with the server. If you wish to disable the automatic startup, type:

  1. sudo systemctl disable mongodb

It’s just as easy to enable it again. To do this, use:

  1. sudo systemctl enable mongodb

Next, let’s adjust the firewall settings for our MongoDB installation.

Step 4 — Adjusting the Firewall (Optional)

Assuming you have followed the initial server setup tutorial instructions to enable the firewall on your server, the MongoDB server will be inaccessible from the internet.

If you intend to use the MongoDB server only locally with applications running on the same server, this is the recommended and secure setting. However, if you would like to be able to connect to your MongoDB server from the internet, you have to allow the incoming connections in ufw.

To allow access to MongoDB on its default port 27017 from everywhere, you could use sudo ufw allow 27017. However, enabling internet access to MongoDB server on a default installation gives anyone unrestricted access to the database server and its data.

In most cases, MongoDB should be accessed only from certain trusted locations, such as another server hosting an application. To accomplish this task, you can allow access on MongoDB’s default port while specifying the IP address of another server that will be explicitly allowed to connect:

  1. sudo ufw allow from your_other_server_ip/32 to any port 27017

You can verify the change in firewall settings with ufw:

  1. sudo ufw status

You should see traffic to port 27017 allowed in the output:

Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
27017                      ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
27017 (v6)                 ALLOW       Anywhere (v6)

If you have decided to allow only a certain IP address to connect to MongoDB server, the IP address of the allowed location will be listed instead of Anywhere in the output.

You can find more advanced firewall settings for restricting access to services in UFW Essentials: Common Firewall Rules and Commands.

Even though the port is open, MongoDB is currently only listening on the local address 127.0.0.1. To allow remote connections, add your server’s publicly-routable IP address to the mongod.conf file.

Open the MongoDB configuration file in your editor:

  1. sudo nano /etc/mongodb.conf

Add your server’s IP address to the bindIP value:

[label ]
...
logappend=true

bind_ip = 127.0.0.1,your_server_ip
#port = 27017

...

Be sure to place a comma between the existing IP address and the one you added.

Save the file, exit the editor, and restart MongoDB:

  1. sudo systemctl restart mongodb

MongoDB is now listening for remote connections, but anyone can access it. Follow Part 2 of How to Install and Secure MongoDB on Ubuntu 16.04 to add an administrative user and lock things down further.

Conclusion

You can find more in-depth tutorials on how to configure and use MongoDB in these DigitalOcean community articles. The official MongoDB documentation is also a great resource on the possibilities that MongoDB provides.

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here


About the authors
Default avatar
Software Engineer, CTO @Makimo

Creating bespoke software ◦ CTO & co-founder at Makimo. I’m a software enginner & a geek. I like making impossible things possible. And I need tea.


Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
10 Comments

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

This method is counter to MongoDB’s own recommendation at the top of their installation guide:

The unofficial mongodb package provided by Ubuntu is not maintained by MongoDB. You should always use the official MongoDB mongodb-org packages, which are kept up-to-date with the most recent major and minor MongoDB releases.

I followed this tutorial, and now my mongodb is broken. How can I uninstall it completely? I tried “sudo apt-get remove mongodb”, but it doesn’t work.

Following this tutorial I get version 2.6.10 instead of 3.6.3

I’m confused why you linked that part two adding authentication article?

That article uses a mongod.conf file whereas here it’s mongodb.conf, so it doesn’t help.

When I searched online it seems that the mongodb.conf file indicates you’re using an older version of mongo, but I’m running 3.6.3

In the code, it should be bindIp and not bind_ip. I created a mongodb server using one click apps. But whenever I add multiple IP address for bindIP or even change the default value then config file shows error. Any suggestions?

How do I uninstall the same?

thank you very much.

The configure to add IP is no longer valid.

When you are to execute this step Add your server’s IP address to the bindIP value:, be sure to change it to

bindIp: [127.0.0.1, X.X.X.X]

https://stackoverflow.com/a/43864346/4068957

I’m using Ubuntu 19.04 and was having problems with the commands

sudo systemctl start|stop|enable|disable mongodb

On my system, after installation, I have /lib/systemd/system/mongod.service and therefore need to use the above command with ‘mongod’ not ‘mongodb’

Why hasn’t anyone responded to any of the issues in these comments?

This guide is the #1 result on google for installing mongodb on ubuntu 18 on digital ocean and it’s not the right way?