CentOS 编译安装 MongoDB

MongoDB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐。Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象。Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作。

系统约定

mongodb源码路径:/usr/local/src
mongodb安装路径为/usr/local/mongodb
mongodb配置文件路径:/etc/mongod.conf
mongodb数据库路径:/data/mongodb
mongodb日志文件路径:/var/log/mongodb

创建mongod用户和组

# groupadd mongodb
# useradd -g mongodb -c "mongodb" -d /var/lib/mongodb -s /bin/false mongodb

下载

# cd /usr/local/src/
# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz

解压缩

# tar zxvf mongodb-linux-x86_64-2.4.9.tgz
# mv mongodb-linux-x86_64-2.4.9 /usr/local/mongodb

创建数据库目录

# mkdir -p /data/mongodb
# chown -R mongodb:mongodb /data/mongodb/

创建日志目录

# mkdir -p /var/log/mongodb
# chown -R mongodb:mongodb /var/log/mongodb/

创建配置文件

# vim /etc/mongod.conf

将以下内容复制粘贴到文件中

# mongo.conf

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

logappend=true

# each database will be stored in a separate directory
directoryperdb=true

# fork and run in background
fork = true

#port = 27017

dbpath=/data/mongodb

# location of pidfile
pidfilepath = /data/mongodb/mongod.pid

# 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

# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = 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 =

# Accout token for Mongo monitoring server.
#mms-token =

# Server name for Mongo monitoring server.
#mms-name =

# Ping interval for Mongo monitoring server.
#mms-interval =

# Replication Options

# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.123admin.com
# Slave only: specify a single database to replicate
#only = master.123admin.com
# or
#master = true
#source = slave.123admin.com

创建开机启动脚本

# vim /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/mongo/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"

# FIXME: 1.9.x has a --shutdown flag that parses the config file and
# shuts down the correct running pid, but that's unavailable in 1.8
# for now.  This can go away when this script stops supporting 1.8.
DBPATH=`awk -F= '/^dbpath=/{print $2}' "$CONFIGFILE"`
PIDFILE=`awk -F= '/^dbpath\s=\s/{print $2}' "$CONFIGFILE"`
mongod=${MONGOD-/usr/local/mongodb/bin/mongod}

MONGO_USER=mongodb
MONGO_GROUP=mongodb

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

start()
{
  echo -n $"Starting mongod: "
  daemon --user "$MONGO_USER" $NUMACTL $mongod $OPTIONS
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
}

stop()
{
  echo -n $"Stopping mongod: "
  killproc -p "$PIDFILE" -d 300 /usr/local/mongodb/bin/mongod
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod
}

restart () {
	stop
	start
}

ulimit -n 12000
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

配置mongodb开机启动

# chmod u+x /etc/init.d/mongod
# chkconfig mongod on

配置环境变量

# sed -i '/unset -f pathmunge/a\export PATH=$PATH:/usr/local/mongodb/bin' /etc/profile
# source /etc/profile

启动mongodb

# serivce mongod start

关闭mongodb

# service mongod stop

重启mongodb

# service mongod restart