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 以上版本) 

MongoDB——关于最大内存的设置 操作记录
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内存

MongoDB——关于最大内存的设置 操作记录
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
MongoDB——关于最大内存的设置 操作记录

 

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"})

 

相关文章:

  • 2021-06-19
  • 2021-05-24
  • 2022-12-23
  • 2021-12-13
  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-05
  • 2022-12-23
  • 2021-06-01
  • 2022-12-23
  • 2021-10-24
  • 2022-03-04
  • 2021-08-13
相关资源
相似解决方案