CentOS 下安装MongoDB 3.0

Eave 2015.04.01 16:42

一、下载MongoDB 3.0

$ wget "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.0.1.tgz"

二、解压mongodb

将mongodb-linux-x86_64-rhel62-3.0.1下的bin目录中的所有文件都拷贝到/usr/local/mongodb中

$ cp -a mongodb-linux-x86_64-rhel62-3.0.1/bin /usr/local/mongodb

二、创建MongoDB配置文件

$ vim /etc/mongod.conf

将以下内容拷贝到/etc/mongod.conf中

# mongod.conf

#where to log
logpath=/var/log/mongodb/mongod.log

logappend=true

# fork and run in background
fork=true

#port=27017

dbpath=/var/lib/mongo

# location of pidfile
pidfilepath=/var/run/mongodb/mongod.pid

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip=127.0.0.1

# Disables write-ahead journaling
# nojournal=true

# Enables periodic logging of CPU utilization and I/O wait
#cpu=true

# Turn on/off security.  Off is currently the default
#noauth=true
#auth=true

# Verbose logging output.
#verbose=true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck=true

# Enable db quota management
#quota=true

# Set oplogging level where n is
#   0=off (default)
#   1=W
#   2=R
#   3=both
#   7=W+some reads
#diaglog=0

# Ignore query hints
#nohints=true

# Enable the HTTP interface (Defaults to port 28017).
#httpinterface=true

# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting=true

# Turns off table scans.  Any query that would do a table scan fails.
#notablescan=true

# Disable data file preallocation.
#noprealloc=true

# Specify .ns file size for new databases.
# nssize=

# Replication Options

# in replicated mongo databases, specify the replica set name here
#replSet=setname
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile

三、创建MongoDB启动脚本

$ vim /etc/init.d/mongod

将以下内容拷贝到/etc/init.d/mongod中

#!/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
# pidfile: /var/run/mongodb/mongod.pid

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

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


# 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"
SYSCONFIG="/etc/sysconfig/mongod"

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

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

MONGO_USER=mongod
MONGO_GROUP=mongod

if [ -f "$SYSCONFIG" ]; then
    . "$SYSCONFIG"
fi

PIDDIR=`dirname $PIDFILEPATH`

# 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

start()
{
    # Make sure the default pidfile directory exists
    if [ ! -d $PIDDIR ]; then
    install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR
    fi

    # Recommended ulimit values for mongod or mongos
    # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
    #
    ulimit -f unlimited
    ulimit -t unlimited
    ulimit -v unlimited
    ulimit -n 64000
    ulimit -m unlimited
    ulimit -u 32000

    if [ -f /var/lib/mongo/mongod.lock ];then
    rm -rf /var/lib/mongo/mongod.lock
    fi

    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

    rm -f "${pid_file}"

    checkpid $pid
    local RC=$?
    [ "$RC" -eq 0 ] && failure "${procname} shutdown" || success "${procname} shutdown"
    RC=$((! $RC))
    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

赋予脚本可执行权限

$ chmod 755 /etc/init.d/mongod

四、启动MongoDB

$ service mongod start