MongoDB  is a document-oriented database management system that can be distributed over any number of computers and does not require a predefined data schema. It is written in C++. The server and tools are distributed under the AGPL license (Is a free copyleft license, intended to oblige the services accessible by the network to publish their source code).


Ubuntu 16.04 or Centos 7.x server set up by following this initial server setup tutorial, including a sudo non-root user.

Add the MongoDB Repository

You have to import their key for the official MongoDB repository.

sudo apt-key adv --keyserver hkp:// --recv EA312927
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

Create a list file for MongoDB.

echo "deb xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Update the packages list.

sudo apt-get update

Installing and Verifying MongoDB

sudo apt-get install -y mongodb-org

Create a unit file to manage the MongoDB service. Create a configuration file named mongodb.service in the /etc/systemd/system directory using nano or your favorite text editor.

sudo nano /etc/systemd/system/mongodb.service

Copy/Paste in the following contents, then save and close the file.

Description=High-performance, schema-free document-oriented database

ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

  • The Unit section contains the overview (e.g. a human-readable description for MongoDB service) as well as dependencies.
  • The Service section how the service should be started.
  • The last section, Install, tells systemd when the service should be automatically started. The is a standard system startup sequence, which means the server will be automatically started during boot.

Start MongoDB

sudo systemctl start mongodb

Use systemctl to check that the service has started properly.

sudo systemctl status mongodb
● mongodb.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/etc/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2016-04-25 14:57:20 EDT; 1min 30s ago
 Main PID: 4093 (mongod)
    Tasks: 16 (limit: 512)
   Memory: 47.1M
      CPU: 1.224s
   CGroup: /system.slice/mongodb.service
           └─4093 /usr/bin/mongod --quiet --config /etc/mongod.conf

Enable automatically starting MongoDB when the system starts.

sudo systemctl enable mongodb

Adjusting the Firewall (Optional)

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

Allow access to MongoDB on its default port 27017 from everywhere:

sudo ufw allow from your_other_server_ip/32 to any port 27017

Check the change in firewall settings with ufw.

sudo ufw status
Status: active

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

Leave a Reply