How to Install MongoDB on Debian 9

By 

Published 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.

Prerequisites

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:

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

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

    Terminal
    sudo add-apt-repository 'deb https://repo.mongodb.org/apt/debian 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:

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

    Terminal
    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:

    Terminal
    sudo systemctl start mongod
    sudo 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:

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

    The output will look like this:

    output
    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
    }

    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:

/etc/mongod.confyaml
security:
  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:

Terminal
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:

Terminal
mongo

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

json
use admin
output
switched to db admin

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

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

Exit the mongo shell with:

json
quit()

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

Terminal
mongo -u mongoAdmin -p --authenticationDatabase admin

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

json
use admin
output
switched to db admin

Now, print the users with:

json
show users
output
{
	"_id" : "admin.mongoAdmin",
	 : "mongoAdmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

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.

Conclusion

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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page