Using MongoDB with DigitalOcean

In this article, you will learn how to host MongoDB on DigitalOcean. DigitalOcean is an Infrastructure as a Service (IaaS) platform targeted at developers.

Choosing the Right Cloud Platform for MongoDB

While you can host MongoDB on Digital Ocean and this article will show you how, many developers opt for more managed and flexible cloud database services like MongoDB Atlas, which is a fully managed MongoDB service available across AWS, GCP, and Azure.

MongoDB Atlas makes all of the features needed by developers available in a forever free tier. If you have application nodes running in DigitalOcean, you can easily connect them to your MongoDB Atlas database.

In the next section, we’re going to look at some options for deploying MongoDB on DigitalOcean and then cover how you can create your own instance of MongoDB on DigitalOcean.

Options for Deploying MongoDB on DigitalOcean

There are two options available for running MongoDB on DigitalOcean:

  1. You can run “empty” DigitalOcean droplets (virtual machines that can be scaled on demand) and install MongoDB.

  2. You can download and spin up a MongoDB droplet, which is available in the marketplace, but it uses an old version (4.0) of MongoDB (current release is 4.4).

To get started deploying your instance of MongoDB Atlas on one of these cloud providers, you can follow this tutorial.

If you want to consider DigitalOcean, please read on.

Installing MongoDB on DigitalOcean

DigitalOcean Droplets

Five types of DigitalOcean droplets are available:

  • Basic: This category facilitates shared CPU and starts at $5/month. This option is viable if you’re starting an application where traffic/data volume is low.

  • General Purpose: This is the most popular droplet that offers a dedicated instance of 4GB memory for each CPU suitable for most production applications using medium-high traffic/data volume.

  • CPU Optimized: If you’re using high CPU intensive applications like video encoding or image parsing, then CPU-optimized droplet would be the best option where you get 100% dedicated vCPU and memory for each vCPU.

  • Memory-Optimized: This category is for memory-intensive applications where you want to use in-memory databases or caches that come with 100% dedicated vCPU and 8GB memory for each vCPU.

  • Storage-Optimized: This category is for droplets with large amounts of NVMe SSD storage and memory, and comes with a 100% dedicated vCPU.


First, you need to set up a Linux droplet on DigitalOcean where you will install the MongoDB community edition. (We use Ubuntu in this article, but the process is similar for the other operating systems.)

Set Up the Droplet

setting up a droplet on digitalocean for mongodb

Go to the Create Droplets page and follow these steps:

  • Choose an image: Select the latest Ubuntu (LTS) x64 image.

  • Choose a size: According to your needs, choose a pricing plan that meets your CPU and memory requirements.

  • Choose a data center region: According to your location, choose the nearest possible data center.

Finally, choose how many droplets you need. We will run a single MongoDB instance on a single droplet; this works for development, but you would need at least three droplets for a production system – and you would need to deploy a replica set. Once decided, click the Create button at the bottom on the same page.

You can also edit the hostname if you want a specific name for your virtual machine, but you need to provide a root password (or set up SSH keys).

Now, wait a few minutes to get the server ready and record the IP address and hostname.

record ip address in mongodb digitalocean droplet

Access DigitalOcean Droplet Console

If you click on the three dots on the top right corner of the droplet information box, you should see the Access console option to access the droplet server console:

Installing MongoDB on DigitalOcean through the Droplet Console

In the next step, a new window opens where you will be asked for username and password for the Ubuntu droplet server. Enter the username as root and the password that you generated while creating the droplet. If you’ve created a droplet without defining an SSH key to use, you should get an email with the username (“root”) and a link with instructions to connect to the droplet server if you face any issues.

Now, let’s install MongoDB on a droplet server.

Installing MongoDB on Droplet Server for Ubuntu - Community Edition

These instructions guide you through the installation of MongoDB 4.4 Community Edition on Ubuntu machines for 20.04 (Focal), 18.04 (Bionic), and Ubuntu 16.04 (Xenial). We will use the apt package manager for installation.

if you want to install a different version of MongoDB, go to this page and select the version from the dropdown in the top left-hand corner.

MongoDB only supports the 64-bit versions of these platforms. So, support for 64-bit x86_64, ARM64, and s390x architectures is available. For more detail on supported platforms, visit Supported Platforms.

1. Import MongoDB.

We import MongoDB files using wget. This command will import the public key provided by MongoDB for the package management system.

Open the terminal and enter the following command:

```wget -qO - | sudo apt-key add -```

When we run the wget command, the system responds with “OK.” If you receive an error that gnupg is not installed, run the following commands:

1.1 Install gnupg and its required libraries using the following commands:

sudo apt-get install gnupg

1.2. After installation, retry importing the public key to the package management system.

wget -qO - | sudo apt-key add -

2. Create a list file for MongoDB.

Now, create a list file for MongoDB that matches your version of Ubuntu. The list file contains active sources of APT data, with one source per line and the most preferred sources listed first.

2.1 Ubuntu 20.04 LTS

echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

2.2 Ubuntu 18.04 (Bionic)

echo "deb [ arch=amd64,arm64 ] bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

2.3. Ubuntu 16.04 (Xenial)

echo "deb [ arch=amd64,arm64 ] xenial/mongodb-org/4.4 multiverse" 
| sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

3. Reload the local package database.

Now, reload the local package database by issuing the following commands:

sudo apt-get update

4. Install MongoDB.

Now, install MongoDB packages. To install the latest stable version, enter the following command. To install a specific version, go to this page.

sudo apt-get install -y mongodb-org

Running MongoDB on Ubuntu - Community Edition

To run MongoDB, follow the below steps:

1. Take a note of your Init System:

ps --no-headers -o comm 1

2. Start MongoDB.

Then, enter the command that matches your Init system:

For systemd (systemctl):

sudo systemctl start mongod

For system V Init (service):

sudo service mongod start

3. Verify if MongoDB is successfully installed.

For systemd (systemctl):

sudo systemctl status mongod

For system V Init (service):

sudo service mongod status

4. Stop MongoDB.

For systemd (systemctl):

sudo systemctl stop mongod

For system V Init (service):

sudo service mongod stop

5. Restart MongoDB.

For systemd (systemctl):

sudo systemctl restart mongod

For system V Init (service):

sudo service mongod restart

6. Starting MongoDB.

For systemd (systemctl) & for system V Init (service):


Set Up MongoDB Authentication

1. Connect to the mongo shell:

> mongo

2. List databases using the following command:

> show dbs

admin   0.000GB
config  0.000GB
local   0.000GB

If authentication is not enabled, it will list all the databases available. Otherwise, it will list databases that the user has access based on the role.

3. Add an administrative user.

We need to first connect to the database before creating the user. To connect, type the following:

> use admin
switched to db admin

Now, we can use db.createUser() method to create a user which requires a username, password, and any roles you want to assign to the user.

> db.createUser({
    user: "myuser",
    pwd: passwordPrompt(),
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]

If you execute the above command, you will be asked to enter a password in the next step.
Enter the password of your choice, and you will get confirmation that user been added:

> Enter the password:
Successfully added user: {
    "user" : "myuser",
    "roles" : [
            "role" : "userAdminAnyDatabase",
            "db" : "admin"

4. Enable authentication.

You need to edit MongoDB’s configuration file mongod.conf to enable authentication. After authentication, users need to provide their username and password to read and modify the data.

In the terminal, type this command:

> sudo nano /etc/mongod.conf

Search for the keyword “security” (which is commented out). Uncomment that section and add the following:

  authorization: "enabled"

Make sure the authorization key is indented with two spaces.

Save and close the file. If you use nano, do so by pressing CTRL + X, Y, then ENTER. Once you have made the changes, restart the mongod daemon for the changes to take effect:

> sudo systemctl restart mongod

5. Test

Now, we need to make sure we are able to connect to the database using our username and password.

To verify authentication, enter this command:

> mongo -u myuser -p --authenticationDatabase admin

You will be prompted to enter the password. Enter it. If you enter the wrong password, you will receive an authentication failed error.

Once signed in, try to list the databases using this command:

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

If you see a list of databases, it confirms that you’ve enabled authentication successfully.

Firewall Configuration

You should be able to access your MongoDB server from any application within the droplet server, such as localhost on its default port 27017.

If you want a remote application to access the MongoDB database on this droplet server, you have to configure a firewall rule to allow access to the database.

We recommend giving access to specific IP addresses, instead of allowing anyone to access MongoDB remotely and using authentication when connecting with the database.

Use the following commands to provide access to a specific IP address:

> sudo ufw allow from your_trusted_server_ip/32 to any port 27017
Rules updated
> sudo ufw enable
Firewall is active and enabled on system startup


You can verify firewall settings by using ufw status. You should see the following:

> sudo ufw status
To                           Action                  From
--                          ------                   ----
27017                        ALLOW              [Remote IP Address]

So far, we have opened port 27017 to receive incoming traffic. Currently, MongoDB is only listening to the local address Now, we need to configure MongoDB to listen to remote addresses as well.

MongoDB Configuration

To edit the mongo.conf file, type this command:

sudo nano /etc/mongod.conf

Go to the network interfaces section under net.bindIp, and add your remote server IP address:

# network interfaces
  port: 27017

Note: Make sure mongodbserverip is the IP of the server where you’ve installed MongoDB. In this example, you can use a droplet public IP address.

Don't forget to add a comma between multiple IP addresses.

Save the file and restart MongoDB to make sure changes are applied.

Monitoring and Backup

You can install a monitoring agent in Cloud Manager to monitor nodes and clusters, and report real-time analytics.

Backups are required when dealing with databases. You can activate the backup, and Cloud Manager will create snapshots with checkpoints to help restore data in the future, if required.

Ready to Get Started?

We learned about DigitalOcean and its developer-friendly platform. And we walked through how to install MongoDB on a Linux (Ubuntu) droplet and manage it. We also looked at a few alternatives to DigitalOcean to get a broad perspective of some of the possibilities available to you.

If you want to install different OS droplets on DigitalOcean, install MongoDB for the corresponding OS.

Ready to get started?

Launch a new cluster or migrate to MongoDB Atlas with zero downtime.