Mongodb的啓動和停止

1、Mongod的啓動

1.1、Mongod的啓動選項

    Mongod有許多可配置的選項,在命令行運行mongod --help可以查看所有選項,常用的選項如下:


序號選項含義
1--dbpath指定數據目錄,默認值是/data/db(Windows下是C:\data\db)。每個mongod進程都需要獨立的數據目錄,所以要是有3個mongod的實例,必須要有獨立的數據目錄。當mongod啓動時,會在數據目錄中創建mongod.lock文件,這個文件用於防止其他mongod進程使用該數據目錄,其文件內容爲mongod線程的pid號。
2--port指定服務器監聽的端口號,默認的端口號是27017,是個其他進程不怎麼用的端口,要是運行多個mongod的進程,則要給每個指定不同的端口號
3--fork以守護進程的方式運行Mongod,創建服務器進程
4--logpath指定日誌輸出路徑,而不是輸出到命令行,如果對文件夾有寫權限的話,系統會在文件不存在時創建它。它將覆蓋已有文件,清除所有原來的日誌記錄,如果想保留原來的日誌,還需使用--logappend選項。
5--config指定配置文件,加載命令行未指定的各種選項。
6--httpinterface啓用http接口



    示例1:查看進程

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

    示例二:查看端口號

[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

    啓動示例:

[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的數據,注意生產中要使用64bit的機器。

1.2MongoDB的配置文件

    MongoDB支持從文件獲取配置信息。當需要的配置非常多或者要自動化運維時,就會用到這個,指定配置文件可以用-f或者--config選項。

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

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

[root@gflinux102 logs]# 

    示例:

mongod --config ~/.mongodb.conf

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

[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的開關選項,其值要設爲true。

1.3、停止MongoDB

1.3.1前臺進程運行在中斷

    如果服務器進程作爲前臺進程運行在終端,直接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

    殺死進程:

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

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

    當mongod收到SIGINT或者SIGTERM時,會穩妥退出,即會等到當前運行的操作或者文件預分配完成(需要一些時間),關閉所有打開的連接,將緩存的數據刷新到磁盤,最後停止。

    【禁止】:千萬不要向運行中的mongodb發送SIGKILL(kill -9),這樣會導致數據庫直接關閉,可能會使數據文件損壞。

1.3.3使用shutdown命令

    使用shutdown命令,{"shutdown":1}。這要在admin數據庫下使用,shell提供了輔助函數,來簡化這一過程。

[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


    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章