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

Introduction

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://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
  • Once the GPG key is imported, run the command below to add a new MongoDB repository:
$ sudo echo"deb http://repo.mongodb.org/apt/debian 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:

●mongod.service-MongoDBDatabaseServer
   Loaded: loaded(/lib/systemd/system/mongod.service;enabled;vendor preset: enabled)
   Active: active(running)sinceWed2018-10-1017:59:56UTC;6sago
     Docs: https://docs.mongodb.org/manual
 Main PID: 4321(mongod)
    Tasks: 31
   CGroup: /system.slice/mongod.service
           └─4321/usr/bin/mongod--config/etc/mongod.conf

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://127.0.0.1:27017
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

Conclusion

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:

HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3
Ultahost
$2.90 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8

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
HostAdvice.com 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