MongoDB是一個高性能,開源,無模式的文檔型數據庫,是當前NoSql數據庫中比較熱門的一種。它在許多場景下可用於替代傳統的關系型數據庫或鍵/值存儲方式,Mongo使用C++開發。Mongo的官方網站地址是:,讀者可以在此獲得更詳細的信息。
特點:
它的特點是高性能、易部署、易使用,存儲數據非常方便。主要功能特性有:
功能:
適用場景:
不適用場景:
版本說明:
RedHat:6.1 x86_64
MongoDB:2.6.3
首先,我們到官網:,然後下載64位Linux 版的MongoDB;
然後,做配置MongoDB之前的准備工作;
# 創建MongoDB主目錄
mkdir /usr/local/mongodb/
# 解壓MongoDB包到MongoDB主目錄
tar -xvf mongodb-linux-x86_64-2.6.3.tgz -C /usr/local/mongodb/
# 創建MongoDB數據目錄,可以存放到其他位置,比如RAID、LVM上
mkdir /usr/local/mongodb/data/
# 創建MongoDB日志目錄,建議放到var目錄下
mkdir /usr/local/mongodb/log/
接著,我們使用mongod命令啟動MongoDB,再打開另一個終端,使用mongo命令連接到MongoDB;
# 進入MongoDB的bin目錄,啟動之
cd /usr/local/mongodb/bin/
./mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/mongo.log
# 登錄到MongoDB
./mongo
MongoDB shell version: 2.6.3
connecting to: test
>
接著,配置環境變量;
# 查看當前路徑
pwd
/usr/local/mongodb/bin
# 編輯bash_profile,內容如下
vim ~/.bash_profile
tail -n3 !$
tail -n3 ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mongodb/bin
export PATH
# 使配置生效
source !$
為了更方便的啟動和關閉MongoDB,我們可以使用Shell寫腳本,當然也可以加入到service中;
cp ssh mongodb
vim mongodb
cat mongodb
# 腳本內容如下:
#!/bin/bash
#
# mongod Start up the MongoDB server daemon
#
# source function library
. /etc/rc.d/init.d/functions
#定義命令
CMD=/usr/local/mongodb/bin/mongod
#定義數據目錄
DBPATH=/usr/local/mongodb/data
#定義日志目錄
LOGPATH=/usr/local/mongodb/log/mongo.log
start()
{
#fork表示後台運行
$CMD --dbpath=$DBPATH --logpath=$LOGPATH --fork
echo "MongoDB is running background..."
}
stop()
{
pkill mongod
echo "MongoDB is stopped."
}
case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo $"Usage: $0 {start|stop}"
esac
我們可以測試編寫腳本的正確性;
/etc/init.d/mongodb start
about to fork child process, waiting until server is ready for connections.
forked process: 1347
child process started successfully, parent exiting
MongoDB is running background...
/etc/init.d/mongodb stop
Terminated
當然,更好的方式是采用配置文件,把MongoDB需要的參數寫入配置文件,然後在腳本中引用;
vim mongodb.conf
cat mongodb.conf
#代表端口號,如果不指定則默認為27017
#port=27027
#MongoDB數據文件目錄
dbpath=/usr/local/mongodb/data
#MongoDB日志文件目錄
logpath=/usr/local/mongodb/log/mongo.log
#日志文件自動累加
logappend=true
編寫好配置文件後,我們需要修改啟動腳本;
vim mongodb
cat mongodb
#!/bin/bash
#
# mongod Start up the MongoDB server daemon
#
# source function library
. /etc/rc.d/init.d/functions
#定義命令
CMD=/usr/local/mongodb/bin/mongod
#定義配置文件路徑
INITFILE=/usr/local/mongodb/mongodb.conf
start()
{
#&表示後台啟動,也可以使用fork參數
$CMD -f $INITFILE &
echo "MongoDB is running background..."
}
stop()
{
pkill mongod
echo "MongoDB is stopped."
}
case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo $"Usage: $0 {start|stop}"
esac
編寫完成後,再次測試腳本的正確性。
# /etc/init.d/mongodb start
MongoDB is running background...
# /etc/init.d/mongodb stop
Terminated
優化過的MongoDB啟動腳本,如下:
#!/bin/bash
# Author: Robin Wen
# Date: 16:20:50 2014-12-12
# Desc: Mongodb startup and shutdown scripts.
# Mongod command path.
mongod=/usr/local/mongodb/bin/mongod
# MongoDB data path.
mongod_data=/data/db
# MongoDB config path.
mongod_conf=/etc/mongod.conf
# MongoDB log path.
mongod_log=/var/log/mongodb.log
# MongDB program name.
prog=mongod
RETVAL=0
# 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="`which numactl` -- $NUMACTL_ARGS"
DAEMON_OPTS=${DAEMON_OPTS:-"--config $mongod_conf"}
else
NUMACTL=""
DAEMON_OPTS="-- "${DAEMON_OPTS:-"--config $mongod_conf"}
fi
# Stop MongoDB function.
stop() {
grep_mongo=`ps aux | grep -v grep | grep "${mongod}"`
if [ ${#grep_mongo} -gt 0 ]
then
echo "MongoDB Stopped!"
PID=`ps x | grep -v grep | grep "${mongod}" \
| awk '{ print $1 }'`
`kill -9 ${PID}`
RETVAL=$?
else
echo "MongoDB is not running."
fi
}
# Start MongoDB function.
start() {
grep_mongo=`ps aux | grep -v grep | grep "${mongod}"`
if [ -n "${grep_mongo}" ]
then
echo "MongoDB is already running."
else
start-stop-daemon --background \
--start --quiet --exec $NUMACTL $mongod $DAEMON_OPTS
echo "MongoDB Started."
RETVAL=$?
fi
}
# MongoDB status funciton.
status() {
grep_mongo=`ps aux | grep -v grep | grep "${mongod}"`
if [ -n "${grep_mongo}" ]
then
echo "MongoDB is running."
else
echo "MongoDB is stopped."
RETVAL=$?
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo $"Usage: $prog {start|stop|restart|status}"
exit 1
esac
exit $RETVAL
這是深入理解MongoDB的第一篇文章,本篇文章簡要地介紹了MongoDB,並把Linux下完整的配置過程呈現給讀者。