mongodb集群性能优化

2017/09/01

在前面两篇文章，我们介绍了如何去搭建mongodb集群，这篇文章我们将介绍如何去优化mongodb的各项配置，以达到最优的效果。

警告

不做任何的优化，集群搭建完成之后，使用命令连接mongodb终端，一般会遇到以下的警告信息：

如何你是用的是我最新一版集群搭建的脚本，估计警告会少几个，因为里面已经做了一些优化

2017-08-16T18:33:42.985+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-08-16T18:33:42.985+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-08-16T18:33:43.024+0800 I CONTROL [initandlisten]
2017-08-16T18:33:43.024+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-08-16T18:33:43.024+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-08-16T18:33:43.024+0800 I CONTROL [initandlisten]
2017-08-16T18:33:43.025+0800 I CONTROL [initandlisten]
2017-08-16T18:33:43.025+0800 I CONTROL [initandlisten] ** WARNING: You are running on a NUMA machine.
2017-08-16T18:33:43.025+0800 I CONTROL [initandlisten] ** We suggest launching mongod like this to avoid performance problems:
2017-08-16T18:33:43.025+0800 I CONTROL [initandlisten] ** numactl --interleave=all mongod [other options]
2017-08-16T18:33:43.026+0800 I CONTROL [initandlisten]
2017-08-16T18:33:43.026+0800 I CONTROL [initandlisten] ** WARNING: /proc/sys/vm/zone_reclaim_mode is 1
2017-08-16T18:33:43.026+0800 I CONTROL [initandlisten] ** We suggest setting it to 0
2017-08-16T18:33:43.026+0800 I CONTROL [initandlisten] ** http://www.kernel.org/doc/Documentation/sysctl/vm.txt
2017-08-16T18:33:43.026+0800 I CONTROL [initandlisten]
2017-08-16T18:33:43.027+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-08-16T18:33:43.027+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-08-16T18:33:43.027+0800 I CONTROL [initandlisten]
2017-08-16T18:33:43.027+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-08-16T18:33:43.027+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-08-16T18:33:43.027+0800 I CONTROL [initandlisten]

这其实是六条建议，下来我们分别来进行解读；

第一条，“Using the XFS filesystem is strongly recommended with the WiredTiger storage engine” ，意思就是强烈建议启用XFS文件系统，启用XFS文件系统会对性能有比较好的提升，我们使用的系统是centos6.9，重新格式化磁盘就可以。

第三条，“ You are running on a NUMA machine.We suggest launching mongod like this to avoid，numactl –interleave=all mongod [other options]”，我们运行的服务器CPU架构是MUMA，因此建议启动的时候加一些参数，可以提高性能，怎么加呢，看下面的命令，想多了解一些可以参考这篇文章：Mongodb NUMA 导致的性能问题

numactl --interleave=all mongod [other options]

第四条，“/proc/sys/vm/zone_reclaim_mode is 1，We suggest setting it to 0”，就是系统的这个值现在是1，请修改为0

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.
Avoid increasing the WiredTiger internal cache size above its default value.
With WiredTiger, MongoDB utilizes both the WiredTiger internal cache and the filesystem cache.
Via the filesystem cache, MongoDB automatically uses all free memory that is not used by the WiredTiger cache or by other processes. Data in the filesystem cache is compressed.
*NOTE**The storage.wiredTiger.engineConfig.cacheSizeGB limits the size of the WiredTiger internal cache. The operating system will use the available free memory for filesystem cache, which allows the compressed MongoDB data files to stay in memory. In addition, the operating system will use any free RAM to buffer file system blocks and file system cache.**To accommodate the additional consumers of RAM, you may have to decrease WiredTiger internal cache size.*
The default WiredTiger internal cache size value assumes that there is a single mongod instance per machine. If a single machine contains multiple MongoDB instances, then you should decrease the setting to accommodate the other mongod instances.
If you run mongod in a container (e.g. lxc, cgroups, Docker, etc.) that does not have access to all of the RAM available in a system, you must set storage.wiredTiger.engineConfig.cacheSizeGB to a value less than the amount of RAM available in the container. The exact amount depends on the other processes running in the container.