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:

Was this article helpful?