Navigation

Install MongoDB Community Edition on Debian

Overview

Use this tutorial to install MongoDB Community Edition from .deb packages on Debian.

MongoDB only provides packages for 64-bit builds of Debian 8 and 9. See Supported Platforms for more information.

Important

The unofficial mongodb package provided by Debian is not maintained by MongoDB and conflict with MongoDB’s offically supported packages. You should always use the official MongoDB mongodb-org packages, which are kept up-to-date with the most recent major and minor MongoDB releases.

Packages

MongoDB provides officially supported packages in their own repository. This repository contains the following packages:

Package Name Description
mongodb-org A metapackage that will automatically install the four component packages listed below.
mongodb-org-server

Contains the mongod daemon, associated init script, and a configuration file (/etc/mongod.conf). You can use the initialization script to start mongod with the configuration file. For details, see Run MongoDB Community Edition.

The /etc/mongod.conf configuration file supplied by the package have bind_ip set to 127.0.0.1 by default. Modify this setting as needed for your environment before initializing a replica set.

mongodb-org-mongos Contains the mongos daemon.
mongodb-org-shell Contains the mongo shell.
mongodb-org-tools Contains the following MongoDB tools: mongoimport bsondump, mongodump, mongoexport, mongofiles, mongorestore, mongostat, and mongotop.

Important

The unofficial mongodb package provided by Debian is not maintained by MongoDB and conflict with MongoDB’s offically supported packages. You should always use the official MongoDB mongodb-org packages, which are kept up-to-date with the most recent major and minor MongoDB releases.

Install MongoDB Community Edition

Note

To install a different version of MongoDB, please refer to that version’s documentation. To install the previous version, see the tutorial for version 3.6.

This installation guide only supports 64-bit systems. See Supported Platforms for more information.

Using .tgz Tarballs

MongoDB .tar.gz tarballs require installing the following dependencies:

sudo apt-get install libcurl3 openssl

Procedure

1

Download the MongoDB .tar.gz tarball.

Download the tarball for your system from the MongoDB Download Center.

2

Extract the files from the downloaded archive.

For example, from a system shell, you can extract using the tar command:

tar -zxvf mongodb-linux-*-4.0.2.tgz
3

Ensure the binaries are in a directory listed in your PATH environment variable.

The MongoDB binaries are in the bin/ directory of the tarball. You must either:

  • Copy these binaries into a directory listed in your PATH variable such as /usr/local/bin,
  • Create symbolic links to each of these binaries from a directory listed in your PATH variable, or
  • Modify your user’s PATH environment variable to include this directory.

For example, you can add the following line to your shell’s initialization script (e.g. ~/.bashrc):

export PATH=<mongodb-install-directory>/bin:$PATH

Replace <mongodb-install-directory> with the path to the extracted MongoDB archive.

Run MongoDB Community Edition

Most Unix-like operating systems limit the system resources that a session may use. These limits may negatively impact MongoDB operation. See UNIX ulimit Settings for more information.

The MongoDB instance stores its data files in /var/lib/mongodb and its log files in /var/log/mongodb by default, and runs using the mongodb user account. You can specify alternate log and data file directories in /etc/mongod.conf. See systemLog.path and storage.dbPath for additional information.

If you change the user that runs the MongoDB process, you must modify the access control rights to the /var/lib/mongodb and /var/log/mongodb directories to give this user access to these directories.

1

Start MongoDB.

Issue the following command to start mongod:

sudo service mongod start
2

Verify that MongoDB has started successfully

Verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading

[initandlisten] waiting for connections on port 27017

<port> is the port the mongod listens on. If you modified the net.port setting in the /etc/mongod.conf configuration file, the port may differ.

If you modified the systemLog.path configuration file option, look for the log file at the location you specified to that setting.

You may see non-critical warnings in the mongod output. As long as you see the log line shown above, you can safely ignore these warnings during your initial evaluation of MongoDB.

3

Stop MongoDB.

As needed, you can stop the mongod process by issuing the following command:

sudo service mongod stop
4

Restart MongoDB.

Issue the following command to restart mongod:

sudo service mongod restart
5

Begin using MongoDB.

Start a mongo shell on the same host machine as the mongod. Use the --host command line option to specify the localhost address (in this case 127.0.0.1) and port that the mongod listens on:

mongo --host 127.0.0.1:27017

Later, to stop MongoDB, press Control+C in the terminal where the mongod instance is running.

Uninstall MongoDB Community Edition

To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs. The following section guides you through the necessary steps.

Warning

This process will completely remove MongoDB, its configuration, and all databases. This process is not reversible, so ensure that all of your configuration and data is backed up before proceeding.

1

Stop MongoDB.

Stop the mongod process by issuing the following command:

sudo service mongod stop
2

Remove Packages.

Remove any MongoDB packages that you had previously installed.

sudo apt-get purge mongodb-org*
3

Remove Data Directories.

Remove MongoDB databases and log files.

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb