How To Install MongoDB 4.2 / 4.0 On Debian 9


MongoDB is an open-source cross-platform, document-oriented database and it was designed with both scalability and developer agility in mind. MongoDB is written in C, C++, and JavaScript, and it uses JSON-like documents with dynamic schemas to store data.

MongoDB is released under the Server Side Public License (SSPL), and the language drivers are available under an Apache License.

MongoDB is available for Linux, Windows, OS X, FreeBSD, and Solaris.


Add MongoDB Repository

MongoDB Inc releases stable packages for Debian 9 and Debian 8, and their packages are generally fresher than those in the Debian repositories.

You should always use the official mongodb-org package.

The mongodb-org package might conflict with the mongodb packages in the Debian repository (If you have it already installed).

Install the certificate server for downloading and managing certificates.

sudo apt update

sudo apt install -y dirmngr gnupg

Add the MongoDB repository to the system.

MongoDB 4.2

wget -qO - | sudo apt-key add -

echo "deb stretch/mongodb-org/4.2 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

MongoDB 4.0

wget -qO - | sudo apt-key add -

echo "deb jessie/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Install MongoDB

Update the repository index.

sudo apt update

Install the MongoDB packages using the apt command. The name of the MongoDB package is mongodb-org.

sudo apt install -y mongodb-org

Post Installation

File System (Optional)

We recommend you use the XFS filesystem for MongoDB (/var/lib/mongodb).

Access Control

By default, MongoDB doesn’t have authentication mechanisms enabled, which means anyone can read and modify data. To restrict access, you would need to enable access control that allows only identified users to perform actions based on their roles.

sudo nano /etc/mongod.conf

Add the below lines.

  authorization: enabled

Start Service

MongoDB services can be started/stopped by the easy known commands.

To start MongoDB service, run.

sudo systemctl start mongod

To enable MongoDB service to start automatically on system startup, run:

sudo systemctl enable mongod

To check the status of MongoDB service, run.

sudo systemctl status mongod


 mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-02-25 16:59:57 UTC; 4s ago
 Main PID: 31859 (mongod)
    Tasks: 32
   CGroup: /system.slice/mongod.service
           └─31859 /usr/bin/mongod --config /etc/mongod.conf

Feb 25 16:59:57 debian9 systemd[1]: Stopped MongoDB Database Server.

Confirm the version of MongoDB.

mongod --version


db version v4.2.3
git version: 6874650b362138df74be53d366bbefc321ea32d4
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019
allocator: tcmalloc
modules: none
build environment:
    distmod: debian92
    distarch: x86_64
    target_arch: x86_64

Access MongoDB

Connect to MongoDB shell by using the mongo command.



MongoDB shell version v4.2.3
connecting to: mongodb://
Implicit session: session { "id" : UUID("68f5ba26-6bdc-44ac-87c9-22a1fb398cc8") }
MongoDB server version: 4.2.3


That’s All. I hope you have learned how to install MongoDB on Debian 9. Please share your feedback in the comments section.

You might also like