How To Install MongoDB 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.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.

Access Control

By default, MongoDB doesn’t restrict access to its data, which means anyone can read and modify data. To restrict access, you would need to 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 Tue 2020-03-5 09:29:43 CST; 6s ago
      Docs: https://docs.mongodb.org/manual
  Main PID: 2574 (mongod)
    Memory: 70.4M
    CGroup: /system.slice/mongod.service
                └─2574 /usr/bin/mongod --config /etc/mongod.conf

Mar 05 09:29:43 debian10 systemd[1]: Started MongoDB Database Server.
Mar 05 09:29:49 debian10 systemd[1]: /lib/systemd/system/mongod.service:10: PIDFile= references path below legacy directory /var/run/, updating /var/run/mongodb/mongod.

Confirm the version of MongoDB.

mongod --version
Output:

db version v4.2.3
git version: 6874650b362138df74be53d366bbefc321ea32d4
OpenSSL version: OpenSSL 1.1.1d  10 Sep 2019
allocator: tcmalloc
modules: none
build 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.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0550e255-f435-410b-9051-83b58676be80") }
MongoDB server version: 4.2.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
>

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