一 配置文件说明

MongoDB有两种配置文件格式,分别是:
3.2版官方yaml配置文件选项参考

用=号的常规格式
类似my.conf等常规配置的文件
yaml语法的新格式
mongodb3.x版本后就是要yaml语法格式的配置文件,下面是yaml配置文件格式如下:
切记yaml只能使用空格,不支持tab键,切记

配置举例

配置文件

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

指定配置启动

mongod --config  /etc/mongod.conf
# 或者
mongod -f        /etc/mongod.conf

配置文件模块

# mongod.conf配置文件分为如下几块内容:

systemLog:           #日志模块相关参数
storage:             #存储引擎相关参数
processManagement:   #进程管理
net:                 #网络
security:            #安全
operationProfiling:  #性能分析器
replication:         #主从复制配置
sharding:            #分片集群配置
setParameter:        #自定义变量
auditLog:            #检测日志
snmp:        #

二 详细配置部分

A systemLog日志相关参数

systemLog:
  verbosity: <int>          #日志级别,默认0,1-5均会包含debug信息
  quiet: <bool>             #安静,true时将会减少日志的输出量
  traceAllExceptions: <bool>   #打印异常详细信息
  syslogFacility: <string>     #启用syslog指定用于登录时信息到syslog Facility水平,前提是
  path: <string>            #日志路径
  logAppend: <bool>         #追加日志还是新建日志
  logRotate: rename|reopen  #日志轮询。默认值rename;reopen前提为 logAppend: true
  destination: <string>     #日志输出目的地,可为file或syslog,不指定会输出到标准输出
  timeStampFormat: <string> #日志时间戳格式,有 ctime,Iso869-utc,iso8691-local
  component:            #为不同的组件指定各自的日志信息级别
      accessControl:
          verbosity: <int>
      command:
          verbosity: <int>

B storage存储引擎相关参数

storage:
  dbPath: <string>        #数据存储目录
  indexBuildRetry: <bool> #构件索引时mongod意外关闭,启动是否重建索引,默认true
  repairPath: <string>    #在repair期间临时数据目录
  journal:        
      enabled: <boolean>        #journal日志是否持久存储,通常用于数据故障恢复,建议开启
      commitIntervalMs: <num>   #mongod日志刷新值,范围1-500毫秒,默认100,不建议修改
  directoryPerDB:  <bool>  #是否将不同的数据存储在不同的目录中,需为dbPath子目录
  syncPeriodSecs:  <int>   #fsync操作将数据flush到磁盘的时间间隔,默认为60秒,不建议修改
  engine:  <string>        #存储引擎类型,3.2前默认 mmapv1,3.2后默认WiredTiger
  wiredTiger:    #存储引擎配置
      engineConfig:
          cacheSizeGB: <number>        #最大缓存大小
          journalCompressor: <string>  #日志压缩算法,可选值有 none,snappy(默认),zlib
          directoryForIndexes: <bool>  #是否将索引和集合数据分别存储在dbPath单独的目录中
      collectionConfig:
          blockCompressor: <string>    #collection数据压缩算法,可选none, snappy,zlib
      indexConfig:
          prefixCompression: <bool>    #是否对索引数据使用前缀压缩。可有效减少索引数据的内存使用量。

C processManagement进程相关参数

processManagement:
  fork: <boolean>        #是否以守护进程运行,默认false
  pidFilePath: <string>  #将mongod进程ID写入指定文件,默认不会创建

D net网络相关参数

net:
  prot: <int>         #监听端口,默认27017
  bindIp: <string>    #绑定IP,如果此值是“0.0.0.0”则绑定所有接口
  maxIncomingConnections: <int>  #进程允许的最大连接数,上限是系统阈值(ulimit)
  wireObjectCheck: <boolean>     #当客户端写入数据时,是否检查数据的有效性(BSON),有效的数据才执行
  ipv6: <bool>    #是否支持多实例之间使用ipv6
  http:    #http配置
      enabled: <boolean>
      JSONEnabled: <boolean>
      RESTInterfaceEnabled: <boolean>
  ssl:     #https配置
      sslOnNormalPorts: <boolean>
      mode: <string>
      PEMKeyFile: <string>
      PEMKeyPassword: <string>
      clusterFile: <string>
      clusterPassword: <string>
      CAFile: <string>
      CRLFile: <string>
      allowConnectionsWithoutCertificates: <boolean>
      allowInvalidCertificates: <boolean>
      allowInvalidHostnames: <boolean>
      disabledProtocols: <string>
      FIPSMode: <boolean>
  compression:
      compressors: <string>

E security安全相关参数

security:
  authorization: enabled    #MondoDB认证功能
  keyFile: /path/mongo.key    #MongoDB副本集节点身份验证密钥文件
  clusterAuthMode: <string>    #集群members间的认证模式
  transitionToAuth: <boolean>
   javascriptEnabled:  <boolean>    #是否允许执行JavaScript脚本
   redactClientLogData: <boolean>
   sasl:
      hostName: <string>
      serviceName: <string>
      saslauthdSocketPath: <string>
   enableEncryption: <boolean>
   encryptionCipherMode: <string>
   encryptionKeyFile: <string>
   kmip:
      keyIdentifier: <string>
      rotateMasterKey: <boolean>
      serverName: <string>
      port: <string>
      clientCertificateFile: <string>
      clientCertificatePassword: <string>
      serverCAFile: <string>
   ldap:
      servers: <string>
      bind:
         method: <string>
         saslMechanism: <string>
         queryUser: <string>
         queryPassword: <string>
         useOSDefaults: <boolean>
      transportSecurity: <string>
      timeoutMS: <int>
      userToDNMapping: <string>
      authz:
         queryTemplate: <string>

F operationProfiling慢查询相关参数:

operationProfiling:
  slowOpThresholdMs: <int>   #“慢查询”的时间阈值,单位毫秒(默认100ms)
  mode: <string>             #数据库profiler级别,可选值“off|slowOp|all”
  # 数据库profiling会影响性能,建议只在性能调试阶段开启,mongod会把慢查询记录到日志中

G replication副本集相关参数

replication:
  oplogSizeMB: <int>      #replication操作日志的最大尺寸,如果太小则全量同步
  replSetName: <string>   #副本集名称,副本集中所有的mongod实例都必须有相同的名字
  secondaryIndexPrefetch: <string>    #副本集中的secondary,从oplog中应用变更操作前,会先把索引加载到内存
  enalbeMajorityReadConcern: <boolean>    #允许readConcern的级别为“majority”

H sharding分片相关参数

sharding:
  clusterRole: <string>    #在sharding集群中的角色。configsvr(27019)|shardsvr(27018)
  archiveMovedChunks: <bool>   #当chunks迁移后,是否归档并保存这些chunks在dbPath/movechunk目录下

I setParameter自定义变量

setParameter:
  <parameter1>: <value1>
  <parameter2>: <value2>
  enableLocalhostAuthBypass: false    #栗子

J auditLog审计相关参数:

auditLog:
  destination: <string>    #指定审计记录的输出方式,有syslog|console|file
  format: <string>    #输出格式,有JSON 和 BSON
  path: <string>      #如果输入为文件,那么指定文件完整路径及文件名
  filter: <string>    #过滤器,可限制审计系统记录的操作类型