mongod.conf 详解

MongoDB 的配置文件(/etc/mongod.conf)使用 YAML 格式(自 2.6 版本起),之前版本使用 INI 格式。

YAML 格式

  • YAML 格式要求严格:使用空格缩进,不支持缩进制表符</font>;字符串通常需要引号,特别是包含特殊字符时
storage:                                     # 存储配置
  dbPath: /var/lib/mongodb                   # 数据目录路径
  engine: wiredTiger                         # 数据库引擎:wiredTiger(默认)、inMemory
  journal:
    commitIntervalMs: 100                    # 日志提交间隔(毫秒)
  wiredTiger:
    engineConfig:
      cacheSizeGB: 2                         # 缓存大小(默认:物理内存的50%)
      journalCompressor: snappy              # 日志压缩算法:none、snappy(默认)、zlib
    collectionConfig:
      blockCompressor: snappy                # 块压缩算法:none、snappy、zlib、zstd
    indexConfig:
      prefixCompression: true                # 前缀压缩:true(默认)

systemLog:                                   # 系统日志配置
  destination: file                          # 日志输出目标:file 或 syslog
  path: /var/log/mongodb/mongod.log          # 日志文件路径
  logAppend: true                            # 是否追加日志(true:追加,false:覆盖)
  verbosity: 0                               # 日志级别:0-5(0:默认,5:最详细)
  logRotate: rename                          # 日志轮转设置
  quiet: false                               # 静默模式,减少日志输出
  timeStampFormat: iso8601-local             # 时间戳格式:iso8601-utc 或 iso8601-local

net:                                         # 网络配置
  port: 3717                                 # 监听端口(默认:27017)
  bindIp: 0.0.0.0                            # 绑定IP地址,可以指定多个IP,用逗号分隔
  # bindIpAll: true                          # 绑定到所有IPv4地址
  unixDomainSocket:                          # Unix域套接字
    enabled: true
    pathPrefix: "/tmp"
    filePermissions: 0700
  maxIncomingConnections: 8000                # 连接数限制
#  ipv6: false                                # 启用IPv6
#  ssl:                                       # 传输层安全/SSL
#    mode: disabled                           # disabled, allowSSL, preferSSL, requireSSL
#    PEMKeyFile: "/etc/ssl/mongodb.pem"
#    CAFile: "/etc/ssl/ca.pem"
#    disabledProtocols: "TLS1_0,TLS1_1"
#    allowConnectionsWithoutCertificates: true
#    allowInvalidCertificates: false
#    FIPSMode: false

processManagement:                             # 进程管理
  fork: true                                   # 是否以后台守护进程方式运行
  pidFilePath: "/var/lib/mongodb/mongo.pid"    # PID文件路径
  timeZoneInfo: "/usr/share/zoneinfo"          # 时区数据库路径

setParameter:
  enableLocalhostAuthBypass: false

#security:                                    # 安全配置
#  authorization: enabled                     # 是否启用身份验证, enabled 或 disabled
#  keyFile: "/data/mongo_3717/data/keyfile"   # 密钥文件路径(用于副本集/分片集群身份验证)
#  javascriptEnabled: true                    # 禁用JavaScript操作(减少安全风险)
#  redactClientLogData: false                 # 启用操作日志
#  clusterAuthMode: keyFile                   # 集群认证模式, keyFile, sendKeyFile, sendX509, x509
#  enableEncryption: false                    # 加密存储
#  encryptionKeyFile: "/path/to/keyfile"
#  encryptionCipherMode: "AES256-CBC"
#  ldap:                                      # LDAP配置
#    servers: "ldap.example.com"
#    bind:
#      method: simple
#      queryUser: "cn=admin,dc=example,dc=com"
#      queryPassword: "password"

#replication:                                 # 副本集配置
#  replSetName: "set3717"                     # 副本集名称
#  oplogSizeMB: 10240                         # 操作日志(oplog)大小(MB)
#  enableMajorityReadConcern: true            # 启用主从复制

#sharding:                                    # 分片集群配置
#  clusterRole: shardsvr                      # 分片角色:configsvr(配置服务器)、shardsvr(数据分片)
#  archiveMovedChunks: true                   # 存档迁移的块

## Enterprise-Only Options:

#auditLog:                                      # 审计日志
#  destination: file                            # 或 syslog, console
#  path: "/var/log/mongodb/audit.log"
#  format: JSON                                 # 或 BSON
#  filter: '{ atype: { $in: [ "createCollection", "dropCollection" ] } }'

#operationProfiling:                             # 性能分析器
#  slowOpThresholdMs: 2000                       # 慢操作阈值(毫秒)
#  mode: slowOp                                  # 分析模式:off, slowOp, all
#  slowOpSampleRate: 1.0                         # 慢操作采样率(1.0=全部记录)

INI 格式

quiet=false
timeStampFormat=iso8601-local
logappend=true
journal=true
#auth=true
directoryperdb=true
fork=true
oplogSize=102400
#replSet=set3717
port=3717
bind_ip=0.0.0.0
profile=1
slowms=2000
maxConns=8000
cpu=true
storageEngine=wiredTiger
wiredTigerCacheSizeGB=2
wiredTigerDirectoryForIndexes=true
wiredTigerIndexPrefixCompression=true
wiredTigerJournalCompressor=zlib
wiredTigerCollectionBlockCompressor=snappy
dbpath=/data/mongo_3717/data
#keyFile=/data/mongo_3717/data/keyfile
pidfilepath=/data/mongo_3717/pid/mongo.pid
logpath=/data/mongo_3717/logs/mongo.log
Copyright © www.sqlfans.cn 2021 - 2026 All Right Reserved更新时间: 2026-08-06 17:18:04

results matching ""

    No results matching ""