How to Install MongoDB on Debian 10 Linux

Choose a different version or distribution

Introduction

Before we begin talking about how to install MongoDB on Debian 10 Linux, let's briefly understand – What is MongoDB?

MongoDB is an open-source document database that is free to use. It is distinct from conventional table-based SQL databases like MySQL and PostgreSQL, and is a member of the NoSQL database family.

Data in MongoDB is kept as flexible, JSON-like documents, with fields that can vary from document to document. There is no need for a specified schema, and the data structure can be changed over time.

In this tutorial, you will install MongoDB on Debian 10 Linux. We will also address a few FAQs on how to install MongoDB on Debian 10 Linux.

Advantages of MongoDB

  1. Flexible schema: MongoDB's document-based model allows for schema flexibility, making it easier to adapt to changing data requirements.
  2. Scalability: MongoDB's horizontal scaling capability allows for seamless growth as your data and application demand increases.
  3. High performance: With its ability to index and shard data, MongoDB delivers fast query responses, ensuring efficient data access.
  4. Easy integration: MongoDB supports easy integration with popular programming languages and tools, simplifying the development process.
  5. Comprehensive documentation and community support: MongoDB benefits from a robust community and extensive documentation, providing helpful resources for developers.

Installing MongoDB

The standard Debian Buster repositories do not include MongoDB. We will enable the MongoDB official repository and install the packages.

The most recent version of MongoDB is 4.2 as of the time this article was written. Go to the Install on Debian page of MongoDB's documentation and see if a new version is available before beginning the installation.

In order to install MongoDB on a Debian system, carry out the subsequent steps as root or a user with sudo privileges:

1) Install the packages necessary for setting up a new repository:

sudo apt install dirmngr gnupg apt-transport-https software-properties-common ca-certificates curl

2) The MongoDB GPG key should be added to your system:

curl -fsSL https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

3) Activate the MongoDB repository:

sudo add-apt-repository 'deb https://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main'

Packages with earlier versions of MongoDB are not available for Debian 10.

4) Install the mongodb-org meta-package and update the list of installed packages:

sudo apt update
sudo apt install mongodb-org

As part of the mongodb-org package, the following packages will be installed on the system:

  • mongodb-org-server - The mongod daemon and associated init scripts and configurations.
  • mongodb-org-mongos - The mongos daemon.
  • mongodb-org-shell - The mongo shell is an immersive JavaScript interface to MongoDB. It is used to carry out administrative activities through the command line.
  • mongodb-org-tools - Includes several MongoDB tools for importing and exporting data, and statistics, along with additional utilities.

5) Launch the MongoDB service and set it to start on boot:

sudo systemctl enable mongod --now
💡
If facing connecting to mongod, then check if /data/db directory exists, else create using the command sudo mkdir -p /data/db and restart mongod service.

6) To ensure that the installation was successful, use the mongo tool to connect to the MongoDB database server and print the connection status:

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

The output will appear as follows:

MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("09f11c53-605f-44ad-abec-ec5801bb6b06") }
MongoDB server version: 4.2.1
{
	"authInfo" : {
		"authenticatedUsers" : [ ],
		"authenticatedUserRoles" : [ ]
	},
	"ok" : 1
}

A value of 1 in the ok field signifies success.

Configuring MongoDB

The MongoDB configuration file, mongod.conf, can be found in the /etc directory. YAML is the file format.

The default configuration settings are enough for the majority of users. However, it is advised to uncomment the security section and enable authorization for production environments, as demonstrated below:

security:
  authorization: enabled

Role-Based Access Control (RBAC) is a feature of the authorization option that limits users' access to database resources and processes. Every user has access to every database and can execute any operation if this option is disabled.

The mongod service must be restarted for changes to take effect after editing the configuration file:

sudo systemctl restart mongod

Visit the Configuration File Options documentation page to learn more about the configuration options offered in MongoDB 4.2.

Creating Administrative MongoDB User

You must establish an administrative account who can access and control the MongoDB instance if you activated MongoDB authentication. You can accomplish this by opening the mongo shell using:

mongo

To connect to the admin database, enter the following command inside the MongoDB shell:

use admin
Output

switched to db admin

To create a new user named mongoAdmin with the userAdminAnyDatabase role, use the following command:

db.createUser(
  {
    user: "mongoAdmin", 
    pwd: "changeMe", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

💡
You can give the administrative MongoDB user any name you prefer.

Leave the mongo shell by using:

quit()

You can access the mongo shell using the administrative user you previously created to test the modifications:

mongo -u mongoAdmin -p --authenticationDatabase admin

When prompted, enter the password. Connect to the admin database once you are inside the MongoDB shell:

use admin
Output

switched to db admin

Print the users now with:

show users
Output

{
	"_id" : "admin.mongoAdmin",
	"userId" : UUID("cdc81e0f-db58-4ec3-a6b8-829ad0c31f5c"),
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

FAQs to Install MongoDB on Debian 10 Linux

Can I install MongoDB using the default Debian package manager? 

Yes, MongoDB can be installed using the default Debian package manager (apt) by adding the official MongoDB repository to your package sources list.

Does MongoDB provide official packages for Debian 10 Linux? 

Yes, MongoDB provides official packages for Debian, including specific versions for Debian 10 (Buster), which can be downloaded from the official MongoDB repository.

Is there a specific user or group required for running MongoDB on Debian 10 Linux? 

On Debian, MongoDB runs as the "mongodb" user and group. The package installation process creates this user and group automatically.

How can I start or stop the MongoDB service on Debian 10 Linux? 

The MongoDB service can be managed using the systemctl command. Use "systemctl start mongodb" to start it and "systemctl stop mongodb" to stop it.

Where can I find the default configuration file for MongoDB on Debian 10 Linux? 

The default MongoDB configuration file can be found at "/etc/mongod.conf". You can modify this file to customize the MongoDB settings.

How do I verify if MongoDB is running on Debian 10 Linux? 

Run the command "systemctl status mongodb" to check the status of the MongoDB service. If it's running, the output will indicate that it's active.

Are there any important security considerations for MongoDB on Debian 10 Linux? 

Yes, it's crucial to secure your MongoDB installation. By default, MongoDB listens on local interfaces only, but you should consider configuring authentication and securing remote access for production environments. MongoDB documentation provides guidance on implementing security measures.

Conclusion

We demonstrated how to install MongoDB 4.2 on Debian 10, Buster. For more details on this topic, visit the MongoDB Manual.

If you have any suggestions or queries, kindly leave them in the comments section.