mongodb配置文件一览

mongod启动的时候一般会指定一个 configuration file,配置文件中包含的配置等同于命令行options,配置文件格式使用YAML,YAML使用空格缩进。
1:systemLog
verbosity 用于指定日志级别,默认是0,注意关键注释:

The default log message verbosity level
for components
. The verbosity level determines the amount of Informational and Debug messages MongoDB
outputs
不同的component可以指定不同级别的level,比如systemLog.component.accessControl.verbosity用于指定ACCESS components的日志级别。
traceAllExceptions比较重要,对于调试和跟踪问题很有用。
syslogFacility,如果将日志发送到syslog,则选项对应于syslog的facility level,注意:

To use this option, you must set systemLog.destination to syslog.

path用于将日志发送到指定的文件,而不是发送到syslog。
logAppend,默认是false,这样重新启动mongod的时候会重新创建新文件,如果是true则追加信息到原有文件中。
logRotate可以指定为rename或reopen,如果logAppend为true,则该选项必须为reopen。
destination可以指定为file和syslog,如果是file,则path选项必须指定。
2:processManagement
fork控制mongod在后台运行,pidFilePath指定PID文件,一般和fork选项结合使用。
3:net
mongod端口一般是27017,分片端口默认是27018,config server端口默认是27019。
bindIp用于绑定ip地址,如果想全部启用ipv4地址,指定为0.0.0.0。
maxIncomingConnections用于控制并发连接数。
wireObjectCheck如果设置为true,则mongod会拒绝不合法的BSON数据到mongod中。
compression控制不同mongod实例中数据传输的压缩方式。
4:setParameter

Set MongoDB parameter or parameters described in MongoDB Server Parameters

5:storage
dbPath用于指定mongod数据目录。
indexBuildRetry选项决定是否在重新启动的时候重建索引:

To stop mongod from rebuilding indexes, set this option to false.

注意该选项和replication.replSetName选项是冲突的。
journal.enabled选项很重要,默认是true,对于mongod崩溃恢复很重要:

Enable or disable the durability journal to ensure data files remain valid and recoverable.Starting in MongoDB 4.0, you cannot specify —nojournal option or storage.journal.enabled: false for replica set members that use the WiredTiger storage engine.

journal.enabled不适用于in-memory存储引擎。

journal.commitIntervalMs选项控制多久持久化日志落盘,如果在write操作的时候显示指定 j:true
,会忽略该参数,立刻落盘。
directoryPerDB选项如果为true,则每个数据库会有单独的目录。
syncPeriodSecs选项用于控制fsync数据文件的时间,注意:

Do not set this value on production systems. In almost every situation, you should use the default setting.If you set storage.syncPeriodSecs to 0, MongoDB will not sync the memory mapped files to disk.

思考fsync和journal的区别:

The mongod process writes data very quickly to the journal and lazily to the data files. storage.syncPeriodSecs has no effect on the journal files or journaling, but if storage.syncPeriodSecs is set to 0 the journal will eventually consume all available disk space. If you set storage.syncPeriodSecs to 0 for testing purposes, you should also set —nojournal to true.

engine选项很重要,默认是wiredTiger存储引擎。
6:选项wiredTiger

storage:
   wiredTiger:
      engineConfig:
         cacheSizeGB: 
         journalCompressor: 
         directoryForIndexes: 
         maxCacheOverflowFileSizeGB: 
      collectionConfig:
         blockCompressor: 
      indexConfig:
         prefixCompression: 

cacheSizeGB表示 internal cache大小,注意和索引内存(maxIndexBuildMemoryUsageMegabytes),文件系统缓存不是一回事。
cacheSizeGB默认最小256M,最大等于50% of (RAM – 1 GB)。
该选项limits the size of the WiredTiger internal cache. The operating system will use the available free memory for filesystem cache, which allows the compressed MongoDB data files to stay in memory. In addition, the operating system will use any free RAM to buffer file system blocks and file system cache.
7:replication
oplogSizeMB用于控制oplog大小,默认大小是可用磁盘空间的5%。需要注意的是mongod启动后,再设置该参数也不会改变oplog大小,为了生效,可以使用管理命令 replSetResizeOplog。
replSetName用于指定副本集名称,在副本集中的所有主机必须有相同的名称。
参考:

  • https://docs.mongodb.com/manual/reference/configuration-options/
  • https://docs.mongodb.com/manual/reference/log-messages/#log-messages-configure-verbosity
  • https://docs.mongodb.com/manual/reference/parameters/