Install Mongo 6 Database on Debian 11

This article covers how to install MongoDB (Community Edition Tutorial) on Debian 11. MongoDB is a popular open source NoSQL database. It is a document oriented database that supports structured and unstructured data, including JSON, XML, and organic data.

Source: https://docs.rocket.chat/quick-start/deploying-rocket.chat/other-deployment-methods/manual-installation/debian-based-distros/debian
Update system

# sudo apt update
# sudo apt dist-upgrade -y

You will need wget and GnuPG packages to download the MongoDB repository securely. Install these dependency packages with the following commands:

$ sudo apt install wget
$ sudo apt install gnupg2

Import the GPG key of the MongoDB repository with the following command.

$ wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK

Next, add and enable MongoDB Repository to APT with the following command.

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

View the output:

$ cat /etc/apt/sources.list.d/mongodb-org-6.0.list

Output:

deb http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 main

Update the system:

$ sudo apt update
.........
Ign:4 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 InRelease
Get:5 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 Release [2,400 B]
Get:6 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 Release.gpg [801 B]
Get:7 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0/main amd64 Packages [7,652 B]
.........

Install MongoDB on your system.

$ sudo apt install -y mongodb-org

Output:

.......
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org-database mongodb-org-database-tools-extra
  mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
  mongodb-database-tools mongodb-mongosh mongodb-org mongodb-org-database
  mongodb-org-database-tools-extra mongodb-org-mongos mongodb-org-server mongodb-org-shell
  mongodb-org-tools
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 134 MB of archives.
After this operation, 460 MB of additional disk space will be used.
Get:1 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0/main amd64 mongodb-database-tools amd64                                      100.6.0 [47.7 MB]
Get:2 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0/main amd64 mongodb-mongosh amd64 1.6.0 [                                     37.4 MB]
Get:3 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0/main amd64 mongodb-org-shell amd64 6.0.2                                      [3,080 B]
Get:4 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0/main amd64 mongodb-org-server amd64 6.0.                                     2 [28.8 MB]
Get:5 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0/main amd64 mongodb-org-mongos amd64 6.0.                                     2 [20.3 MB]
Get:6 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0/main amd64 mongodb-org-database-tools-ex                                     tra amd64 6.0.2 [7,748 B]
Get:7 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0/main amd64 mongodb-org-database amd64 6.                                     0.2 [3,536 B]
Get:8 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0/main amd64 mongodb-org-tools amd64 6.0.2                                      [2,892 B]
Get:9 http://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0/main amd64 mongodb-org amd64 6.0.2 [2,92                                     8 B]
Fetched 134 MB in 7s (20.1 MB/s)
Selecting previously unselected package mongodb-database-tools.
(Reading database ... 35446 files and directories currently installed.)
Preparing to unpack .../0-mongodb-database-tools_100.6.0_amd64.deb ...
Unpacking mongodb-database-tools (100.6.0) ...
Selecting previously unselected package mongodb-mongosh.
Preparing to unpack .../1-mongodb-mongosh_1.6.0_amd64.deb ...
Unpacking mongodb-mongosh (1.6.0) ...
Selecting previously unselected package mongodb-org-shell.
Preparing to unpack .../2-mongodb-org-shell_6.0.2_amd64.deb ...
Unpacking mongodb-org-shell (6.0.2) ...
Selecting previously unselected package mongodb-org-server.
Preparing to unpack .../3-mongodb-org-server_6.0.2_amd64.deb ...
Unpacking mongodb-org-server (6.0.2) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../4-mongodb-org-mongos_6.0.2_amd64.deb ...
Unpacking mongodb-org-mongos (6.0.2) ...
Selecting previously unselected package mongodb-org-database-tools-extra.
Preparing to unpack .../5-mongodb-org-database-tools-extra_6.0.2_amd64.deb ...
Unpacking mongodb-org-database-tools-extra (6.0.2) ...
Selecting previously unselected package mongodb-org-database.
Preparing to unpack .../6-mongodb-org-database_6.0.2_amd64.deb ...
Unpacking mongodb-org-database (6.0.2) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../7-mongodb-org-tools_6.0.2_amd64.deb ...
Unpacking mongodb-org-tools (6.0.2) ...
Selecting previously unselected package mongodb-org.
Preparing to unpack .../8-mongodb-org_6.0.2_amd64.deb ...
Unpacking mongodb-org (6.0.2) ...
Setting up mongodb-mongosh (1.6.0) ...
Setting up mongodb-org-server (6.0.2) ...
Adding system user `mongodb' (UID 107) ...
Adding new user `mongodb' (UID 107) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 114) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org-shell (6.0.2) ...
Setting up mongodb-database-tools (100.6.0) ...
Setting up mongodb-org-mongos (6.0.2) ...
Setting up mongodb-org-database-tools-extra (6.0.2) ...
Setting up mongodb-org-database (6.0.2) ...
Setting up mongodb-org-tools (6.0.2) ...
Setting up mongodb-org (6.0.2) ...
Processing triggers for man-db (2.9.4-2) ...

Enable the MongoDB service to start on boot with the following command.

$ sudo systemctl enable mongod

Output

...Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.

Start MongoDB

$ sudo systemctl start mongod

Check status:

$ sudo systemctl status mongod

Output:

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-10-20 12:12:56 CDT; 1min 22s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 7749 (mongod)
     Memory: 76.6M
        CPU: 1.053s
     CGroup: /system.slice/mongod.service
             └─7749 /usr/bin/mongod --config /etc/mongod.conf

Verify your MongoDB’s successful installation with the following command. (This used to be “mongo” but has been replaced by “mongosh” in version 6.).

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

Output:

Current Mongosh Log ID: 6351852ac5d17178a2b8eee3
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
Using MongoDB:          6.0.2
Using Mongosh:          1.6.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting
   2022-10-20T12:12:56.716-05:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2022-10-20T12:12:57.427-05:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2022-10-20T12:12:57.428-05:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
   2022-10-20T12:12:57.428-05:00: vm.max_map_count is too low
------

------
   Enable MongoDB's free cloud-based monitoring service, which will then receive and display
   metrics about your deployment (disk utilization, CPU, operation statistics, etc).

   The monitoring data will be available on a MongoDB website with a unique URL accessible to you
   and anyone you share the URL with. MongoDB may use this information to make product
   improvements and to suggest MongoDB products and deployment options to you.

   To enable free monitoring, run the following command: db.enableFreeMonitoring()
   To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
------

{
  authInfo: { authenticatedUsers: [], authenticatedUserRoles: [] },
  ok: 1
}

Configure MongoDB

$ sudo nano /etc/mongod.conf

Go to the “#security” section and comment out security and add the following line of code. (note spaces!!)

security:
  authorization: enabled

Next, go to the #network interfaces section and add your system IP in the bindIp field.

net:
port: 27017
bindIp: 127.0.0.1,192.168.0.20

Save and exit the configuration file.

Restart the MongoDB service for the changes to take effect.

$ sudo systemctl restart mongod

Create MongoDB Admin User

For that, launch the MongoDB shell using the following command.

mongosh
Current Mongosh Log ID: 6351a5fe6f68885a7b8b95d0
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
Using MongoDB:          6.0.2
Using Mongosh:          1.6.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test>

Change to the admin database.

test> use admin
switched to db admin
admin>

And run the following command to add the admin user. Enter a password when prompted.

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

Output:

Enter password
****{ ok: 1 }

Setup the password on the prompt.

Now type exit to get out of the MongoDB shell.

Connect to the MongoDB
Now, you can use the admin user to connect to the MongoDB shell with the following command.

$ sudo mongosh --port 27017 --authenticationDatabase "admin" -u "admin" -p

Output:

Enter password: **********
Current Mongosh Log ID: 6351a75c4e0ef8b1b32b0b73
Connecting to:          mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+1.6.0
Using MongoDB:          6.0.2
Using Mongosh:          1.6.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

test>

Uninstall MongoDB Community Edition
To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs. The following section guides you through the necessary steps.

Stop MongoDB.
Stop the mongod process by issuing the following command:

$ sudo service mongod stop

Remove Packages.
Remove any MongoDB packages that you had previously installed.

$ sudo apt-get purge mongodb-org*

Remove Data Directories.
Remove MongoDB databases and log files.

$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongodb

Leave a Comment