How to install MongoDB on a Debian 9 VPS or Dedicated Server

How to install MongoDB on a Debian 9 VPS or Dedicated Server


MongoDB is a scalable, open-source NoSQL document database that stores data in JSON-like documents. It is a free database system which is distributed at its core to be highly available and facilitate built-in geographical distribution.

MongoDB is utilized in numerous modern web application and can easily be installed on Debian 9 systems. This tutorial will show you how to deploy MongoDB version 4.0 on your Debian 9 system at a good MongoDB hosting service.

Ready? Let’s get started!

Before You Begin

For this tutorial to run successfully, you require the following:

  • A fully configured Debian 9 VPS or dedicated server

Step 1 – Downloading and Installing MongoDB

Before you proceed with the installation, visit the official MongoDB websites and check the most recent version of MongoDB. It always nice to deploy the latest version!

Once you are sure of the MongoDB version you want to install, follow the steps below to get the application up and running.

  • First, run the command below to install all the packages needed to support the addition of a new repository.
$ sudo apt install software-properties-common dirmngr
  • Next, run the command below add a GPG key for MongoDB to your Debian system:
$ sudo apt-key adv --keyserver hkp:// --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
  • Once the GPG key is imported, run the command below to add a new MongoDB repository:
$ sudo echo"deb stretch/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
  • Now, execute the command below to update your local package index:
$ sudo apt-get update
  • By now, the platform is set, and it’s easy to install MongoDB package. Issue the command below to install this package:
$ sudo apt-get install -y mongodb-org

Apt-get will install MongoDB package which includes the following components: mongodb-org-server, mongodb-org-mongos, mongodb-org-shell, and mongodb-org-tools. In addition, apt-get will automatically upgrade all the components of MongoDB packages whenever a new version is availed.

If you do not want these packages to be upgraded automatically, pin them at their current version. Run the commands below to achieve this:

$ sudo echo"mongodb-org hold" | sudo dpkg --set-selections
$ sudo echo"mongodb-org-server hold" | sudo dpkg --set-selections
$ sudo echo"mongodb-org-shell hold" | sudo dpkg --set-selections
$ sudo echo"mongodb-org-mongos hold" | sudo dpkg --set-selections
$ sudo echo"mongodb-org-tools hold" | sudo dpkg --set-selections

Step 2 – Managing MongoDB

By now, the MongoDB is successfully installed and its time you learn how to manage this service.

Starting And Enabling MongoDB

Now, issue the command below to start MongoDB and enable the service to start automatically on boot:

$ sudo systemctl start mongod
$ sudo systemctl enable mongod

Verifying The MongoDB Service

Once, you start MongoDB, you can now run the command below to check the status:

$ sudo systemctl status mongod

This will give you an output similar to the one below:

   Loaded: loaded(/lib/systemd/system/mongod.service;enabled;vendor preset: enabled)
   Active: active(running)sinceWed2018-10-1017:59:56UTC;6sago
 Main PID: 4321(mongod)
    Tasks: 31
   CGroup: /system.slice/mongod.service

This output indicates that MongoDB service is running correctly. Next, you should verify the status further by connecting to a database system and  issuing a diagnostic command:

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

This will give you an output showing your database version, status command output, and server port and address:

MongoDB shell version v4.0.2
connecting to: mongodb://
MongoDB server version: 4.0.2
    "authInfo" : {
        "authenticatedUsers" : [ ],
        "authenticatedUserRoles" : [ ]
    "ok" : 1

This line “ok” : 1 shows that your server is functioning properly.

Stopping MongoDB

To stop your MongoDB service execute the command:

$ sudo systemctl stop mongod

Restarting MongoDB

Then, to restart the MongoDB service issue the command:

$ sudo systemctl restart mongod

Disabling MongoDB

If you want to deactivate automatic MongoDB startup, issue the command;

$ sudo systemctl disable mongod

Step 3 – Uninstalling MongoDB

If for any reason you want to uninstall the MongoDB service, follow the steps below:

  • First, issue the command below to stop the service:
$ sudo systemctl stop mongod
  • Then, run the command below to remove all the packages:
$ sudo apt-get purge mongodb-org*
  • Next, execute the commands below to remove the data directions.
$ sudo rm -r /var/log/mongodb
$sudorm -r /var/lib/mongodb


You have learned how to install and manage the MongoDB on your Debian 9 system. You can refer to the MongoDB documentation for in-depth tutorial and guides on the possibilities delivered by MongoDB.

Check out these top 3 VPS services:

$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
  • Support
  • Features
  • Reliability
  • Pricing
$2.95 /mo
Starting price
Visit ScalaHosting
Rating based on expert review
  • User Friendly
  • Support
  • Features
  • Reliability
  • Pricing
Liquid Web
$5.00 /mo
Starting price
Visit Liquid Web
Rating based on expert review
  • User Friendly
  • Support
  • Features
  • Reliability
  • Pricing

Installing MongoDB on Ubuntu 22.04 – A Complete Guide

This tutorial will help you install and configure MongoDB 5.0.9 on your Ubuntu 2
7 min read
Bruno Mirchevski
Bruno Mirchevski
Hosting Expert

How to Secure phpMyAdmin on Ubuntu 18.04 VPS or Dedicated Server

Learn how to secure your phpMyAdmin installation and secure your MySQL/MariaDB f
3 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install MYSQL 8.0 and Create a Database on an Ubuntu 18.04 Linux VPS

This article shows how to install MySQL 8.0 on an Ubuntu 18.04 VPS. In addition,
4 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Install phpMyAdmin on a CentOS 7 VPS or Dedicated Server

Learn how to setup phpMyAdmin on your CentOs 7 VPS server and manage your MySQL/
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top