MongoDB Server - mongod, Configurations

Here you will learn what is MongoDB server, how to run it manually, and how to set various configuration settings for your MongoDB server.

We installed the MongoDB server as a Windows service in the Install MongoDB chapter. MongoDB windows service is automatically up and running at startup, as shown below.

MongoDB Service on Windows

Internally, the MongoDB service starts the local MongoDB server at the default address http://127.0.0.1:27017 whenever your machine starts. MongoDB server is installed as mongod.exe on Windows in the {mongodb install folder}/bin folder, as shown below.

MongoDB Server File - mongod.exe

The MongoDB server mongod can be started or stop manually whether you installed it as a service or not.

Run mongod Manually

If you have installed MongoDB as a windows service then stop the service first as we are going to start it manually.

Now, add {mongodb install folder}/bin to your environment variable on your local Windows machine. Search for "Environment" on windows search and click "Edit the system environment variable" to open system properties, as shown below.

System Properties

Click on "Environment Variables.." to edit path, as shown below.

Environment Variable - Path

Now, select Path in the bottom pane and click on the Edit button. This will open "Edit environment variable" window. Click on the New button to add MongoDB folder path "C:\Program Files\MongoDB\Server\5.0\bin\", as shown below.

Add MongoDB Folder Path

Click OK to close all pop-up windows.

Adding MongoDB folder path to environment variable allow us to run mongod.exe from anywhere on command prompt/terminal.

Now, open a command prompt on Windows or terminal as an administrator and execute the mongod on Windows or sudo mongod on Mac to run your MongoDB server, as shown below.

Start MongoDB Server

You can specify various command options with mongod command for testing purposes. The following is list of some useful command options.

  -h [ --help ]                         Show this usage information
  --version                             Show version information
  -f [ --config ] arg                   Configuration file specifying
                                        additional options
  --configExpand arg                    Process expansion directives in config
                                        file (none, exec, rest)
  --port arg                            Specify port number - 27017 by default
  --ipv6                                Enable IPv6 support (disabled by
                                        default)
  --listenBacklog arg (=2147483647)     Set socket listen backlog size
  --maxConns arg (=1000000)             Max number of simultaneous connections
  --pidfilepath arg                     Full path to pidfile (if not set, no
                                        pidfile is created)
  --timeZoneInfo arg                    Full path to time zone info directory,
                                        e.g. /usr/share/zoneinfo
  -v [ --verbose ] [=arg(=v)]           Be more verbose (include multiple times
                                        for more verbosity e.g. -vvvvv)
  --quiet                               Quieter output
  --logpath arg                         Log file to send write to instead of
                                        stdout - has to be a file, not
                                        directory
  --logappend                           Append to logpath instead of
                                        over-writing
  --logRotate arg                       Set the log rotation behavior
                                        (rename|reopen)
  --timeStampFormat arg                 Desired format for timestamps in log
                                        messages. One of iso8601-utc or
                                        iso8601-local
  --setParameter arg                    Set a configurable parameter
  --bind_ip arg                         Comma separated list of ip addresses to
                                        listen on - localhost by default
  --bind_ip_all                         Bind to all ip addresses
  --noauth                              Run without security
  --transitionToAuth                    For rolling access control upgrade.
                                        Attempt to authenticate over outgoing
                                        connections and proceed regardless of
                                        success. Accept incoming connections
                                        with or without authentication.
  --slowms arg (=100)                   Value of slow for profile and console
                                        log
  --slowOpSampleRate arg (=1)           Fraction of slow ops to include in the
                                        profile and console log
  --profileFilter arg                   Query predicate to control which
                                        operations are logged and profiled
  --auth                                Run with security
  --clusterIpSourceAllowlist arg        Network CIDR specification of permitted
                                        origin for `__system` access
  --profile arg                         0=off 1=slow, 2=all
  --cpu                                 Periodically show cpu and iowait
                                        utilization
  --sysinfo                             Print some diagnostic system
                                        information
  --noscripting                         Disable scripting engine
  --notablescan                         Do not allow table scans
  --keyFile arg                         Private key for cluster authentication
  --clusterAuthMode arg                 Authentication mode used for cluster
                                        authentication. Alternatives are
                                        (keyFile|sendKeyFile|sendX509|x509)

Replication options:
  --oplogSize arg                       Size to use (in MB) for replication op
                                        log. default is 5% of disk space (i.e.
                                        large is good)

Replica set options:
  --replSet arg                         arg is [/]
  --enableMajorityReadConcern [=arg(=1)] (=1)
                                        Enables majority readConcern.
                                        enableMajorityReadConcern=false is no
                                        longer supported

Sharding options:
  --configsvr                           Declare this is a config db of a
                                        cluster; default port 27019; default
                                        dir /data/configdb
  --shardsvr                            Declare this is a shard db of a
                                        cluster; default port 27018

Storage options:
  --storageEngine arg                   What storage engine to use - defaults
                                        to wiredTiger if no data files present
  --dbpath arg                          Directory for datafiles - defaults to
                                        \data\db\ which is C:\data\db\ based on
                                        the current working drive
  --directoryperdb                      Each database will be stored in a
                                        separate directory
  --syncdelay arg (=60)                 Seconds between disk syncs
  --journalCommitInterval arg (=100)    how often to group/batch commit (ms)
  --upgrade                             Upgrade db if needed
  --repair                              Run repair on all dbs
  --journal                             Enable journaling
  --nojournal                           Disable journaling (journaling is on by
                                        default for 64 bit)
  --oplogMinRetentionHours arg (=0)     Minimum number of hours to preserve in
                                        the oplog. Default is 0 (turned off).
                                        Fractions are allowed (e.g. 1.5 hours)

Free Monitoring Options:
  --enableFreeMonitoring arg            Enable Cloud Free Monitoring
                                        (on|runtime|off)
  --freeMonitoringTag arg               Cloud Free Monitoring Tags

Windows Service Control Manager options:
  --install                             Install Windows service
  --remove                              Remove Windows service
  --reinstall                           Reinstall Windows service (equivalent
                                        to --remove followed by --install)
  --serviceName arg                     Windows service name
  --serviceDisplayName arg              Windows service display name
  --serviceDescription arg              Windows service description
  --serviceUser arg                     Account for service execution
  --servicePassword arg                 Password used to authenticate
                                        serviceUser

WiredTiger options:
  --wiredTigerCacheSizeGB arg           Maximum amount of memory to allocate
                                        for cache; Defaults to 1/2 of physical
                                        RAM
  --zstdDefaultCompressionLevel arg (=6)
                                        Default compression level for zstandard
                                        compressor
  --wiredTigerJournalCompressor arg (=snappy)
                                        Use a compressor for log records
                                        [none|snappy|zlib|zstd]
  --wiredTigerDirectoryForIndexes       Put indexes and data in different
                                        directories
  --wiredTigerCollectionBlockCompressor arg (=snappy)
                                        Block compression algorithm for
                                        collection data [none|snappy|zlib|zstd]
  --wiredTigerIndexPrefixCompression arg (=1)
                                        Use prefix compression on row-store
                                        leaf pages

ADVERTISEMENT

MongoDB Server Configuration

MongoDB uses the configuration file options to control the behavior of a database. MongoDB server mongod runs with the default configurations added in the config file. On Windows, it is {install directory}\bin\mongod.cfg file. On MacOS, it is /usr/local/etc/mongod.conf or /opt/homebrew/etc/mongod.conf. And, on Linux, it is /etc/mongod.conf file. MongoDB configuration files use the YAML format. The following is the mongod.cfg file on Windows:

mongod.cfg

The following is the sample config file which you may like to use for your local mongod server.

systemLog:
   destination: file
   path: "/var/log/mongodb/mongod.log"
   logAppend: true
storage:
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: 127.0.0.1
   port: 27017
setParameter:
   enableLocalhostAuthBypass: false

Visit mongod configuration options for more information.

Want to check how much you know MongoDB?