您的位置:首页 > 数据库 > Mongodb

RedHat7搭建MongoDB

2015-12-08 10:24 676 查看

yum安装MongoDB

添加MongoDB源
# vi /etc/yum.repos.d/mongodb-org-3.0.repo

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


MongoDB包

mongodb-org
This package is a metapackage that will automatically install the four component packages listed below.

mongodb-org-server
This package contains the mongod daemon and associated configuration and init scripts.

mongodb-org-mongos
This package contains the mongos daemon.

mongodb-org-shell
This package contains the mongo shell.

mongodb-org-tools
This package contains the following MongoDB tools: mongoimport bsondump, mongodump, mongoexport, mongofiles, mongooplog, mongoperf, mongorestore, mongostat, and mongotop.


安装MongoDB
# yum install -y mongodb-org

设置MongoDB服务开机自启动
# chkconfig mongod on

启动MongoDB服务
# service mongod start

备注:卸载MongoDB

停止MongoDB服务
# service mongod stop

卸载MongoDB安装包
# yum erase $(rpm -qa | grep mongodb-org)

删除数据库和日志目录
# rm -r /var/lib/mongo
# rm -r /var/log/mongodb

手动安装MongoDB

下载MongoDB安装包
# curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.7.tgz
解压缩MongoDB
# tar -zxvf mongodb-linux-x86_64-3.0.7.tgz

移动到安装目录
# mv mongodb-linux-x86_64-3.0.7 /usr/local/mongodb

设置环境变量
# echo "export PATH=\$PATH:/usr/local/mongodb/bin" > /etc/profile.d/mongodb.sh
# source /etc/profile.d/mongodb.sh

创建mongod用户和用户组
# useradd -r -M -s /sbin/nologin mongod

创建数据库和日志目录并修改属组
# mkdir -p /data/mongodb/{db,log}/
# chown -R mongod:mongod /data/mongodb/

创建MongoDB配置文件
# 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: /data/mongodb/log/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: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:


备注:

如果MongoDB端口不在27017-27019, 28017-28019之间,需要执行以下步骤:

# yum -y install policycoreutils-python

# semanage port -a -t mongod_port_t -p tcp <port_number>

创建MongoDB自启动脚本
# vi /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"

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

mongod=${MONGOD-/usr/local/mongodb/bin/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 64000

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


设置MongoDB服务开机自启动
# chmod +x /etc/init.d/mongod
# chkconfig mongod on

启动MongoDB服务
# service mongod start

备注:

如果启动发生错误,请检查数据库和日志目录及里面文件的权限是否设置正确,再次运行前须删除数据库目录下的mongod.lock文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: