正文:
mongodb占用内存非常高,这是因为官方为了提升存储的效率,设计就这么设计的。
但是大部分的个人开发者所购买的服务器内存并没有那么大,所以,我们需要配置下MongoDB的内存缓存大小,不然mongodb会占用非常多。
官方的配置缓存项处文档是这么解释的:
WiredTiger Options
--wiredTigerCacheSizeGB
number
-
New in version 3.0.
Defines the maximum size of the internal cache that WiredTiger will use for all data.
With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache.
Changed in version 3.2: Starting in MongoDB 3.2, the WiredTiger internal cache, by default, will use the larger of either:
- 60% of RAM minus 1 GB, or
- 1 GB.
mongodb会尽可能的把所有的数据都缓存,以便提高效率。
以mongodb 3.2为例,WiredTiger内部缓存,默认会用掉
- 60% * 内存 - 1GB
- 1GB
当你的内存大于1GB,mongodb会用掉 内存的60% - 1GB 的内存作为缓存;
当你的内存小于1GB,mongodb会直接用掉1GB。
另外,MongoDB 3.4与3.2也是有区别的,MongoDB 3.4该配置项为:
storage.wiredTiger.engineConfig.
cacheSizeGB
Type: float
The maximum size of the internal cache that WiredTiger will use for all data.
Changed in version 3.4: Values can range from 256MB to 10TB and can be a float. In addition, the default value has also changed.
Starting in 3.4, the WiredTiger internal cache, by default, will use the larger of either:
- 50% of RAM minus 1 GB, or
- 256 MB.
这样显然很不合理,对于大部分的个人开发,内存是宝贵的。所以,我们需要配置为MB。
配置项参考此处配置:WiredTiger cache size is only configurable in whole gigabytes.
下面是修改后的配置:/etc/mongod.conf
# Where and how to store data. storage: dbPath: /var/lib/mongo #dbPath: /mongodata journal: enabled: true # engine: mmapv1: smallFiles: true wiredTiger: engineConfig: configString : cache_size=512M
其实重点就是下面一项,配置之后,重启mongodb生效:
wiredTiger: engineConfig: configString : cache_size=512M
文章的脚注信息由WordPress的wp-posturl插件自动生成