The mongod.cfg file for configuring the mongod process

So with mongodb there is the mongod binary that is the actually process daemon or service that handles requests for data in a database. It other words it is what always runs in the background on the computer that is hosting the database that is being used in a project that makes use of mongodb. Well one thing that might come of interest with this is how to configure mongodb with settings like the port to listen on, and if authentication should be enabled or not. Well one way is to give some of these options to the binary via the command line when staring it, but maybe a better way is to park these settings in a configuration file of sorts, thats where the mongod.cfg file comes into play. In this post I will be giving a quick overview of

1 - what to know

This is a post on the config file that is used to configure the mongod process. In this post I am using mongodb 4.0, and the config file that I am writing about is in the yaml format. In older versions of mongodb this file may be of a different format.

2 An example of mongod.cfg

An example of a mongod.cfg file that I have in a windows environment looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: C:\Program Files\MongoDB\Server\4.0\data
journal:
enabled: true
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: C:\Program Files\MongoDB\Server\4.0\log\mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
## security settings
security:
authorization: enabled

Regardless of the operation system environment, you want the paths to be absolute paths. In fact I would say that using absolute paths in general is good practice, but that is getting off topic. In a posix environment that might be the only note worthy difference though. In a Linux environment where I have another setup the config file is in a different format, because it is an older version of mongodb, so I will not be writing about there here.

2.1 - Storage Options

For storage options this is where I set the path where the database folder will be by giving an absolute path to the desired location.

1
2
3
4
storage:
dbPath: C:\Program Files\MongoDB\Server\4.0\data
journal:
enabled: true

Here journaling should be enabled, it often is be default. This helps when it comes to unclean shutdowns.

2.2 - System log options

here I set the location of the system logs where I should look when something goes wrong to see what the problem might be.

1
2
3
4
systemLog:
destination: file
logAppend: true
path: C:\Program Files\MongoDB\Server\4.0\log\mongod.log

2.3 - Network interfaces

Here I set the port, and the ip to bind to. In most cases so far I am just working with mongodb locally, when I go to deploy I dot have to wory about much of this, ans the default settings work okay.

1
2
3
net:
port: 27017
bindIp: 127.0.0.1

2.4 - Security settings

So far I have only been playing with authentication, when I go to deploy an app authentication is enabled so I only enabled this and set up user accounts to help better emulate what I am dealing with when I go to deploy. For the most part this is the only reason why I bother with this at all when working with mongodb locally.

1
2
3
## security settings
security:
authorization: enabled

3 - Conclusion

Thats it for now, I might write more about this if I play around with mognodb more however do not hold your breath. Most of my future content on mongodb will likely be on mongo clients like mongoose, when it comes to working with mngodb with respect to making apps, rather than fiddling with config files.