Background

通常第一次在Linux安裝完Mongodb都會出現下列的WARNING. 好奇了一下去查了一下原因&解決的方法. 並記錄一下.

MongoDB shell version: 3.2.9
connecting to: test
Server has startup warnings:
2016-10-16T21:09:32.964+0800 I CONTROL  [initandlisten]
2016-10-16T21:09:32.964+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-10-16T21:09:32.964+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-10-16T21:09:32.964+0800 I CONTROL  [initandlisten]
2016-10-16T21:09:32.964+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-10-16T21:09:32.964+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-10-16T21:09:32.964+0800 I CONTROL  [initandlisten]

Transparent Huge Pages (THP) 是一個Linux的記憶體管理系統. 它透過增大Page的Size來得到一些記憶體管理上的一些優勢. 但是對大部分的DB在THP上只會得到Poor的Performace.

詳細的介紹可以參考這篇: http://www.ahlinux.com/start/kernel/11738.html

所以官方都建議都是把THP的功能關閉. 來達到一個比較好的Performance. 下面為官方教學:

1. Create the init.d script.

Create the following file at /etc/init.d/disable-transparent-hugepages:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    re='^[0-1]+$'
    if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
    then
      # RHEL 7
      echo 0  > ${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > ${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac

2. Make it executable.

sudo chmod 755 /etc/init.d/disable-transparent-hugepages

3. Start init.d script

sudo /etc/init.d/disable-transparent-hugepages start

4. Restart mongodb

sudo service mongod restart

Reference