mongoDB 啟動與停止

來源:互聯網
上載者:User

標籤:var   子進程   hup   serve   ppi   mongodb   檔案的   removing   about   

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 &


預設連接埠為

[[email protected] 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

        [[email protected] conf]# 

        [[email protected] 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 啟動與停止

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.