Run-time Database Configuration
The command line and configurationfile interfaces provide MongoDBadministrators with a large number of options and settings forcontrolling the operation of the database system. This documentprovides an overview of common configurations and examples ofbest-practice configurations for common use cases.
While both interfaces provide access to the same collection of optionsand settings, this document primarily uses the configuration fileinterface. If you run MongoDB using a init script or if you installedfrom a package for your operating system, you likely already have aconfiguration file located at /etc/mongod.conf
. Confirm this bychecking the contents of the /etc/init.d/mongod
or/etc/rc.d/mongod
script to ensure that the init scripts start themongod
with the appropriate configuration file.
To start a MongoDB instance using this configuration file, issue acommand in the following form:
- mongod --config /etc/mongod.conf
- mongod -f /etc/mongod.conf
Modify the values in the /etc/mongod.conf
file on your system tocontrol the configuration of your database instance.
Configure the Database
Consider the following basic configuration which uses the YAMLformat:
- processManagement:
- fork: true
- net:
- bindIp: localhost
- port: 27017
- storage:
- dbPath: /srv/mongodb
- systemLog:
- destination: file
- path: "/var/log/mongodb/mongod.log"
- logAppend: true
- storage:
- journal:
- enabled: true
Or, if using the older .ini
configuration file format:
- fork = true
- bind_ip = localhost
- port = 27017
- quiet = true
- dbpath = /srv/mongodb
- logpath = /var/log/mongodb/mongod.log
- logappend = true
- journal = true
For most standalone servers, this is a sufficient baseconfiguration. It makes several assumptions, but consider thefollowing explanation:
fork
istrue
, which enables adaemon mode formongod
, which detaches (i.e. “forks”)the MongoDB from the current session and allows you to run thedatabase as a conventional server.bindIp
islocalhost
, which forces theserver to only listen for requests on the localhost IP. Only bind tosecure interfaces that the application-level systems can access withaccess control provided by system network filtering(i.e. “firewall”).
New in version 2.6: mongod
installed from official .deb and .rpm packageshave the bind_ip
configuration set to 127.0.0.1
bydefault.
port
is27017
, which is the defaultMongoDB port for database instances. MongoDB can bind to anyport. You can also filter access based on port using networkfiltering tools.
Note
UNIX-like systems require superuser privileges to attach processesto ports lower than 1024.
quiet
istrue
. This disables all butthe most critical entries in output/log file, and is _not_recommended for production systems. If you do set this option, youcan usesetParameter
to modify this setting duringrun time.dbPath
is/srv/mongodb
, whichspecifies where MongoDB will store its data files./srv/mongodb
and/var/lib/mongodb
are popular locations. The user accountthatmongod
runs under will need read and write access to thisdirectory.systemLog.path
is/var/log/mongodb/mongod.log
which is wheremongod
will write its output. If you do not setthis value,mongod
writes all output to standard output(e.g.stdout
.)logAppend
istrue
, which ensures thatmongod
does not overwrite an existing log filefollowing the server start operation.storage.journal.enabled
istrue
, which enablesjournaling. Journaling ensures single instancewrite-durability. 64-bit builds ofmongod
enablejournaling by default. Thus, this setting may be redundant.
Given the default configuration, some of these values may beredundant. However, in many situations explicitly stating theconfiguration increases overall system intelligibility.
Security Considerations
The following configuration options are useful for limiting access to amongod
instance:
- net:
- bindIp: localhost,10.8.0.10,192.168.4.24,/tmp/mongod.sock
- security:
- authorization: enabled
net.bindIp
This example provides four values to the
bindIp
option:localhost
, the localhost interface;10.8.0.10
, a private IP address typically used for localnetworks and VPN interfaces;192.168.4.24
, a private network interface typically used forlocal networks; and/tmp/mongod.sock
, a Unix domain socket path.Because production MongoDB instances need to be accessible frommultiple database servers, it is important to bind MongoDB tomultiple interfaces that are accessible from your applicationservers. At the same time it’s important to limit these interfacesto interfaces controlled and protected at the network layer.
- Setting this option to
true
enables the authorizationsystem within MongoDB. If enabled you will need to log in byconnecting over thelocalhost
interface for the first time tocreate user credentials.
See also
Replication and Sharding Configuration
Replication Configuration
Replica set configuration is straightforward, and onlyrequires that the replSetName
have a value that is consistentamong all members of the set. Consider the following:
In YAML format
- replication:
- replSetName: set0
Or, if using the older configuration file format:
- replSet = set0
Use descriptive names for sets. Once configured, use themongo
shell to add hosts to the replica set.
See also
To enable authentication for the replica set usingkeyfiles , add the followingkeyFile
option [1]:
In YAML format
- security:
- keyFile: /srv/mongodb/keyfile
Or, if using the older configuration file format:
- keyFile = /srv/mongodb/keyfile
Setting keyFile
enables authentication andspecifies a keyfile for the replica set member to use whenauthenticating to each other.
See also
The Replica Set Security section forinformation on configuring authentication with replica sets.
The Replication document for more informationon replication in MongoDB and replica set configuration in general.
[1] | Sharded clusters and replica sets can use x.509 for membership verification instead ofkeyfiles. For details, see x.509. |
Sharding Configuration
Sharding requires mongod
instances with differentmongod
configurations for the config servers and the shards. The config servers store the cluster’smetadata, while the shards store the data.
To configure the config server mongod
instances, in theconfiguration file, specify configsvr
for thesharding.clusterRole
setting.
Changed in version 3.4: Starting in version 3.4, MongoDB removes support for mirroredconfig servers and config servers must bedeployed as a replica set.
- sharding:
- clusterRole: configsvr
- net:
- bindIp: 10.8.0.12
- port: 27001
- replication:
- replSetName: csRS
To deploy config servers as a replica set, the config servers must runthe WiredTiger Storage Engine. Initiate
thereplica set and add members.
To configure the shard mongod
instances, specifyshardsvr
for the sharding.clusterRole
setting, and ifrunning as a replica set, the replica set name:
- sharding:
- clusterRole: shardsvr
- replication:
- replSetName: shardA
If running as a replica set, initiate
theshard replica set and add members.
For the router (i.e. mongos
), configure at least onemongos
process with the following setting:
- sharding:
- configDB: csRS/10.8.0.12:27001
You can specify additional members of the config server replica set byspecifying hostnames and ports in the form of a comma separated listafter the replica set name.
See also
The Sharding section of the manual for moreinformation on sharding and cluster configuration.
Run Multiple Database Instances on the Same System
In many cases running multiple instances of mongod
on asingle system is not recommended. On some types of deployments[2] and for testing purposes you may need to run more thanone mongod
on a single system.
In these cases, use a base configuration for eachinstance, but consider the following configuration values:
In YAML format:
- storage:
- dbPath: /srv/mongodb/db0/
- processManagement:
- pidFilePath: /srv/mongodb/db0.pid
Or, if using the older configuration file format:
- dbpath = /srv/mongodb/db0/
- pidfilepath = /srv/mongodb/db0.pid
The dbPath
value controls the location of themongod
instance’s data directory. Ensure that each databasehas a distinct and well labeled data directory. ThepidFilePath
controls where mongod
processplaces it’s process id file. As this tracks the specificmongod
file, it is crucial that file be unique and welllabeled to make it easy to start and stop these processes.
Create additional init scripts and/oradjust your existing MongoDB configuration and init script asneeded to control these processes.
[2] | Single-tenant systems with SSD or other highperformance disks may provide acceptable performance levels formultiple mongod instances. Additionally, you may find thatmultiple databases with small working sets may function acceptablyon a single system. |
Diagnostic Configurations
The following configuration options control various mongod
behaviors for diagnostic purposes:
operationProfiling.mode
sets the database profiler level. The profiler is not active by defaultbecause of the possible impact on the profiler itself on performance.Unless this setting is on, queries are not profiled.
operationProfiling.slowOpThresholdMs
configures thethreshold which determines whether a query is “slow” for thepurpose of the logging system and the profiler. The default value is 100 milliseconds. Setto a lower value if the logging system and the database profiler donot return useful results or set to a higher value to only log thelongest running queries.
Starting in version 4.2 (also available starting in 4.0.6), secondary members of a replica set nowlog oplog entries that take longer than the slowoperation threshold to apply. These slow oplog messages are loggedfor the secondaries in the diagnostic log
under the REPL
component with the text appliedop: <oplog entry> took <num>ms
. These slow oplog entries dependonly on the slow operation threshold. They do not depend on the loglevels (either at the system or component level), or the profilinglevel, or the slow operation sample rate. The profiler does notcapture slow oplog entries.
systemLog.verbosity
controls the amount of logging output thatmongod
write to the log. Only use this option if you areexperiencing an issue that is not reflected in the normal logginglevel.
Starting in MongoDB 3.0, you can also specify verbosity level forspecific components using thesystemLog.component.<name>.verbosity
setting. For the availablecomponents, see component verbosity settings
.
For more information, see also Database Profiling andMongoDB Performance.