1. 程式人生 > >mongoDB 啟動與停止

mongoDB 啟動與停止

參考 kconfig 1.2 cleanup socket conn parent 方案 ons

MongoDB是一個基於分布式文件存儲的數據庫。由C++語言編寫。旨在為WEB應用提供可擴展的高性能數據存儲解決方案。它以高性能、易部署、易使用,存儲數據非常方便等優點被廣泛使用。其安裝配置相當簡單,有如輕量級的mysql,但功能絲毫不差。本文主要描述mongoDB的啟動與停止。

一、準備環境


1、當前環境

# more /etc/redhat-release

CentOS release 6.7 (Final)


2、安裝mongod

# which mongod

/var/lib/mongodb/bin/mongod


參考:

Linux下快速安裝MongoDB

Windows平臺下安裝MongoDB


3、創建數據目錄

# 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、使用系統服務的方式啟動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進程發送信號

###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


信號 產生方式

sigint 通過ctrl+c將會對當進程發送此信號

sigterm kill命令不加參數就是發送這個信號


對進程的影響

sigint 信號被當前進程樹接收到,也就是說,不僅當前進程會收到信號,它的子進程也會收到

sigterm只有當前進程收到信號,子進程不會收到。如果當前進程被kill了,那麽它的子進程的父進程將會是init,也就是pid為1的進程


上述信號在發出後

不再接受新的連接請求

等待現有的連接處理完畢

關閉所有打開的連接

將內存的數據寫出到磁盤

安全停止


2、使用系統服務腳本方式停止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、使用命令行方式關閉(補充@20160901)

# mongod -f /etc/mongo-m.conf --shutdown


5、強制關閉mongod

# kill -9 5675

缺點:

數據庫直接關閉

數據丟失

數據文件容易損壞(需要進行修復)


mongoDB 啟動與停止