How to Install MongoDB on Debian 10 Buster

How to Install MongoDB on Debian 10 Buster

In this MongoDB tutorial, we will explain how to install and configure the latest version of MongoDB on Debian 10 Buster. Learn how to Install MongoDB on Debian 10 Linux. Learn how To Install MongoDB 4 on Debian 10 (Buster)

MongoDB is a free and open-source document database. It belongs to a family of databases called NoSQL, which is 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 explain how to install and configure the latest version of MongoDB Community Edition on Debian 10 Buster.

Installing MongoDB

MongoDB is not available in the standard Debian Buster repositories. We'll enable the official MongoDB repository and install the packages.

At the time of writing this article, the latest version of MongoDB is version 4.2. Before starting with the installation, head over to the Install on Debian page of MongoDB’s documentation and check if there is a new version available.

Perform the following steps as root or user with sudo privileges to install MongoDB on a Debian system:

  1. Install the packages required for adding a new repository:

    sudo apt install dirmngr gnupg apt-transport-https software-properties-common ca-certificates curl
  2. Add the MongoDB GPG key to your system:

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

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

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

  1. Update the packages list and install the mongodb-org meta-package:

    sudo apt update
    sudo apt install mongodb-org

The following packages will be installed on the 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.
  1. Start the MongoDB service and enable it to start on boot:

    sudo systemctl enable mongod --now
  2. To verify whether the installation has completed successfully, 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.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 for the ok field indicates success.

Configuring MongoDB

The MongoDB configuration file is named mongod.conf and is located in the /etc directory. The file is in YAML format.

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:

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 can access all databases and perform any action.

After editing the configuration file, restart the mongod service for changes to take effect:

sudo systemctl restart mongod

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

Creating Administrative MongoDB User

If you enabled the MongoDB authentication, you'll need to create an administrative user that can access and manage the MongoDB instance. To do so, access the mongo shell with:

mongo

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

db.createUser(
  {
    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:

quit()

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",
    "userId" : UUID("cdc81e0f-db58-4ec3-a6b8-829ad0c31f5c"),
    "user" : "mongoAdmin",
    "db" : "admin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ],
    "mechanisms" : [
        "SCRAM-SHA-1",
        "SCRAM-SHA-256"
    ]
}

Conclusion

We have shown you how to install MongoDB 4.2 on Debian 10, Buster. Visit the MongoDB Manual for more information on this topic.

If you hit a problem or have feedback, leave a comment below.

MongoDB Linux Databases

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

MongoDB Database and java applications

Enroll for free demo to acquire the best knowledge on the schema-less database from live industry experts through MongoDB training

MongoDB – Database, Collection, and Document

Our MongoDB Online Training provide you to learn about MongoDB strategies with realty. Our MongoDB Online Training also includes live sessions, live Projects, and much

MongoDB brings database product range under one cloudy roof

Enroll for free demo to acquire the best knowledge on the schema-less database from live industry experts through MongoDB training

Hire Dedicated Linux Developer

Looking to develop real-time applications? **[Hire Dedicated Linux Developer](https://hourlydeveloper.io/hire-dedicated-linux-developer/ "Hire Dedicated Linux Developer")** from **[HourlyDeveloper.io](https://hourlydeveloper.io/...

User Administration in Linux - Linux Tutorial - Linux Training

User Administration in Linux - Linux Tutorial - Linux Training