一、系统环境
CentOS 6.8_x64
官方参考文档https://docs.mongodb.org/manual/reference/glossary/#term-init-script

二、添加官方yum库
#cd /etc/yum.repo.d/
#vim  mongodb.repo

[mongodb-org-3.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/
gpgcheck=0
enabled=1

三、安装配置
1、安装并创建数据目录

#yum install -y mongodb-org
#mkdir -p /Data/mongodb 
#chown   mongod.mongod /Data/mongodb -R


2、配置mongod.conf
#vim /etc/mongod.conf

# mongod.conf
# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /Data/mongodb/mongod.log    #需要自定义
# Where and how to store data.
storage:
  dbPath: /Data/mongodb/db           #需要自定义
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:
# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
# network interfaces
net:
  port: 27017
  bindIp: 10.1.0.7  # Listen to local interface only, comment to listen on all interfaces.    需要自定义
#security:
#operationProfiling:
#replication:
#sharding:
# Enterprise-Only Options
#auditLog:

启动mongod
#service mongod start


四、测试
登录mongodb
#mongo --host 10.1.0.7
> db.version();
3.0.7
> show dbs
com_ylt_plat_passport  0.078GB
local                  0.078GB
chown mongod.mongod   /Data/mongodb -R
service  mongod start


五、排错

故障描述 :
service mongod stop 时发现 并没有 关闭mongod服务 进程依然在

通过排查发现问题出在/etc/mongod.conf中第24行
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  把后面的# location of pidfile 删除掉  即可,这个是一个小bug

六、mogond备份与还原脚本

#cat mongodb_bak.sh

#!/bin/sh
DUMP=/usr/bin/mongodump
OUT_DIR=/data1/backup/mongodb/mongod_bak_now
BAK_DIR=/data1/backup/mongodb/mongod_bak_list
DATE=`date +%F_%H%M%d`
#DB_USER=username
#DB_PASS=
DAYS=7
TAR_BAK="mongodb_bak_$DATE.tar.gz"
[ -d $OUT_DIR ] || mkdir -v $OUT_DIR
[ -d $BAK_DIR ] || mkdir -v $BAK_DIR
BAK_DB(){
cd $OUT_DIR
rm -rf $OUT_DIR/*
mkdir -p $DATE

#$DUMP -u $DB_USER -p $DB_PASS -o $OUT_DIR/$DATE
$DUMP -o $OUT_DIR/$DATE
tar czvf $BAK_DIR/$TAR_BAK $OUT_DIR/$DATE
find $BAK_DIR/ -mtime +$DAYS -delete
}

RESTORE_ALL(){
cd $OUT_DIR
for d in `ls`;
do
echo $OUT_DIR/$d
/usr/bin/mongorestore -d $OUT_DIR/$d
done
}
RESTORE_Choose(){
while true
do
  echo "when you choose 'quit|exit' exit to restore!" 
  read -p "What's  your choose?(Enter continue!)" choose
  if [[ $choose == 'quit' || $choose == 'exit' ]]
  then
       echo "You choose exit!" && exit 2
  fi
  cd $OUT_DIR
  d=`ls`
  cd $OUT_DIR/$d
  ls
  read -p "What's db your choose?" whatdb
  if [ "$whatdb" != '' ];
    then
      /usr/bin/mongorestore -d $whatdb
  else
        echo "choose is empty,exit~" && exit 0
  fi
done
}

case $1 in
        back)
        BAK_DB
        ;;
        resall)
        RESTORE_ALL
        ;;
        resone)
        RESTORE_Choose
        ;;
        *)
        echo "USGE:back|resall|resone"
        ;;
esac

使用说明:

back   备份全部的mongod数据库

resall  还原所有的数据库

resone可以指定还原某一个数据库



七、解决警告提示
 1、问题描述
解决登录mongo --host 10.1.0.7 --port 27017   类似如下提示

  ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.   和 ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
MongoDB shell version: 3.0.7
connecting to: 10.1.0.7:27017/test
Server has startup warnings:
2016-12-08T16:10:15.638+0800 I CONTROL  [initandlisten]
2016-12-08T16:10:15.638+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-12-08T16:10:15.638+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-12-08T16:10:15.638+0800 I CONTROL  [initandlisten]
2016-12-08T16:10:15.638+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-12-08T16:10:15.638+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-12-08T16:10:15.638+0800 I CONTROL  [initandlisten]


由于环境为CentOS6.8 所以解决方法如下,其他平台及版本请参考官方文档:https://docs.mongodb.org/manual/tutorial/transparent-huge-pages/

 

2、解决方法:
添加如下脚本
#vim /etc/init.d/disable-transparent-hugepages

#!/bin/sh### 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    
        unset thp_path
    ;;
    esac

添加到开机自启服务

#chmod +x /etc/init.d/disable-transparent-hugepages
#chkconfig --add disable-transparent-hugepages


3、修改系统参数

#mkdir -p /etc/tune-profiles/no-thp
#cd /etc/tune-profiles/no-thp
#echo "set_transparent_hugepages never" > ktune.sh
#chmod +x ktune.sh
#tuned-adm profile no-thp 如果提示找不到命令请执行yum install tuned -y

reboot 系统

4、验证:

$mongo --host 10.1.0.7 --port 27017

MongoDB shell version: 3.0.7

connecting to: 10.1.0.7:27017/test

>

5、出现如下错误:

** WARNING: soft rlimits too low. rlimits set to 1024 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.

#vim /etc/security/limits.conf

添加:

mongod soft nofile 64000

mongod hard nofile 64000

mongod soft nproc 32000

mongod hard nproc 32000


重启mongod

到此mongod安装完成~如有错误之处欢迎指正!