MongoDB 3.6 部署实录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB 3.6 部署实录相关的知识,希望对你有一定的参考价值。

[[email protected] ~]# groupadd mongod

[[email protected] ~]# useradd -s /sbin/nologin -M -g mongod mongod 


[[email protected] ~]# tar zxvf mongodb-linux-x86_64-3.6.3.tgz -C /usr/local/

[[email protected] ~]# cd /usr/local

[[email protected] local]# mv mongodb-linux-x86_64-3.6.3 mongodb


[[email protected] ~]# mkdir -p /usr/local/mongodb/data

[[email protected] ~]# mkdir -p /usr/local/mongodb/logs


[[email protected] ~]# chown -R mongod:mongod /usr/local/mongodb/data/

[[email protected] ~]# chown -R mongod:mongod /usr/local/mongodb/logs/


[[email protected] ~]# mkdir -p /var/run/mongodb

[[email protected] ~]# chown -R mongod:mongod /var/run/mongodb


[[email protected] ~]# vi /etc/security/limits.conf

mongod          soft    nofile          64000

mongod          hard    nofile          64000

mongod          soft    nproc           32000

mongod          hard    nproc           32000


[[email protected] ~]# vi /etc/profile

PATH=$PATH:/usr/local/mongodb/bin

[[email protected] ~]# source /etc/profile


[[email protected] ~]# vi /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: /usr/local/mongodb/logs/mongod.log


# Where and how to store data.

storage:

  dbPath: /usr/local/mongodb/data

  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

  timeZoneInfo: /usr/share/zoneinfo


# network interfaces

net:

  port: 27017

  bindIp: 192.168.1.201  # Listen to local interface only, comment to listen on all interfaces.


#security:


#operationProfiling:


#replication:


#sharding:


## Enterprise-Only Options


#auditLog:


#snmp:


[[email protected] ~]# vi /etc/init.d/mongodb

#!/bin/bash


# mongod - Startup script for mongod


# chkconfig: 35 85 15

# description: Mongo is a scalable, document-oriented database.

# processname: mongod

# config: /etc/mongod.conf


. /etc/rc.d/init.d/functions


# NOTE: if you change any OPTIONS here, you get what you pay for:

# this script assumes all options are in the config file.

CONFIGFILE="/etc/mongod.conf"

OPTIONS=" -f $CONFIGFILE"


mongod=${MONGOD-/usr/local/mongodb/bin/mongod}


MONGO_USER=mongod

MONGO_GROUP=mongod


# All variables set before this point can be overridden by users, by

# setting them directly in the SYSCONFIG file. Use this to explicitly

# override these values, at your own risk.

SYSCONFIG="/etc/sysconfig/mongod"

if [ -f "$SYSCONFIG" ]; then

    . "$SYSCONFIG"

fi


# Handle NUMA access to CPUs (SERVER-3574)

# This verifies the existence of numactl as well as testing that the command works

NUMACTL_ARGS="--interleave=all"

if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null

then

    NUMACTL="numactl $NUMACTL_ARGS"

else

    NUMACTL=""

fi


# things from mongod.conf get there by mongod reading it

PIDFILEPATH="`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' \"$CONFIGFILE\" | tr -d \"[:blank:]\\"'\" | awk -F'#' '{print $1}'`"

PIDDIR=`dirname $PIDFILEPATH`


start()

{

  # Make sure the default pidfile directory exists

  if [ ! -d $PIDDIR ]; then

    install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR

  fi


  # Make sure the pidfile does not exist

  if [ -f "$PIDFILEPATH" ]; then

      echo "Error starting mongod. $PIDFILEPATH exists."

      RETVAL=1

      return

  fi


  # Recommended ulimit values for mongod or mongos

  # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

  #

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then 

   echo never > /sys/kernel/mm/transparent_hugepage/enabled 

fi 

if test -f /sys/kernel/mm/transparent_hugepage/defrag; then 

    echo never > /sys/kernel/mm/transparent_hugepage/defrag 

fi  

ulimit -f unlimited

  ulimit -t unlimited

  ulimit -v unlimited

  ulimit -n 64000

  ulimit -m unlimited

  ulimit -u 64000

  ulimit -l unlimited


  echo -n $"Starting mongod: "

  daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"

  RETVAL=$?

  echo

  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod

}


stop()

{

  echo -n $"Stopping mongod: "

  mongo_killproc "$PIDFILEPATH" $mongod

  RETVAL=$?

  echo

  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod

}


restart () {

        stop

        start

}


# Send TERM signal to process and wait up to 300 seconds for process to go away.

# If process is still alive after 300 seconds, send KILL signal.

# Built-in killproc() (found in /etc/init.d/functions) is on certain versions of Linux

# where it sleeps for the full $delay seconds if process does not respond fast enough to

# the initial TERM signal.

mongo_killproc()

{

  local pid_file=$1

  local procname=$2

  local -i delay=300

  local -i duration=10

  local pid=`pidofproc -p "${pid_file}" ${procname}`


  kill -TERM $pid >/dev/null 2>&1

  usleep 100000

  local -i x=0

  while [ $x -le $delay ] && checkpid $pid; do

    sleep $duration

    x=$(( $x + $duration))

  done


  kill -KILL $pid >/dev/null 2>&1

  usleep 100000


  checkpid $pid # returns 0 only if the process exists

  local RC=$?

  [ "$RC" -eq 0 ] && failure "${procname} shutdown" || rm -f "${pid_file}"; success "${procname} shutdown"

  RC=$((! $RC)) # invert return code so we return 0 when process is dead.

  return $RC

}


RETVAL=0


case "$1" in

  start)

    start

    ;;

  stop)

    stop

    ;;

  restart|reload|force-reload)

    restart

    ;;

  condrestart)

    [ -f /var/lock/subsys/mongod ] && restart || :

    ;;

  status)

    status $mongod

    RETVAL=$?

    ;;

  *)

    echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"

    RETVAL=1

esac


exit $RETVAL


[[email protected] ~]# chmod a+x /etc/init.d/mongod


[[email protected] ~]# /etc/init.d/mongod start

Starting mongod:                                           [  OK  ]

[[email protected] ~]# /etc/init.d/mongod status

mongod (pid 1263) is running...


以上是关于MongoDB 3.6 部署实录的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB自动备份全过程实录

centos7 安装mongodb 3.6

MongoDB:如何在 3.6 mongoDb 版本中解析日期?

MongoDB:如何在 3.6 mongoDb 版本中解析日期?

centos6 7 yum安装mongodb 3.6

Graylog 部署(Centos 6/7+ MongoDB3* + Graylog 3.2 + Elasticsearch6* )