mongodb 配置文件


本文檔是在mongodb為3.4下編寫的,僅作為參考,詳細內容請參考:https://docs.mongodb.com/manual/reference/configuration-options/#configuration-file

一.說明

     配置mongodb有兩種方式,一種是通過mongod和mongos兩個命令;另外一種方式就是配置文件的方式。因為更容易去管理,所以后者更受大家的青睞。

二. 配置文件格式

    mongodb 配置文件采用的YAML格式;

    例如:

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和mongos命令去執行配置文件,這里要使用他們的一個選項--config(這里是兩個橫線,具體查看 > mongod --help)或者-f(--config的簡寫)

          例如:

mongod --config  D:/mongodb/mongod.conf

mongos --config  D:/mongodb/mongos.conf

  或

mongod -f  D:/mongodb/mongod.conf

mongos -f  D:/mongodb/mongos.conf

四 配置文件的核心選項

 1. systemLog 選項

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>

 systemLog.traceAllExceptions

               類型:boolean

               作用: 為調試打印詳細信息,用於支持相關的故障排除。

     systemLog.syslogFacility

               類型:string

               默認值:user

               作用:將mongodb使用日志記錄到系統日志中,如果要使用這個選項,必須開啟--sysylog選項

      systemLog.path

               類型:string

               作用:指定日志文件的目錄

        syetemLog.logAppend

               類型:boolean

                默認值:False

               作用:當mongod或mongos重啟時,如果為true,將日志追加到原來日志文件內容末尾;如果為false,將創建一個新的日志文件

         systemLog.destination

               類型:string

               作用:指定日志文件的路徑,如果設置了這個值,必須指定systemLog.path.如果沒有設置,日志會標准的輸出到后台

         systemLog.timeStampFormat

                類型:string

                默認值:iso8601-local

               作用:為日志添加時間戳。

描述
ctime  顯示時間戳格式為:Wed Dec 31 18:17:54.811.
iso8601-utc 安裝iso-8601-utc格式顯示:1970-01-01T00:00:00.000Z
iso8601-local 按照iso8601-local格式顯示:1969-12-31T19:00:00.000-0500

  processMangement 選項    

processManagement:
   fork: <boolean>
   pidFilePath: <string>

  processMangement.fork

             類型:Boolean

             默認值:False

            作用:在前台啟動Mongodb進程,如果Session窗口關閉,Mongodb進程也隨之停止。不過Mongodb同時還提供了一種后台Daemon方式啟動,只需要加上一個"--fork"參數即可,值得注意的是,用到了"--fork"參數就必須啟用"--logpath"參數。如下所示:

       

[root@localhost mongodb]# ./bin/mongod --dbpath=data/db --fork  
--fork has to be used with --logpath  
[root@localhost mongodb]# ./bin/mongod --dbpath=data/db --fork --logpath=log/mongodb.log   
all output going to: /opt/mongodb/log/mongodb.log  
forked process: 3300  

  daemon方式啟動的fork參數也可以配置配置文件中,如下所示:

port=27017  
dbpath=data/db  
logpath=log/mongodb.log  
logappend=true  
fork=true  

 net 選項 

 

net:
   port: <int>
   bindIp: <string>
   maxIncomingConnections: <int>
   wireObjectCheck: <boolean>
   ipv6: <boolean>
   unixDomainSocket:
      enabled: <boolean>
      pathPrefix: <string>
      filePermissions: <int>
   http:
      enabled: <boolean>
      JSONPEnabled: <boolean>
      RESTInterfaceEnabled: <boolean>
   ssl:
      sslOnNormalPorts: <boolean>  # deprecated since 2.6
      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>

  net.port

                類型:integer

               默認值:27017

                作用:設置mongodb的監聽TCP端口

       net.bindIp

                類型:string

                作用:設置mongodb服務器監聽ip地址,默認是127.0.0.1;如果監聽多個ip地址,使用逗號隔開

        net.maxIncomingConnections

                類型:integer 

               默認值:65536

               作用:最大並發鏈接數

       net.ipv6

              類型:boolean

              默認值:false

             作用:開啟或關閉支持ipv6地址;

  security 選項     

 

security:
   keyFile: <string>
   clusterAuthMode: <string>
   authorization: <string>
   transitionToAuth: <boolean>
   javascriptEnabled:  <boolean>
   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>
         saslMechanisms: <string>
         queryUser: <string>
         queryPassword: <string>
         useOSDefaults: <boolean>
      transportSecurity: <string>
      timeoutMS: <int>
      userToDNMapping: <string>
      authz:
         queryTemplate: <string>

  ....查看mongodb手冊 #security選項

setParameter 選項

     設置mongodb參數,查看參數列表

     采用YAML語言格式

setParameter:
   <parameter1>: <value1>
   <parameter2>: <value2>

  storage 選項

   

storage:
   dbPath: <string>
   indexBuildRetry: <boolean>
   repairPath: <string>
   journal:
      enabled: <boolean>
      commitIntervalMs: <num>
   directoryPerDB: <boolean>
   syncPeriodSecs: <int>
   engine: <string>
   mmapv1:
      preallocDataFiles: <boolean>
      nsSize: <int>
      quota:
         enforced: <boolean>
         maxFilesPerDB: <int>
      smallFiles: <boolean>
      journal:
         debugFlags: <int>
         commitIntervalMs: <num>
   wiredTiger:
      engineConfig:
         cacheSizeGB: <number>
         journalCompressor: <string>
         directoryForIndexes: <boolean>
      collectionConfig:
         blockCompressor: <string>
      indexConfig:
         prefixCompression: <boolean>
   inMemory:
      engineConfig:
         inMemorySizeGB: <number>

  storage.dbPath

                類型:string

                默認值:/data/db(linux和macOS系統) ,\data\db(window系統)

               作用:設置數據存儲文件目錄

     storage.indexBuildRetry

               類型:boolean

               默認值:true

               作用:開啟或關閉是否在mongod下次啟動重建不完整的索引。

               注:在in-memory存儲引擎下不可用

storage.repairPath

               類型:string

               默認值:在dbpath下的A _tmp_repairDatabase_<num> 文件目錄

               作用:為進行恢復操作指定目錄

                注意:僅僅在MMAPv1存儲引擎下可用

  storage.journal.enabled

              類型:boolean

               默認值:true(64-bit系統);false(32-bit系統)

               作用:開啟和關閉journal,為了保證數據文件的有效性和可恢復性;在設置了dbpath之后有效

                注:在in-memory存儲引擎下不可用

        

   storage.directoryPerDB

       類型:boolean

        默認值:false

         作用:當為true,mongodb為每個數據庫建立一個單獨的路徑,這個路徑是在dbpath下創建的;每次創建需要重啟服務器

           注:在in-memory存儲引擎下不可用

   storage.engine

           默認值:wiredTiger

           作用:這是數據存儲引擎

描述
MMAPV1 MMAPCV1 storage engine
wiredTiger WiredTiger Storage Engine.
inMemory In-Memory Storage Engine.

   storage.mmapv1 選項

storage:
   mmapv1:
      preallocDataFiles: <boolean>
      nsSize: <int>
      quota:
         enforced: <boolean>
         maxFilesPerDB: <int>
      smallFiles: <boolean>
      journal:
         debugFlags: <int>
         commitIntervalMs: <num>

  storage.mmapv1.preallocDataFiles

                 類型:boolean

                 默認值:true

                 作用:開啟或關閉數據文件的預分配;

   storage.mmapv1.quota.enforced

                 類型:boolean

                 默認值:false

                  作用:開啟或關閉每個數據庫中的數據文件個數的限額;默認是每個數據庫最多有8個數據文件,通過調整storage.mmapv1.quota.maxFilesPerDB

        storage.mmapv1.quota.maxFilesPerDB

                  類型:integer

                      默認值:8

                      作用:設置每個數據庫中數據文件的限制個數;

     storage.wiredTiger 選項

storage:
   wiredTiger:
      engineConfig:
         cacheSizeGB: <number>
         journalCompressor: <string>
         directoryForIndexes: <boolean>
      collectionConfig:
         blockCompressor: <string>
      indexConfig:
         prefixCompression: <boolean>

  storage.wiredTiger.engineConfig.cacheSizeGB

                  類型:float

                  作用:設置緩存大小,從3.4版本開始,內存的50%-1GB 和256MB的最大值

        storage.wriedTiger.engineConfig.journalCompressor

                  默認值:snappy

                  作用:設置journal壓縮方式;可選項:none/snappy/zlib

  storage.inmemory 選項

storage:
   inMemory:
      engineConfig:
         inMemorySizeGB: <number>

  storage.inmemory.engineConfig.inMemorySizeGB

                     類型:float

                     默認值:物理內存的50%-1GB

                    作用:設置緩沖區大小;

    opeartionProfiling 選項       

operationProfiling:
   slowOpThresholdMs: <int>
   mode: <string>

  opeartionProfiling.slowOpThresholdMs

                類型:integer

                默認值:100

                 作用:設置區分慢查詢的一個閾值,比如100,當查詢速度大於100ms,記錄到日志中

        operationProfiling.mode

                 類型:string

                 默認值:off

      replication 選項       

replication:
   oplogSizeMB: <int>
   replSetName: <string>
   secondaryIndexPrefetch: <string>
   enableMajorityReadConcern: <boolean>

  replication.oplogSizeMB

               類型:integer

                作用:設置復制日志文件的大小;

 sharding 選項   

sharding:
   clusterRole: <string>
   archiveMovedChunks: <boolean>

   sharding.clusterRole

              類型:string

               作用:設置分片集群中的角色;

值   描述
configsvr 作為配置服務器,默認端口27019
shardsvr 作為一個分片,默認端口27018

 auditLog 選項 (MongoDB Enterprise可用)

auditLog:
   destination: <string>
   format: <string>
   path: <string>
   filter: <string>

  auditLog.destination 

                 類型:string

                 作用:審計日志位置;

描述
syslog JSON文件格式輸出在系統日志中,window系統中不可用
console JSON格式輸出
file 輸出到文件

    auditLog.format

              類型:string

              作用:設置設計日志輸出格式;可用值:JSON/BSON

   auditLog.path

             類型:string

                作用:設計日志輸出文件路徑

    auditLog.filter

             類型:string

              作用:審計日志過濾器

              格式:{ <field1>: <expression1>, ... }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

              

              

              


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM