How to Install MongoDB 6.0 on Debian 11 Bullseye

Advertisement

MongoDB is a powerful document-oriented database system. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls this BSON), integrating data in certain types of applications easier and faster. Although it uses memory for storage, it can also write data to disk so that it persists beyond the life of the process. In addition to its speed and flexibility, MongoDB’s other main advantage is its active and vibrant open source community, with drivers and tools for many programming languages. As a result, MongoDB has become one of the most popular databases for modern web applications.

The following tutorial will teach you how to install MongoDB 6.0 Debian 11 Bullseye Desktop or Server by importing the community repository and installing and securing the software.

Install Required Packages

The first step is to install the dependencies needed during the installation; use the following command to install or check that they are present.

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

Import MongoDB Repository

Before you install the latest Community Release of the MongoDB 6.0 series, you will need to install the required MongoDB GPG key with the following.

curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | gpg --dearmor | sudo tee /usr/share/keyrings/mongodb-6.0.gpg > /dev/null

Next, you will add the repository which supports amd64 or arm64 architectures.

Advertisement

echo 'deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-6.0.gpg] http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 main' | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

Before installing, it is a good idea to refresh your apt-cache.

sudo apt-get update

Install MongoDB 6.0

Once you have imported the repository, you can install the MongoDB software. The installation will install the following.

Advertisement

The following packages will be installed as part of the MongoDB-org package.

  • mongodb-org-server – The MongoDB server daemon.
  • mongodb-org-mongos – The MongoDB Mongos daemon.
  • monodb-org-shell – The MongoDB shell.
  • monodb-org-tools – Contains tools for importing, exporting, and other utilities.

In your terminal, use the following command to begin.

sudo apt install mongodb-org -y

To start and activate MongoDB, use the following command.

sudo systemctl enable mongod --now

Verify the installation by testing the database connection and version check with the following terminal command.

Advertisement

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

Example output:

How to Install MongoDB 6.0 on Debian 11 Bullseye

Check MongoDB 6.0 Service Status

Before you continue, it is advised to check the status of MongoDB even though you did a connection test to make sure everything is in working order.

systemctl status mongod

Example:

How to Install MongoDB 6.0 on Debian 11 Bullseye

The following commands will provide systemd operations in managing the MongoDB service.

Advertisement

To start MongoDB:

sudo systemctl start mongod

To stop MongoDB:

Advertisement

sudo systemctl stop mongod

To enable MongoDB on system startup:

sudo systemctl enable mongod

To disable MongoDB on system startup:

sudo systemctl disable mongod

To restart the MongoDB service:

sudo systemctl restart mongod

Configure MongoDB Security

The next in configuring MongoDB is the need to modify the MongoDB configuration file named mongod.conf, which is located in the /etc directory. You will uncomment the security section, or else any users on the system will access any database and perform any actions, leaving the databases more exposed to possible attacks.

Open the mongod.conf file using nano editor.

sudo nano /etc/mongod.conf

Next, uncomment the following line and add authorization: enabled as follows.

Advertisement
Advertisement

security:
   authorization: enabled

Example:

How to Install MongoDB 6.0 on Debian 11 Bullseye

Once complete, perform a restart of the MongoDB service using the following systemctl command.

sudo systemctl restart mongod

Create Admin User on MongoDB

By default on MongoDB, the admin account is insecure, which is why authorization was enabled. You need to create an account to access the Admin database securely.

mongosh

Please be mindful that the mongo command is obsolete. You must use the mongosh command instead. Please remember this; you can see it here from the official website.

Now inside the MongoDB shell, connect to the admin database.

use admin

Example output:

Advertisement

How to Install MongoDB 6.0 on Debian 11 Bullseye

Next, type the following to create a new Mongo user account. For the tutorial, a user mongouser will be made.

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

Remember, change the pwd: “PASSWORDCHANGE”, do not just blindly copy this without editing first, and optionally rename the admin if you wish.

Advertisement

Example output if successful:

How to Install MongoDB 6.0 on Debian 11 Bullseye

Now you can exit MongoDB with the following.

quit()

Next, test MongoDB with the new account you created connection status.

mongosh --port 27017 --authenticationDatabase "admin" -u "mongouser" -p

Example output:

Advertisement

How to Install MongoDB 6.0 on Debian 11 Bullseye

The above output states that the user mongouser has connected to the admin database. This is, of course, just a test. You can rename the databases and rename users as you would going forward.

To exit MongoDB with the following.

quit()

How to Update MongoDB

Updates are done with the standard terminal command sudo apt update and upgrade.

sudo apt update && sudo apt upgrade 

New versions do come frequently, given you are using the Community Edition. Remember to keep a snapshot of your database if things break, especially in a production environment.

Advertisement

How to Remove (Uninstall) MongoDB

To remove MongoDB, use the following command.

sudo apt remove mongodb-org

This will remove MongoDB from your system. However, many packages will remain that are no longer needed. To remove everything, use the following command instead.

Advertisement

sudo apt autoremove mongodb-org

The next step is if you no longer want to use, for example, MongoDB 6.0 series repository, you can safely remove this using the following command.

sudo rm /etc/apt/sources.list.d/mongodb-org-6.0.list

Now you can install an alternative branch of the MongoDB repository if you wish.

Comments and Conclusion

MongoDB is a powerful document-oriented database system with many benefits for your business. It is classified as a NoSQL database, which eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas. This makes integrating data in certain types of applications easier and faster. Although it uses memory for storage, it can also write data to disk so that you don’t lose any information if there is a power outage or other issue.

Share on: