How to Install MongoDB on Debian 9

Updated on

4 min read

Install MongoDB on Debian 9

MongoDB is a free and open-source document database. It belongs to a family of databases called NoSQL which are different from the traditional table-based SQL databases like MySQL and PostgreSQL.

In MongoDB, data is stored in flexible, JSON-like documents where fields can vary from document to document. It does not require a predefined schema and data structure can be changed over time.

In this tutorial, we will show you how to install and configure the latest version of MongoDB Community Edition on a Debian 9 systems from the official MongoDB repositories.


Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges .

Installing MongoDB

At the time of writing this article, the latest version of MongoDB is version 4.0.

Before continuing with the installation process, head over to the Install on Debian section of MongoDB’s documentation and check if there is a new release available.

The following steps describe how to install MongoDB on a Debian system:

  1. First, install the packages required for adding a new repository:

    sudo apt install software-properties-common dirmngr
  2. Add the MongoDB GPG key to your system using the following command:

    sudo apt-key adv --keyserver hkp:// --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
  3. Once the key is imported, to add the MongoDB repository run:

    sudo add-apt-repository 'deb stretch/mongodb-org/4.0 main'

    If you want to install an older version of MongoDB, replace 4.0 with your preferred version.

  4. Update the packages list:

    sudo apt update
  5. Install the mongodb-org meta-package with:

    sudo apt install mongodb-org

    The following packages will be installed on your system as a part of the mongodb-org package:

    • mongodb-org-server - The mongod daemon and corresponding init scripts and configurations.
    • mongodb-org-mongos - The mongos daemon.
    • mongodb-org-shell - The mongo shell is an interactive JavaScript interface to MongoDB. It is used to perform administrative tasks through the command line.
    • mongodb-org-tools - Contains several MongoDB tools for importing and exporting data, statistics, as well as other utilities.
  6. Start the MongoDB daemon and enable it to start on boot by typing:

    sudo systemctl start mongodsudo systemctl enable mongod
  7. To verify whether the installation has completed successfully we will connect to the MongoDB database server using the mongo tool and print the connection status:

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

    The output will look like this:

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

    A value of 1 for the ok field indicates success.

Configuring MongoDB

MongoDB uses a YAML formatted configuration file, /etc/mongod.conf . You can configure your MongoDB instance by editing this file.

The default configuration settings are sufficient for most users. However, for production environments, it is recommended to uncomment the security section and enable authorization as shown below:

  authorization: enabled

The authorization option enables Role-Based Access Control (RBAC) that regulates users access to database resources and operations. If this option is disabled each user will have access to all databases and perform any action.

After making changes to the MongoDB configuration file, restart the mongod service with:

sudo systemctl restart mongod

To find more information about the configuration options available in MongoDB 4.0 visit the Configuration File Options documentation page.

Creating Administrative MongoDB User

If you enabled the MongoDB authentication, create an administrative MongoDB user that will be used to access and manage the MongoDB instance.

First access the mongo shell with:


Once you are inside the MongoDB shell type the following command to connect to the admin database:

use admin
switched to db admin

Issue the following command to create a new user named mongoAdmin with the userAdminAnyDatabase role:

    user: "mongoAdmin", 
    pwd: "changeMe", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
You can name the administrative MongoDB user as you want.

Exit the mongo shell with:


To test the changes, access the mongo shell using the administrative user you have previously created:

mongo -u mongoAdmin -p --authenticationDatabase admin

Enter the password when prompted. Once you are inside the MongoDB shell connect to the admin database:

use admin
switched to db admin

Now, print the users with:

show users
	"_id" : "admin.mongoAdmin",
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
	"mechanisms" : [

You can also try to access the mongo shell without any arguments ( just type mongo) and see if you can list the users using the same commands as above.


You have learned how to install and configure MongoDB 4.0 on your Debian 9 server.

You can consult The MongoDB 4.0 Manual for more information on this topic.