標籤: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:查看進程
[[email protected] 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
[[email protected] data]# cat mongod.lock
3620
[[email protected] data]#
樣本二:查看連接埠號碼
[[email protected] data]# netstat -ntlp|grep 27017
[[email protected] data]# netstat -ntlp|grep 10001
tcp 0 0 0.0.0.0:10001 0.0.0.0:* LISTEN 3620/mongod
[[email protected] data]#
[email protected] 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
啟動樣本:
[[email protected] 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選項。
[[email protected] logs]# mongod --help|grep " -f"
-f [ --config ] arg configuration file specifying additional options
[[email protected] logs]#
樣本:
mongod --config ~/.mongodb.conf
設定檔範本如下,注意這個是手工編輯的:
[[email protected] 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
[[email protected] 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殺死
[[email protected] 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:
[[email protected] bin]# cat /opt/mongo/data/mongod.lock
3854
殺死進程:
[[email protected] bin]# kill `cat /opt/mongo/data/mongod.lock` (SIGTERM)
[[email protected] 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提供了輔助函數,來簡化這一過程。
[[email protected] 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
Mongodb的啟動和停止