net2020

Installing MongoDB community edition on Debian Linux

10/08/2022

Installing MongoDB community edition on Debian Linux

Introduction

MongoDB is a document database. Instead of storing data in tables of rows or columns like SQL databases, each record in a MongoDB database is a document described in BSON, a binary representation of the data. Applications can then retrieve this information in a JSON format. SQL (relational) databases might need to lock multiple tables when introducing changes and they keep track of those changes using transactions. This level of complexity guarantees data consistency but negatively affects performance. With document databases the scope of atomic changes is smaller, down to a single document.

Installation

Install DEB archives

Download deb archive files from mongodb.com website: MongoDB deb download

Right click on links under Debian 11.0 x64 (Server Package and Mongos Package) and save each file to Downloads folder.

Now download MongoDB shell: MongoDB shell download

Version = 1.6.0

Platform = Debian / Ubuntu 64-bit

Package = deb

Log in as root (su command) and install all deb files:

su
...
dpkg -i mongodb-org-server_6.0.2_amd64.deb
dpkg -i mongodb-org-mongos_6.0.2_amd64.deb
dpkg -i mongodb-mongosh_1.6.0_amd64.deb

If you get prompted about keeping existing configuration file (e.g. /etc/mongod.conf) select Y to replace existing file with new file (old configuration file will be renamed to mongod.conf.dpkg-old in the same folder).

New user and group should be automatically added for MongoDB service (mongodb).

Update service configuration

If you want to keep database and log files in a location that is different from the default one follow these steps:

Create new folders for database and log files. For example:

su
...
mkdir -p /Database/mongoData
mkdir -p /Database/mongoLogs
cd /Database
chown -R mongodb.mongodb mongo*

Update MongoDB configuration (in /etc/mongod.conf) using your favorite text editor:

storage/dbPath: /Database/mongoData
systemLog/path: /Database/mongoLogs/mongod.log

Restart MongoDB service with this command: systemctl restart mongod

Verify database functions

Now we can check if server is working. Open MongoDB shell: mongosh And run this shell command: show dbs You should see something like this:

test> show dbs
admin   40.00 KiB
config  12.00 KiB
local   40.00 KiB

If you want to disable MongoDB's cloud monitoring service run this shell command:

test> db.disableFreeMonitoring();
{ ok: 1 }

If you scroll your shell console output up you might see messages like these:

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

We're not going to do anything with XFS file system but we can address all other issues. You can exit MongoDB shell by typing quit.

Create test database and user credentials

Let's see what database users are already defined using MongoDB shell:

mongosh
test> use admin
switched to db admin
admin> db.system.users.find({}, {_id:1, roles:1});

If you're not seeing any users that's OK, fresh MongoDB installation does not have any.

Small side note, in find query above the first parameter ({}) is the query (an empty one) and the second one is the list of fields to be returned ({_id:1, roles:1}).

Let's create a new database called stats. From MongoDB docs:

There is no "create" command in the MongoDB shell. In order to create a database, you will first need to switch the context to a non-existing database using the use command.

mongosh
test> use admin
switched to db admin
db.createUser({user:"admin",pwd:"secret",roles:[{role:"dbOwner", db:"stats"}]});
{ ok: 1 }

See this page for a list of MongoDB built-in roles: MongoDB built-in roles

Now we can enable authorization in MongoDB configuration (e.g. in /etc/mongod.conf):

su
...
# open /etc/mongod.conf for editing
security:
  authorization: enabled
# save changes, close editor and restart MongoDB service
systemctl restart mongod
# let's try to open shell using an incorrect password
mongosh --authenticationDatabase admin -u admin -p incorrect stats
MongoServerError: Authentication failed.
# let's provide the right password
mongosh --authenticationDatabase admin -u admin -p secret stats
Using MongoDB:		6.0.2
Using Mongosh:		1.6.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
# we're in

If you need to make changes remove authorization: enabled section in configuration file and restart MongoDB service.

Disable Transparent Huge Pages (THP)

Let's take care of the second to last warning from MongoDB startup warnings listed above. Use these commands:

su
...
apt install sysfsutils
# add this line to /etc/sysfs.conf
kernel/mm/transparent_hugepage/enabled = never
# restart MongoDB service
systemctl restart mongod

That should take care of that warning.

Increase maximum number of network connections

That is the last warning on the MongoDB startup warning list above. Enter these commands:

su
...
# check the current setting
/sbin/sysctl vm.max_map_count
# prints vm.max_map_count = 65530 on my machine
# add this line to /etc/sysctl.conf
vm.max_map_count=9999999
# restart your computer

These commands take care of the vm.max_map_count setting only but there is more. See this page for a detailed explanation: Tuning MongoDB on Linux

Conclusion

There is more to configuring MongoDB, check out their website for detailed instructions: MongoDB administration

I hope this info helps you get started. Enjoy MongoDB!