How To Install MongoDB 4.4 / 4.2 / 4.0 on Debian 10

THIS DOCUMENT IS ALSO AVAILABLE FOR

MongoDB is an open-source document-oriented database designed for both scalability and developer agility in mind. It uses JSON-like documents with dynamic schemas to store information (data).

MongoDB is available for Linux, Windows, OS X, FreeBSD, and Solaris.

Add MongoDB Repository

MongoDB Inc provides MongoDB packages for Debian 10.

Install the certificate management server for downloading and managing certificates.

sudo apt update

sudo apt install -y dirmngr gnupg wget

MongoDB 4.4

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

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

MongoDB 4.2

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

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

MongoDB 4.0

MongoDB 4.0 is not available for Debian 10. As a workaround for applications (Graylog supports only MongoDB 4.0) that require MongoDB 4.0 on Debian 10, we are going to install MongoDB 4.0 for Debian 9 on Debian 10.

This workaround may not be suitable for production use and may cause unexpected results. Use it with caution.

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

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

sudo apt update

sudo apt install -y libcurl3

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

Install MongoDB

Update the repository index.

sudo apt update

Install the MongoDB packages using the apt command. The name of the MongoDB package is mongodb-org.

sudo apt install -y mongodb-org

Post Installation

Filesystem (Optional)

You can either use XFS or EXT4 filesystem to hold the MongoDB database (/var/lib/mongodb). If possible, use the XFS filesystem for better performance.

Security Checklist

Create Admin User

By default, MongoDB doesn’t restrict access to its data, which means anyone can read and modify data. To restrict access, you would need create the database administrative user first.

Access Control

Now, Enable access control to enforce authentication, and that allows only identified users to perform actions based on their roles.

sudo nano /etc/mongod.conf

Add the below lines.

security:
   authorization: enabled

After enabling the access control, proceed to create an admin user for daily operation.

Disable Huge Pages

Transparent huge pages often create performance issues for database loads. So, MongoDB recommends the transparent huge pages be disabled for best performance.

Create a systemd file.

sudo nano /etc/systemd/system/disable-transparent-huge-pages.service

Place the below lines into the above file.

[Unit]
Description=Disable Transparent Huge Pages (THP)
DefaultDependencies=no
After=sysinit.target local-fs.target
Before=mongod.service

[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled > /dev/null'

[Install]
WantedBy=basic.target

Reload the systemd daemon to read new services.

sudo systemctl daemon-reload

Start the service.

sudo systemctl start disable-transparent-huge-pages

Enable the service to ensure transparent huge pages be disabled on system boot.

sudo systemctl enable disable-transparent-huge-pages

Validate if huge pages are disabled.

cat /sys/kernel/mm/transparent_hugepage/enabled
Output:

always madvise [never]

Start Service

MongoDB services can be started/stopped by the easy known commands.

Start MongoDB service with,

sudo systemctl start mongod

Enable MongoDB service to start automatically on system startup.

sudo systemctl enable mongod

Check the status of MongoDB service, run.

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 Mon 2020-08-10 09:40:57 EDT; 22s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 2319 (mongod)
   Memory: 67.4M
   CGroup: /system.slice/mongod.service
           └─2319 /usr/bin/mongod --config /etc/mongod.conf

Aug 10 09:40:57 debian10 systemd[1]: Started MongoDB Database Server.
Aug 10 09:41:02 debian10 systemd[1]: /lib/systemd/system/mongod.service:10: PIDFile= references path b

Confirm the version of MongoDB.

mongod --version
Output:

db version v4.4.0
Build Info: {
    "version": "4.4.0",
    "gitVersion": "563487e100c4215e2dce98d0af2a6a5a2d67c5cf",
    "openSSLVersion": "OpenSSL 1.1.1d  10 Sep 2019",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "debian10",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Access MongoDB

Connect to MongoDB shell by using the mongo command.

mongo
Output:

MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("70a272d5-cfbf-43b3-a0d2-19ff49b51e51") }
MongoDB server version: 4.4.0
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
>

Conclusion

That’s All. I hope you have learned how to install MongoDB on Debian 10. Please share your feedback in the comments section.

Prev Post
Next Post
comments powered by Disqus