Site icon LinuxTuto

How to Install MongoDB 5 on Debian 11

MongoDB 5.0

MongoDB is an open-source document database used in many modern web applications. It is classified as a NoSQL database. Unlike relational databases which store data in tables according to a rigid schema, MongoDB stores data in documents with flexible schema. Applications can then retrieve this information in a JSON format.

Update Operating System

Update your Debian 11 operating system to make sure all existing packages are up to date:

$ sudo apt update && sudo apt upgrade -y

Install Dependencies

Install the required packages for the installation with the following command:

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

Import MongoDB GPG Key

We first need to import MongDB public GPG key as below:

$ wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo  apt-key add -

The output of the executed command should be “OK”.

Add MongoDB 5.0 Repository

MongoDB 5.0 packages are not available to install directly from the base repository of Debian 11 and we need to add the official one offered by the developers of this NoSQL database:

echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Update the repository:

$ sudo apt update

Install MongoDB on Debian 11

Now we will Install MongoDB with the following command:

$ sudo apt-get install -y mongodb-org

By default, MongoDB service is not started. Start the MongoDB service:

$ sudo systemctl start mongod

Confirm that MongoDB is actually running:

$ sudo systemctl status mongod

Output:

 mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2022-04-03 08:02:06 EDT; 9s ago
Docs: https://docs.mongodb.org/manual
Main PID: 6197 (mongod)
Memory: 66.4M
CPU: 974ms
CGroup: /system.slice/mongod.service
└─6197 /usr/bin/mongod --config /etc/mongod.conf

Apr 03 08:02:06 debian systemd[1]: Started MongoDB Database Server.

To check the version of MongoDB which is installed.

$ sudo mongod --version
 version v5.0.6
Build Info: {
    "version": "5.0.6",
    "gitVersion": "212a8dbb47f07427dae194a9c75baec1d81d9259",
    "openSSLVersion": "OpenSSL 1.1.1k  25 Mar 2021",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "debian10",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Let us enable it to start on boot using this command:

$ sudo systemctl enable mongod

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

$ sudo systemctl stop mongod

Configure MongoDB

By default, the configuration file for MongoDB is located at “/etc/mongod.conf”.

Enable password authentication

To enable password authentication open the configuration file:

$ sudo nano /etc/mongod.conf

Find the line #security and uncomment it (remove the #) and add “authorization: enabled”:

security:
  authorization: enabled

After that restart the mongod service for the changes to take effect:

$ sudo systemctl restart mongod

Enable remote access

To enable remote access to your MongoDB database, you need to edit the configuration file at “/etc/mongod.conf”.

$ sudo nano /etc/mongod.conf
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,your-server-IP

After you’ve added the IP, restart the service:

$ sudo systemctl restart mongod

You also need to allow on the firewall the trusted remote IP addresses if you have enabled your firewall:

$ sudo ufw allow from your-server-ip to any port 27017

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 access MongoDB shell, run the command mongosh on the terminal as shown:

$ mongosh

List existing databases:

show dbs
admin 41 kB
config 36.9 kB
local 41 kB

Connect to the admin database:

use admin

Create a admin user which will be used to manage the MongoDB:

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

Exit the mongo shell.

exit

Try to connect to MongoDB using a admin user you have previously created:

$ sudo mongosh -u admin -p --authenticationDatabase admin

You can try to connect to MongoDB by typing mongosh without any arguments.

Once connected, you can execute show dbs command. You will get error:

MongoServerError: command listDatabases requires authentication

Uninstall MongoDB

If you want to completely remove MongoDB and related dependencies, run the following command:

$ sudo apt purge --autoremove -y mongodb-org

You can also remove MongoDB logs, data, and other related directories and files:

$ sudo rm -rf /var/log/mongodb
$ sudo rm -rf /var/lib/mongodb
$ sudo rm -rf ~/.mongodb
$ sudo rm -rf ~/.dbshell
$ sudo rm -rf ~/.mongorc.js

Comments and Conclusion

That’s it. You have successfully installed MongoDB 5.0 on Debian 11.

If you have any questions please leave a comment below.

Exit mobile version