centos安裝MongoDB,並設定開機自啟動
阿新 • • 發佈:2018-12-24
1:安裝
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz
mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb
vim /etc/profile(新增如下圖環境變數)
source /etc/profile(使環境變數生效)
2:mongodb啟動
cd /usr/local/momgodb/bin ./mongod
mongodb客戶端連線
cd /usr/local/momgodb/bin ./mongo
3:以守護程序方式啟動
cd /usr/local/mongodb touch log
vi mongodb.conf(建立配置檔案)
chmod –R a+x ./mongodb.conf
kill掉當前MongoDB程序(以防埠被佔用,出現以下錯誤)
cd /us r/local/mongodb/bin/
./mongod –f mongodb.conf(如果不是同一目錄,輸入剛剛建立的配置檔案的絕對位置)
出現以下結果 表示配置檔案啟動成功
cd /etc/init.d
vi mongodb(建立啟動檔案,內容如下,確保下面啟動和關閉的命令可以正常執行)
#!/bin/sh
#
#chkconfig: 2345 80 90
#description: mongodb
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
start() {
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf
}
stop() {
/usr/local/mongodb/bin/mongod -f /usr/local/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 –R a+x mongodb
chkconfig --add mongodb
chkconfig mongodb on
mongodb start(啟動之前確保已經kill 掉mongodb,出現如下,表明啟動成功)