1. 編輯mongodb的配置文件:
修改/opt/mongo/bin/mongodb.conf配置文件,在/etc/init.d中編寫自啟動(dòng)腳本時(shí),需要讓mongodb按照配置文件來(lái)啟動(dòng)和關(guān)閉:
/*數(shù)據(jù)存放目錄*/
dbpath=/opt/mongo/data/db
/*指定日志文件,該文件將保存所有的日志記錄、診斷信息*/
logpath=/opt/mongo/log/mongodb.log
/*進(jìn)程ID,沒有指定則啟動(dòng)時(shí)候就沒有PID文件*/
pidfilepath=/opt/mongo/db.pid
//設(shè)置為true,修改數(shù)據(jù)目錄存儲(chǔ)模式,每個(gè)數(shù)據(jù)庫(kù)的文件存儲(chǔ)在DBPATH指定目錄的不同的文件夾中。
//使用此選項(xiàng),可以配置的MongoDB將數(shù)據(jù)存儲(chǔ)在不同的磁盤設(shè)備上,以提高寫入吞吐量或磁盤容量。
directoryperdb=true
//寫日志的模式:設(shè)置為true為追加。默認(rèn)是覆蓋。如果未指定此設(shè)置,啟動(dòng)時(shí)MongoDB的將覆蓋現(xiàn)有的日志文件。
logappend=true
//綁定地址
bind_ip= localhost
/*端口。默認(rèn)27017*/
port=27017
//是否后臺(tái)運(yùn)行,設(shè)置為true 啟動(dòng)進(jìn)程在后臺(tái)運(yùn)行的守護(hù)進(jìn)程模式。
fork=true
//來(lái)禁用預(yù)分配的數(shù)據(jù)文件,會(huì)縮短啟動(dòng)時(shí)間,但在正常操作過(guò)程中,可能會(huì)導(dǎo)致性能顯著下降。
noprealloc=true
//設(shè)置為true,使用較小的默認(rèn)數(shù)據(jù)文件大小。smallfiles減少數(shù)據(jù)文件的初始大小,并限制他們到512M,也減少了日志文件的大小,
//并限制他們到128M。如果數(shù)據(jù)庫(kù)很大,各持有少量的數(shù)據(jù),會(huì)導(dǎo)致mongodb創(chuàng)建很多文件,會(huì)影響性能。
smallfiles=true
2. 創(chuàng)建mongodb服務(wù)腳本
文件夾/etc/init.d/是用來(lái)放服務(wù)腳本的,當(dāng)Linux啟動(dòng)時(shí),會(huì)尋找這些目錄中的服務(wù)腳本,并根據(jù)腳本的run level確定不同的啟動(dòng)級(jí)別。
- 在
/etc/init.d/中創(chuàng)建mongodb服務(wù)腳本文件,編輯內(nèi)容如下:
#!/bin/bash
#
#chkconfig: 2345 80 90
#description: mongodb
start() {
/opt/mongo/bin/mongod --config /opt/mongo/bin/mongodb.conf
}
stop() {
/opt/mongo/bin/mongod --config /opt/mongo/bin/mongodb.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo
$"Usage: $0 {start|stop|restart}"
exit 1
esac
上面腳本,是讓mongodb腳本服務(wù),可以通過(guò)service mongodb start來(lái)實(shí)現(xiàn)mongod服務(wù)按照配置文件啟動(dòng);通過(guò)service mongodb stop來(lái)實(shí)現(xiàn)mongod服務(wù)按照配置文件停止;還有service mongodb restart實(shí)現(xiàn)mongod服務(wù)重啟。
- 增加服務(wù)并開機(jī)啟動(dòng):
chmod +x /etc/init.d/mongodb
chkconfig --add mongodb
chkconfig --level 345 mongodb on
chkconfig --list mongodb
service mongodb start
以后啟動(dòng)和重啟mongdb服務(wù),都是通Linux守護(hù)進(jìn)程開啟的,關(guān)閉命令窗口也不會(huì)停止服務(wù)。