溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Mongodb的啟動(dòng)和停止

發(fā)布時(shí)間:2020-07-29 12:45:31 來(lái)源:網(wǎng)絡(luò) 閱讀:1152 作者:gfsunny 欄目:MongoDB數(shù)據(jù)庫(kù)

1、Mongod的啟動(dòng)

1.1、Mongod的啟動(dòng)選項(xiàng)

    Mongod有許多可配置的選項(xiàng),在命令行運(yùn)行mongod --help可以查看所有選項(xiàng),常用的選項(xiàng)如下:


序號(hào)選項(xiàng)含義
1--dbpath指定數(shù)據(jù)目錄,默認(rèn)值是/data/db(Windows下是C:\data\db)。每個(gè)mongod進(jìn)程都需要獨(dú)立的數(shù)據(jù)目錄,所以要是有3個(gè)mongod的實(shí)例,必須要有獨(dú)立的數(shù)據(jù)目錄。當(dāng)mongod啟動(dòng)時(shí),會(huì)在數(shù)據(jù)目錄中創(chuàng)建mongod.lock文件,這個(gè)文件用于防止其他mongod進(jìn)程使用該數(shù)據(jù)目錄,其文件內(nèi)容為mongod線程的pid號(hào)。
2--port指定服務(wù)器監(jiān)聽的端口號(hào),默認(rèn)的端口號(hào)是27017,是個(gè)其他進(jìn)程不怎么用的端口,要是運(yùn)行多個(gè)mongod的進(jìn)程,則要給每個(gè)指定不同的端口號(hào)
3--fork以守護(hù)進(jìn)程的方式運(yùn)行Mongod,創(chuàng)建服務(wù)器進(jìn)程
4--logpath指定日志輸出路徑,而不是輸出到命令行,如果對(duì)文件夾有寫權(quán)限的話,系統(tǒng)會(huì)在文件不存在時(shí)創(chuàng)建它。它將覆蓋已有文件,清除所有原來(lái)的日志記錄,如果想保留原來(lái)的日志,還需使用--logappend選項(xiàng)。
5--config指定配置文件,加載命令行未指定的各種選項(xiàng)。
6--httpinterface啟用http接口



    示例1:查看進(jìn)程

[root@gflinux102 data]# ps -ef|grep -v grep |grep mongod

root      3620  2132  0 14:05 pts/1    00:00:00 mongod --port 10001 --dbpath /opt/mongo/data/ --logpath /opt/mongo/logs/mongodb.log

[root@gflinux102 data]# cat mongod.lock 

3620

[root@gflinux102 data]#

    示例二:查看端口號(hào)

[root@gflinux102 data]# netstat -ntlp|grep 27017

[root@gflinux102 data]# netstat -ntlp|grep 10001

tcp        0      0 0.0.0.0:10001               0.0.0.0:*                   LISTEN      3620/mongod         

[root@gflinux102 data]# 

root@gflinux102 logs]# more mongodb.log 

2015-02-10T14:05:14.531+0800 [initandlisten] MongoDB starting : pid=3620 port=10001 dbpath=/opt/mongo/data/ 32-bit host=gflinux102

2015-02-10T14:05:14.531+0800 [initandlisten] 

2015-02-10T14:05:14.531+0800 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.

2015-02-10T14:05:14.531+0800 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).

2015-02-10T14:05:14.531+0800 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.

2015-02-10T14:05:14.531+0800 [initandlisten] **       See http://dochub.mongodb.org/core/32bit

    啟動(dòng)示例:

[root@gflinux102 bin]# mongod --port 10001 --dbpath /opt/mongo/data/ --logpath /opt/mongo/logs/mongodb.log

2015-02-10T14:05:14.516+0800 

2015-02-10T14:05:14.517+0800 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.

2015-02-10T14:05:14.517+0800 

    在32bit下,mongod只能處理2Gb的數(shù)據(jù),注意生產(chǎn)中要使用64bit的機(jī)器。

1.2MongoDB的配置文件

    MongoDB支持從文件獲取配置信息。當(dāng)需要的配置非常多或者要自動(dòng)化運(yùn)維時(shí),就會(huì)用到這個(gè),指定配置文件可以用-f或者--config選項(xiàng)。

[root@gflinux102 logs]# mongod --help|grep "  -f"

  -f [ --config ] arg         configuration file specifying additional options

[root@gflinux102 logs]# 

    示例:

mongod --config ~/.mongodb.conf

    配置文件模板如下,注意這個(gè)是手工編輯的:

[root@gflinux102 bin]# mongod -f /opt/mongo/data/mongod.conf

2015-02-10T15:06:28.199+0800 

2015-02-10T15:06:28.200+0800 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.

2015-02-10T15:06:28.200+0800 

about to fork child process, waiting until server is ready for connections.

forked process: 3854

child process started successfully, parent exiting

[root@gflinux102 data]# vi mongod.conf

# Start MongoDB as a daemon on port 10001

port = 10001

fork = true

logappend = true

dbpath = /opt/mongo/data

logpath = /opt/mongo/logs/mongodb.log

   注意:命令行中哪些如--fork的開關(guān)選項(xiàng),其值要設(shè)為true。

1.3、停止MongoDB

1.3.1前臺(tái)進(jìn)程運(yùn)行在中斷

    如果服務(wù)器進(jìn)程作為前臺(tái)進(jìn)程運(yùn)行在終端,直接CTL-C。

1.3.2kill殺死

[root@gflinux102 bin]# ps -ef|grep -v grep |grep mongod

root      3854     1  0 15:06 ?        00:00:00 mongod -f /opt/mongo/data/mongod.conf

或者這樣查看pid:

[root@gflinux102 bin]# cat /opt/mongo/data/mongod.lock

3854

    殺死進(jìn)程:

[root@gflinux102 bin]# kill `cat /opt/mongo/data/mongod.lock` (SIGTERM)

[root@gflinux102 bin]# kill -2 `cat /opt/mongo/data/mongod.lock` (SIGINT)

    當(dāng)mongod收到SIGINT或者SIGTERM時(shí),會(huì)穩(wěn)妥退出,即會(huì)等到當(dāng)前運(yùn)行的操作或者文件預(yù)分配完成(需要一些時(shí)間),關(guān)閉所有打開的連接,將緩存的數(shù)據(jù)刷新到磁盤,最后停止。

    【禁止】:千萬(wàn)不要向運(yùn)行中的mongodb發(fā)送SIGKILL(kill -9),這樣會(huì)導(dǎo)致數(shù)據(jù)庫(kù)直接關(guān)閉,可能會(huì)使數(shù)據(jù)文件損壞。

1.3.3使用shutdown命令

    使用shutdown命令,{"shutdown":1}。這要在admin數(shù)據(jù)庫(kù)下使用,shell提供了輔助函數(shù),來(lái)簡(jiǎn)化這一過(guò)程。

[root@gflinux102 bin]# mongo localhost:10001

MongoDB shell version: 2.6.6

connecting to: localhost:10001/test

Server has startup warnings: 

2015-02-10T15:37:43.973+0800 [initandlisten] 

2015-02-10T15:37:43.973+0800 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.

2015-02-10T15:37:43.973+0800 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).

2015-02-10T15:37:43.973+0800 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.

2015-02-10T15:37:43.973+0800 [initandlisten] **       See http://dochub.mongodb.org/core/32bit

2015-02-10T15:37:43.973+0800 [initandlisten] 

> show dbs

admin  (empty)

local  0.078GB

> use admin

switched to db admin

> db.shutdownServer()

2015-02-10T15:39:04.616+0800 DBClientCursor::init call() failed

server should be down...

2015-02-10T15:39:04.624+0800 trying reconnect to localhost:10001 (127.0.0.1) failed

2015-02-10T15:39:04.626+0800 warning: Failed to connect to 127.0.0.1:10001, reason: errno:111 Connection refused

2015-02-10T15:39:04.627+0800 reconnect localhost:10001 (127.0.0.1) failed failed couldn't connect to server localhost:10001 (127.0.0.1), connection attempt failed


    

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI