How to quickly setup MongoDB on DigitalOcean

MongoDB is the most popular NoSQL database. Let’s quickly setup a single MongoDB server on an DigitalOcean Droplet in 4 easy steps. We can run multiple mongo databases on this system and keep them separated and secure with different user permissions.

Johannes Linowski
Feb 24, 2017 · 3 min read

1. Create the MongoDB droplet

Log in your DigitalOcean Account and create your new MongoDB droplet based on the newest MongoDB image.

  • Choose “Create Droplet”
  • One-click apps => Newest MongoDB image (actually MongoDB 3.2.12 on 16.04)
  • Choose a size => I recommend at least 2GB RAM = 20$/month. Smaller instances can quickly run in overload errors!
  • Select the datacenter region of your choice
  • Access and naming is up to you. It does not matter for this instruction.
Creation of MongoDB Instance in DigitalOcean

Now, we have a virtual ubuntu instance with preinstalled MongoDB.

2. Open MongoDB & droplet for access from the world

To access the database from outside the droplet, let’s connect to the instance (insert your droplet IP and DigitalOcean access key):

ssh -i /Users/yourusername/.ssh/yourkey root@123.123.123.123

Now, let’s bind the MongoDB to localhost. To do this, edit the file /etc/mongod.conf with your favorite editor and change bindIp to 0.0.0.0.

bindIp: 0.0.0.0

We also have to allow access to the MongoDB port from outside the droplet. DigitalOcean uses ufw (“uncomplicated firewall”) as a default on Ubuntu instances, so we can simply type:

ufw allow 27017

Perfect! A restart of MongoDB will activate the settings:

sudo service mongod restart

The MongoDB database is now ready to use. You can connect to your database from everywhere. But actually, there is no security. Everyone can create and delete databases, collections and entries. In the next step, we protect the database with the MongoDB authorization system.

3. Enable MongoDB Authorization

Let’s make our database secure. Sometimes there are problems with the the language environment variables in some regions. We can prevent this with a simple command:

export LC_ALL=C

Now let’s enter the MongoDB shell and connect to the admin database:

mongo
use admin

Execute the following multi line command to create an admin user. Don’t forget to choose a secure password:

db.createUser(
{
user: "adminUser",
pwd: "yourSecretPassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

The build-in MongoDB authorization system in disabled by default. To activate it, quit the MongoDB shell and change/add the corresponding lines in the file /etc/mongod.conf:

security:
authorization: enabled

We’re done with authorization! Simply restart MongoDB to apply the changes.

sudo service mongod restart

Our MongoDB is secure now.

4. Create databases and users

We’re ready to go. Now, we can create databases and database users. We can do this with the MongoDB shell. I prefer to use an interface to create databases and database users. I can recommend the desktop application Studio3T (previously called ‘MongoChef’). Use the following order to create databases and users. You can do all the steps with the Studio3T interface:

1. Connect to the MongoDB instance with your admin credentials (see step 3):

2. Create a database (or multiple databases)

3. Create users for this database(s) and grant the correct roles (read / readWrite) by selecting the created database and click on “Users”

4. Disconnect your admin user. The admin user is not able to see/create database details like collections or entries. Connect to a single database with the corresponding created user.

You’re ready to create great applications! Have fun.

NinjaConcept

Hi! We’re NinjaConcept, a web consultancy with leading-edge development skills and a passion for outstanding user experience design.

Johannes Linowski

Written by

CTO at Foodly www.getfoodly.com | FullStack-WebDeveloper from Germany/Karlsruhe. In ♥ with NodeJS, React, ReactNative, MongoDB, Docker, NextJS..

NinjaConcept

Hi! We’re NinjaConcept, a web consultancy with leading-edge development skills and a passion for outstanding user experience design.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store