一、下載完安裝包,並解壓 tgz(以下演示的是 64 位 Linux上的安裝) 。
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 # 將解壓包拷貝到指定目錄
二、MongoDB 的可執行文件位於 bin 目錄下,所以可以將其添加到 PATH 路徑中:
export PATH=<mongodb-install-directory>/bin:$PATH
<mongodb-install-directory> 為 MongoDB 的安裝路徑。如本文的 /usr/local/mongodb 。
三、創建數據庫目錄
MongoDB的數據存儲在data目錄的db目錄下,但是這個目錄在安裝過程不會自動創建,所以你需要手動創建data目錄,並在data目錄中創建db目錄。以下實例中我們將data目錄創建於根目錄下(/)。
注意:/data/db 是 MongoDB 默認的啟動的數據庫路徑(--dbpath)。
mkdir -p /data/db
四、命令行中運行 MongoDB 服務
①可以在 /usr/local/mongodb/bin/ 目錄下使用命令:
./mongod
②在 /usr/local/mongodb/ 下添加conf目錄,並添加mongodb.conf配置文件。
./mongod -f /usr/local/mongodb/conf/mongodb.conf 或者
./mongod --config /usr/local/mongodb/conf/mongodb.conf
mongodb.conf配置文件內容如下:
dbpath=/data/db
bind_ip=127.0.0.1
port=27017
fork=true
logappend=true
shardsvr=true
pidfilepath=/usr/local/mongodb/mongo.pid
logpath=/usr/local/mongodb/log/mongodb.log
五、設置開機啟動mongo
方法一、①在/etc/init.d/目錄下添加mongod腳本,
②添加腳本執行權限 chmod +x /etc/init.d/mongod
③設置開機啟動 chkconfig mongod on
mongo腳本內容如下:
#!/bin/bash
#chkconfig: 2345 80 90
#description: mongodb
start() {
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
}
stop() {
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
方法二、將 /usr/local/mongodb/bin/mongod
--dbpath /data/db
--fork
--port 27017
--logpath=/usr/local/mongodb/log/mongodb.log
--logappend
添加到 /etc/rc.local 中。