阿里雲centos服務器安裝mongodb數據庫,開機啓動
##1.安裝:
- 從mongodb官方網站下載二進制安裝包(本文以mongodb-linux-x86_64-rhel62-3.4.5爲例進行步驟講解).
- 將下載下來的文件放到/opt文件夾中,解壓文件
tar zxvf mongodb-linux-x86_64-rhel62-3.4.5.tgz
mv mongodb-linux-x86_64-rhel62-3.4.5 mongodb
- 切到mongodb的目錄下,進行如下操作:
cd mongodb
mkdir -p data/db
mkdir logs
cd bin/
touch mongodb.conf
並添加如下內容:
dbpath=/opt/mongodb/data/db
logpath=/opt/mongodb/logs/mongodb.log
pidfilepath=/opt/mongodb/db.pid
directoryperdb=true
logappend=true
bind_ip= localhost
port=27017
oplogSize=1000
fork=true
noprealloc=true
nojournal=true
smallfiles=true
- 編輯~/.bash_profile:
將mongodb的bin路徑添加到系統路徑下
export PATH=/opt/mongodb/bin:$PATH
並執行source ~/.bash_profile,生效當前設置
到目前爲止,直接輸入如下命令:
mongod --config /opt/mongodb/bin/mongodb.conf
可以看到mongodb正常啓動
##2.配置開機自動啓動:
- 先在/etc/rc.d/init.d下新建文件 mongod,內容如下:
#!/bin/bash
#
#chkconfig: 2345 80 90
#description: mongod
start() {
mongod --config /opt/mongodb/bin/mongodb.conf
}
stop() {
mongod --config /opt/mongodb/bin/mongodb.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo
$"Usage: $0 {start|stop|restart}"
exit 1
esac
- 增加服務並開機啓動
chmod +x /etc/rc.d/init.d/mongod
chkconfig --add mongod
chkconfig --level 345 mongod on
chkconfig --list mongod
service mongod start
執行該腳本後,就可以開始使用service mongod start|stop|restart 管理你的mongodb服務了。