1.下載安裝linux
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgzmongodb
tar zxf mongodb-linux-x86_64-3.0.6.tgz 數據庫
mv mongodb-linux-x86_64-3.0.6 /usr/local/mongodb vim
2.建立數據目錄,必須建立,否則啓動失敗。服務器
mkdir -p /data/dbapp
建立配置文件,默認是沒有模板的須要本身建立:添加:(啓動參數均可以寫到配置文件中)spa
vim mongodb.conf 3d
#數據文件存放位置
dbpath=/data/dbrest
#日誌文件存放位置
logpath=/usr/local/mongodb/logs/mongodb.logs日誌
#PID的路徑
pidfilepath=/usr/local/mongodb/pid/mongodb.pid
#端口號
port=27017
#後臺運行
fork=true
#日誌輸出方式:追加
logappend=true
#打開28017網頁端口(若不開啓註釋掉便可)
rest=true
./mongod --config /usr/local/mongodb/bin/mongodb.conf
2017-06-11T16:20:40.499+0800 I CONTROL ** WARNING: --rest is specified without --httpinterface,
2017-06-11T16:20:40.499+0800 I CONTROL ** enabling http interface
about to fork child process, waiting until server is ready for connections.
forked process: 21179
child process started successfully, parent exiting(若啓動不成功查看日誌)
2017-06-11T00:12:09.875+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:54624 #13 (1 connection now open)
2017-06-11T00:12:09.880+0800 I ACCESS [conn13] Unauthorized not authorized on admin to execute command { getLog: "startupWarnings" }
2017-06-11T00:12:28.804+0800 I NETWORK [initandlisten] connection accepted from 10.10.2.13:63225 #14 (2 connections now open)
2017-06-11T00:12:28.805+0800 I NETWORK [initandlisten] connection accepted from 10.10.2.13:63227 #15 (3 connections now open)
2017-06-11T00:12:28.805+0800 I NETWORK [initandlisten] connection accepted from 10.10.2.13:63226 #16 (4 connections now open)
2017-06-11T00:12:28.808+0800 I NETWORK [conn14] end connection 10.10.2.13:63225 (3 connections now open)
2017-06-11T00:12:28.831+0800 I NETWORK [conn15] end connection 10.10.2.13:63227 (2 connections now open)
2017-06-11T00:12:29.717+0800 I NETWORK [conn16] end connection 10.10.2.13:63226 (1 connection now open)
(1 connection now open)#代表一個來自本機的鏈接
show dbs : 查看數據庫列表。數據庫爲空默認是不顯示的,要想顯示,須要把它插入至少一個文件
use 命令 :該命令將建立一個新的數據庫
dropDatabase() 方法 :
db.dropDatabase() 命令是用來刪除一個現有的數據庫。它將刪除選定的數據庫。
若是尚未選擇任何數據庫,而後它會刪除依次往下默認的 ' mogodb' 數據庫
若是刪除指定的數據庫,使用use進入數據庫中在執行db.dropDatabase()
4.製做啓動腳本
vim /etc/init.d/mongodb
#!/bin/sh
case $1 in
start)
/usr/local/mongodb/bin/mongod --maxConns 20000 --config /usr/local/mongodb/bin/mongodb.conf
;;
stop)
/usr/local/mongodb/bin/mongo 127.0.0.1:27017/admin --eval "db.shutdownServer()"
;;
status)
/usr/local/mongodb/bin/mongo 127.0.0.1:27017/admin --eval "db.stats()" #以管理員身份進入管理後臺
;;
*)
echo "start|stop|status"
;;
esac
chmod +x /etc/init.d/mongod
啓動開啓兩個端口,默認服務端是27017端口號,可使用默認端口號ip:28017進行用戶訪問:
6.MongoDB 備份(mongodump)與恢復(mongorestore)
備份:
mongodump -h IP --port 端口 -u 用戶名 -p 密碼 -d 數據庫 -o 文件存在路徑
-h:MongDB所在服務器地址,例如:127.0.0.1,固然也能夠指定端口號:127.0.0.1:27017
-d:須要備份的數據庫實例,例如:test
-o:備份的數據存放位置,例如:c:\data\dump,固然該目錄須要提早創建,在備份完成後,系統自動在dump目錄下創建一個test目錄,這個目錄裏面存放該數據庫實例的備份數據。
若是想導出全部數據庫,能夠去掉-d。
恢復:
mongorestore -h IP --port 端口 -u 用戶名 -p 密碼 -d 數據庫 --drop 文件存在路徑
--drop的意思是,先刪除全部的記錄,而後恢復。