1 MongoDB 配置文件变化历史说明
在之前的博客中我们讲MongoDB数据库启动时需要指定配置文件。
MongoDB 的 启动 和 关闭
https://www.cndba.cn/dave/article/107962
安装DB之后,我们建议配置放在etc下:/etc/mongod.conf。
MongoDB的配置文件有两种格式:
- 直接配置,即
= 的方法进行配置。 - YAML 的配置文件。
关于YAML 语言,我之前的博客说明,如下:
YAML 语言 概述
https://www.cndba.cn/dave/article/4590
注意
YAML does not support tab characters for indentation: use spaces instead.
yaml格式的配置文件中只能使用空格,不支持tab键,一旦使用tab键,启动mongod服务是就会报错。
在MongoDB 2.6 之前的版本,都是直接在参数文件中配置的。 从MongoDB 2.6 开始,引入了YAML配置文件。 但以后的高版本对2.6 之前的配置文件依旧保持兼容。
比如我这里4.4 的环境,使用的依旧是旧格式的配置文件:
> db.version()
4.4.13
[dave@www.cndba.cn logs]# cat /etc/mongo.conf
#数据库数据存放目录
dbpath=/mongodb/data
#数据库日志存放目录
logpath=/mongodb/logs/mongodb.log
#以追加的方式记录日志
logappend = true
#端口号 默认为27017
port=27017
#以后台方式运行进程
fork=true
#开启用户认证
auth=true
#关闭http接口,默认关闭http端口访问
#nohttpinterface=true
#mongodb所绑定的ip地址
#bind_ip = 127.0.0.1
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true
[dave@www.cndba.cn logs]#
关于2.4 版本的配置文件,参考如下链接,这里不再说明:
https://www.mongodb.com/docs/v2.4/reference/configuration-options/
本片我们重点了解下MongoDB的YAML 配置。
2 MongoDB的YAML 配置文件内容组成
在官方文档里有详细的说明:
https://www.mongodb.com/docs/manual/reference/configuration-options/
YAML 一个有12个核心选项,每个选项里又有众多参数。我们这里摘录如下:
systemLog:
verbosity: <int>
quiet: <boolean>
traceAllExceptions: <boolean>
syslogFacility: <string>
path: <string>
logAppend: <boolean>
logRotate: <string>
destination: <string>
timeStampFormat: <string>
component:
accessControl:
verbosity: <int>
command:
verbosity: <int>
# COMMENT additional component verbosity settings omitted for brevity
processManagement:
fork: <boolean>
pidFilePath: <string>
timeZoneInfo: <string>
cloud:
monitoring:
free:
state: <string>
tags: <string>
net:
port: <int>
bindIp: <string>
bindIpAll: <boolean>
maxIncomingConnections: <int>
wireObjectCheck: <boolean>
ipv6: <boolean>
unixDomainSocket:
enabled: <boolean>
pathPrefix: <string>
filePermissions: <int>
tls:
certificateSelector: <string>
clusterCertificateSelector: <string>
mode: <string>
certificateKeyFile: <string>
certificateKeyFilePassword: <string>
clusterFile: <string>
clusterPassword: <string>
CAFile: <string>
clusterCAFile: <string>
CRLFile: <string>
allowConnectionsWithoutCertificates: <boolean>
allowInvalidCertificates: <boolean>
allowInvalidHostnames: <boolean>
disabledProtocols: <string>
FIPSMode: <boolean>
logVersions: <string>
compression:
compressors: <string>
security:
keyFile: <string>
clusterAuthMode: <string>
authorization: <string>
transitionToAuth: <boolean>
javascriptEnabled: <boolean>
redactClientLogData: <boolean>
clusterIpSourceAllowlist:
- <string>
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>
clientCertificateSelector: <string>
serverCAFile: <string>
connectRetries: <int>
connectTimeoutMS: <int>
ldap:
servers: <string>
bind:
method: <string>
saslMechanisms: <string>
queryUser: <string>
queryPassword: <string | array>
useOSDefaults: <boolean>
transportSecurity: <string>
timeoutMS: <int>
userToDNMapping: <string>
authz:
queryTemplate: <string>
validateLDAPServerConfig: <boolean>
setParameter:
<parameter1>: <value1>
<parameter2>: <value2>
storage:
dbPath: <string>
journal:
enabled: <boolean>
commitIntervalMs: <num>
directoryPerDB: <boolean>
syncPeriodSecs: <int>
engine: <string>
wiredTiger:
engineConfig:
cacheSizeGB: <number>
journalCompressor: <string>
directoryForIndexes: <boolean>
maxCacheOverflowFileSizeGB: <number> // deprecated in MongoDB 4.4
collectionConfig:
blockCompressor: <string>
indexConfig:
prefixCompression: <boolean>
inMemory:
engineConfig:
inMemorySizeGB: <number>
oplogMinRetentionHours: <double>
operationProfiling:
mode: <string>
slowOpThresholdMs: <int>
slowOpSampleRate: <double>
filter: <string>
replication:
oplogSizeMB: <int>
replSetName: <string>
enableMajorityReadConcern: <boolean>
sharding:
clusterRole: <string>
archiveMovedChunks: <boolean>
auditLog:
destination: <string>
format: <string>
path: <string>
filter: <string>
snmp:
disabled: <boolean>
subagent: <boolean>
master: <boolean>
关于每个参数的说明,直接参考官方手册。
3 YAML 参数说明
以下内容是从网络找的一些过滤之后的yaml 参数,汇总如下:
3.1 系统日志相关的配置
systemLog:
destination: file --指定一个文件
path: "/mongodb/log/mongodb.log" --日志位置
logAppend: true --日志以追加模式记录
quiet: true --在quite模式下会限制输出信息
timeStampFormat: iso8601-utc --默认是iso8601-local,日志信息中还有其他时间戳格式:ctime,iso8601-utc,iso8601-local
#最后两个参数不常用。
3.2 数据存储相关的配置
storage:
journal: --是否开启redo日志
enabled: true
dbPath: "/mongodb/data" --数据存放的路径
directoryPerDB: true --指定存储每个数据库文件到单独的数据目录。如果在一个已存在的系统使用该选项,需要事先把存在的数据文件移动到目录。
engine: wiredTiger --指定mongodb数据引擎 默认为wiredTiger,也可不用指定
wiredTiger:
engineConfig: --wt引擎配置
cacheSizeGB: 1 --数据缓冲区的大小, 类似mysql中buffer pool;此值决定了 wiredTiger 与 mmapv1 的内存模型不同,它可以限制 mongod 对内存的使用量,而 mmapv1 则不能(依赖于系统级的 mmap)。默认情况下,cacheSizeGB 的值为假定当前节点只部署一个 mongod 实例,此值的大小为物理内存的一半;如果当前节点部署了多个 mongod 进程,那么需要合理配置此值。如果 mongod 部署在虚拟容器中(比如,lxc,cgroups,Docker)等,它将不能使用整个系统的物理内存,则需要适当调整此值。默认值为物理内存的一半
directoryForIndexes: true --是否将索引也按数据库名单独存储
collectionConfig: --表压缩配置;
blockCompressor: zlib --(默认snappy,还可选none、zlib) 压缩 算法 Tips:性能: none > snappy >zlib 压缩比:zlib > snappy > none
indexConfig: --索引配置
prefixCompression: true
operationProfiling:
slowOpThresholdMs: <NUM> --指定慢查询时间,单位毫秒,如果打开功能,则向system.profile集合写入数据
mode: "slowOp" --off、slowOp、all,分别对应关闭,仅打开慢查询,记录所有操作。
3.3 进程控制相关的配置
processManagement:
fork: true --后台守护进程
pidFilePath: <string> --pid文件的位置,一般不用配置,可以去掉这行,自动生成到data中
3.4 网络相关的配置
net:
bindIp: <ip> -- 监听地址,如果不配置这行是监听在0.0.0.0
port: <port> -- 端口号,默认不配置端口号,是27017
maxIncomingConnections:<NUM> --默认65535,mongodb实例接受的最多连接数,如果高于操作系统接受的最大线程数,设置无效。
3.5 安全验证相关的配置
security:
keyFile: "/data/mongodb-keyfile" --指定分片集或副本集成员之间身份验证的key文件存储位置
clusterAuthMode: "keyFile" --集群认证模式,默认是keyFile
authorization: "disabled" --访问数据库和进行操作的用户角色认证
3.6 复制集相关的配置
replication:
oplogSizeMB: <NUM> --默认为磁盘的5%,指定oplog的最大尺寸。对于已经建立过oplog.rs的数据库,指定无效
replSetName: "<REPSETNAME>" --指定副本集的名称
secondaryIndexPrefetch: "all" --指定副本集成员在接受oplog之前是否加载索引到内存。默认会加载所有的索引到内存。none不加载;all加载所有;_id_only仅加载_id
3.7 分片集相关的配置
sharding:
clusterRole: <string> --指定当前分片在集群中的角色:configsvr或shardsvr;shardsvr:表明是的后端shard节点 configsvr:表明在集群中的角色是config server配置节点
archiveMovedChunks: <boolean>
3.8 config server相关的配置
sharding:
clusterRole: configsvr --配置config server节点时,需要在分片配置中指定当前节点为config server
3.9 mongos 相关的配置
(mongos节点是前端路由,客户端由此接入。mongos节点一般不存储数据,所以配置文件中无需配置”storage”信息)
sharding:
configDB: <config server/IP...> --告诉mongos节点config server的地址信息。将整个config server复制集都告诉mongos,这样即使config server复制集中有一台config server损坏,mongos可以自动连接复制集中的其它config server.
如:
sharding:
configDB: configReplSet/10.0.0.51:38018,10.0.0.51:38019,10.0.0.51:38020
4 切换YAML 配置文件
最后将我的测试环境从传统的配置文件格式,切换到了YAML 格式。 如下:
[dave@www.cndba.cn logs]# cat /etc/mongo.conf
systemLog:
destination: file
path: "/mongodb/logs/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/mongodb/data"
#net:
# bindIp: 127.0.0.1
# port: 27017
security:
authorization: "enabled"
processManagement:
fork: true
[dave@www.cndba.cn logs]#
[dave@www.cndba.cn logs]# service mongodb start
about to fork child process, waiting until server is ready for connections.
forked process: 61487
child process started successfully, parent exiting
[dave@www.cndba.cn logs]# ps -ef|grep mongo
root 61487 1 1 16:16 ? 00:00:00 /usr/local/mongodb/bin/mongod -f /etc/mongo.conf
root 61908 16668 0 16:17 pts/0 00:00:00 grep --color=auto mongo
[dave@www.cndba.cn logs]#
版权声明:本文为博主原创文章,未经博主允许不得转载。



