您好,登錄后才能下訂單哦!
MongoDB是一個基于分布式文件存儲的數(shù)據(jù)庫。由C++語言編寫。旨在為WEB應(yīng)用提供可擴展的高性能數(shù)據(jù)存儲解決方案。它以高性能、易部署、易使用,存儲數(shù)據(jù)非常方便等優(yōu)點被廣泛使用。其安裝配置相當簡單,有如輕量級的mysql,但功能絲毫不差。本文主要描述mongoDB的啟動與停止。
一、準備環(huán)境
1、當前環(huán)境
# more /etc/redhat-release
CentOS release 6.7 (Final)
2、安裝mongod
# which mongod
/var/lib/mongodb/bin/mongod
參考:
Linux下快速安裝MongoDB
Windows平臺下安裝MongoDB
3、創(chuàng)建數(shù)據(jù)目錄
# mkdir -pv /data/mongodata/{rs1,rs2,rs3}
# mkdir -pv /var/lib/mongodb/conf
二、啟動mongodb
1、基于命令行方式啟動mongodb
# mongod --dbpath=/data/mongodata/rs1 --logpath=/data/mongodata/rs1/rs1.log &
缺省端口為
[root@node3 rs1]# netstat -nltp|grep mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 5062/mongod
2、基于配置文件的命令行啟動
vi /var/lib/mongodb/conf/rs2.conf
port = 27000
dbpath = /data/mongodata/rs2
logpath = /data/mongodata/rs2/rs2.log
smallfiles = true
fork = true
pidfilepath = /var/run/mongo.pid
# mongod --config /var/lib/mongodb/conf/rs2.conf &
### Author : Leshami
### Blog : http://blog.csdn.net/leshami
# netstat -nltp|grep 27000
tcp 0 0 0.0.0.0:27000 0.0.0.0:* LISTEN 5356/mongod
3、以守護進程方式啟動mongodb
# mongod --dbpath=/data/mongodata/rs3 --logpath=/data/mongodata/rs1/rs3.log --fork --port 28000
# netstat -nltp|grep mongod
tcp 0 0 0.0.0.0:28000 0.0.0.0:* LISTEN 5465/mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 5435/mongod
tcp 0 0 0.0.0.0:27000 0.0.0.0:* LISTEN 5448/mongod
4、使用系統(tǒng)服務(wù)的方式啟動mogodb
啟動腳本
# vi /etc/init.d/mongod
#!/bin/sh
# chkconfig: 2345 93 18
#MogoDB home directory
MONGODB_HOME=/var/lib/mongodb
#mongodb command
MONGODB_BIN=$MONGODB_HOME/bin/mongod
#mongodb config file
MONGODB_CONF=$MONGODB_HOME/conf/mongodb.conf
#mongodb PID
MONGODB_PID=/var/run/mongo.pid
#set open file limit
SYSTEM_MAXFD=65535
MONGODB_NAME="mongodb"
. /etc/rc.d/init.d/functions
if [ ! -f $MONGODB_BIN ]
then
echo "$MONGODB_NAME startup: $MONGODB_BIN not exists! "
exit
fi
start(){
ulimit -HSn $SYSTEM_MAXFD
$MONGODB_BIN --config="$MONGODB_CONF" --fork ##added @20160901
ret=$?
if [ $ret -eq 0 ]; then
action $"Starting $MONGODB_NAME: " /bin/true
else
action $"Starting $MONGODB_NAME: " /bin/false
fi
}
stop(){
PID=$(ps aux |grep "$MONGODB_NAME" |grep "$MONGODB_CONF" |grep -v grep |wc -l)
if [[ $PID -eq 0 ]];then
action $"Stopping $MONGODB_NAME: " /bin/false
exit
fi
kill -HUP `cat $MONGODB_PID`
ret=$?
if [ $ret -eq 0 ]; then
action $"Stopping $MONGODB_NAME: " /bin/true
rm -f $MONGODB_PID
else
action $"Stopping $MONGODB_NAME: " /bin/false
fi
}
restart() {
stop
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
esac
# chmod u+x /etc/init.d/mongod
# service mongod start
about to fork child process, waiting until server is ready for connections.
forked process: 5543
child process started successfully, parent exiting
Starting mongodb: [ OK ]
三、停止mongoDB
1、向mongod進程發(fā)送信號
###SIGINT信號
# ps -ef|grep mongod|grep rs1
root 5435 4914 1 19:13 pts/2 00:00:14 mongod --dbpath=/data/mongodata/rs1 --logpath=/data/mongodata/rs1/rs1.log
# kill -2 5435
2016-08-30T17:02:00.528+0800 I CONTROL[signalProcessingThread] got signal 2(Interrupt), will terminate after current cmd ends
2016-08-30T17:02:00.530+0800 I REPL [signalProcessingThread] Stopping replication applier threads
2016-08-30T17:02:00.554+0800 I STORAGE [conn1253] got request after shutdown()
2016-08-30T17:02:00.774+0800 I CONTROL [signalProcessingThread] now exiting
2016-08-30T17:02:00.774+0800 I NETWORK [signalProcessingThread] shutdown: going to close listening sockets...
2016-08-30T17:02:00.774+0800 I NETWORK [signalProcessingThread] closing listening socket: 6
2016-08-30T17:02:00.775+0800 I NETWORK [signalProcessingThread] closing listening socket: 7
2016-08-30T17:02:00.775+0800 I NETWORK [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2016-08-30T17:02:00.775+0800 I NETWORK [signalProcessingThread] shutdown: going to flush diaglog...
2016-08-30T17:02:00.775+0800 I NETWORK [signalProcessingThread] shutdown: going to close sockets...
2016-08-30T17:02:00.775+0800 I STORAGE [signalProcessingThread] shutdown: waiting for fs preallocator...
2016-08-30T17:02:00.775+0800 I STORAGE [signalProcessingThread] shutdown: final commit...
2016-08-30T17:02:00.775+0800 I JOURNAL [signalProcessingThread] journalCleanup...
2016-08-30T17:02:00.775+0800 I JOURNAL [signalProcessingThread] removeJournalFiles
2016-08-30T17:02:00.777+0800 I NETWORK [conn1254] end connection 192.168.1.247:58349 (0 connections now open)
2016-08-30T17:02:00.779+0800 I JOURNAL [signalProcessingThread] Terminating durability thread ...
2016-08-30T17:02:00.881+0800 I JOURNAL [journal writer] Journal writer thread stopped
2016-08-30T17:02:00.882+0800 I JOURNAL [durability] Durability thread stopped
2016-08-30T17:02:00.882+0800 I STORAGE [signalProcessingThread] shutdown: closing all files...
2016-08-30T17:02:00.884+0800 I STORAGE [signalProcessingThread] closeAllFiles() finished
2016-08-30T17:02:00.884+0800 I STORAGE [signalProcessingThread] shutdown: removing fs lock...
2016-08-30T17:02:00.885+0800 I CONTROL [signalProcessingThread] dbexit: rc: 0
###SIGTERM信號
# ps -ef|grep mongod|grep rs3
# ps -ef|grep mongod|grep rs3
root 5465 1 1 19:14 ? 00:00:13 mongod --dbpath=/data/mongodata/rs3 --logpath=/data/mongodata/rs1/rs3.log --fork --port 28000
# kill -4 5465
信號 產(chǎn)生方式
sigint 通過ctrl+c將會對當進程發(fā)送此信號
sigterm kill命令不加參數(shù)就是發(fā)送這個信號
對進程的影響
sigint 信號被當前進程樹接收到,也就是說,不僅當前進程會收到信號,它的子進程也會收到
sigterm只有當前進程收到信號,子進程不會收到。如果當前進程被kill了,那么它的子進程的父進程將會是init,也就是pid為1的進程
上述信號在發(fā)出后
不再接受新的連接請求
等待現(xiàn)有的連接處理完畢
關(guān)閉所有打開的連接
將內(nèi)存的數(shù)據(jù)寫出到磁盤
安全停止
2、使用系統(tǒng)服務(wù)腳本方式停止mongod
# ps -ef|grep mongod
root 5675 1 3 19:33 ? 00:00:00 /var/lib/mongodb/bin/mongod --config=/var/lib/mongodb/conf/rs2.conf
root 5689 4950 0 19:33 pts/3 00:00:00 grep mongod
[root@node3 conf]#
[root@node3 conf]# service mongod stop
Stopping mongodb: [ OK ]
3、db.shutdownServer()方式
# mongo localhost:27000
> use admin
> db.shutdownServer()
4、使用命令行方式關(guān)閉(補充@20160901)
# mongod -f /etc/mongo-m.conf --shutdown
5、強制關(guān)閉mongod
# kill -9 5675
缺點:
數(shù)據(jù)庫直接關(guān)閉
數(shù)據(jù)丟失
數(shù)據(jù)文件容易損壞(需要進行修復)
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。