MongoDB——關於最大內存的設置 操作記錄


Memory Use

With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache.

Starting in 3.4, the WiredTiger internal cache, by default, will use the larger of either:

  • 50% of (RAM - 1 GB), or
  • 256 MB.

To adjust the size of the WiredTiger internal cache, see

storage.wiredTiger.engineConfig.cacheSizeGB       in config file

--wiredTigerCacheSizeGB                                           in command

Avoid increasing the WiredTiger internal cache size above its default value.

 

查看mongod -h發現mongod提供了額外的可選參數來控制WiredTiger存儲引擎所占用的cache size。需要注意的是,cache size設置較低,同時mongodb復雜查詢很頻繁的話,會有延遲發生。

cacheSizeGB 指的就是Cache size,包括數據和索引。Mongod本身使用內存如連接池堆棧以及sorting buffer等都是額外的,不會被統計到這個數字里面。如果索引在內存,查詢冷數據取決於你的IO能力。如果IO latency很低,系統也沒有是高負載,那響應時間應該是毫秒級的區別。但是如果查詢很頻繁,又涉及到很多范圍、批量查詢,IOPS又跟不上,那就有可能到幾百幾千毫秒都有可能。

配置:

#設置最大占用內存

如下配置文件僅對 wiredTiger 引擎生效(3.0 以上版本) 

復制代碼
storage:
  dbPath: /data/mongodb/db
  journal:
    enabled: true
  engine: wiredTiger
  wiredTiger:
     engineConfig:
          cacheSizeGB: 5
復制代碼

配置mongoDB限制使用最大內存 命令行

啟動參數:./mongod -f config.cnf  --storageEngine wiredTiger --wiredTigerEngineConfigString="cache_size=300M"

shell查看是否生效: db.serverStatus().wiredTiger.cache 

 

docker方式啟動一個片,指定內存占用,容器和mongo都指定, 60% docker內容為宜

表示此容器最大占1G內存,禁用swap,數據和索引cache占0.6G內存

復制代碼
docker run -d --name mongoshard2 \
              -p 26003:27018  \
              -v /root/data/soft/mongo/shard2db:/data/db \
              -m 1G --memory-swap 1G \
              mongo:4.0.24 \
              --shardsvr \
              --replSet "replshard2" \
              --bind_ip_all \
              --storageEngine wiredTiger \
              --wiredTigerCacheSizeGB 0.6
復制代碼

 

9、重新設置每個節點的 cache_size 

由於每個分片都作了高可用,且數據文件映射到了宿主,

依次停止、刪除、用加wiredTigerCacheSizeGB參數的命令重啟,即可

docker run -d --name mongoshard1 \
              -p 26002:27018  \
              -v /root/data/soft/mongo/shard1db:/data/db \
              -m 1G --memory-swap 1G \
              mongo:4.0.24 \
              --shardsvr \
              --replSet "replshard1" \
              --bind_ip_all \
              --storageEngine wiredTiger \
              --wiredTigerCacheSizeGB 0.6

 

以下命令查看狀態

docker exec -it mongoshard1 mongo 192.168.1.21:26002 -eval "rs.status().members"
docker exec -it mongoshard1 mongo 192.168.1.22:26002/test \
-eval "db.serverStatus().wiredTiger.cache" | grep max

 關注 bytes read into cache 這個指標

嗯?? 調節 cache 規模不一定非得重啟服務,我們可以動態調整:

docker exec -it mongoconfig mongo 192.168.1.22:26001/test \
-eval "db.adminCommand({setParameter:1, wiredTigerEngineRuntimeConfig:'cache_size=600M'})"

cache_size

指定WT存儲引擎內部cache的內存用量上限。

需要注意的是,僅作用於WiredTiger cache,而非mongod進程的內存用量上限。MongoDB同時使用WT cache和文件系統cache,往往mongod進程的內存用量高於該值。cache_size相對於物理內存總量不要設置的太滿,需要留有一定內存為操作系統所用,否則有OOM潛在風險。

cache_size支持在不停服的情況下動態調整,比如將cache_size設置為80GB,執行如下命令:

db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig: "cache_size=80G"})

 


免責聲明!

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



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