MongoDB 安裝
  • 5,231 views,
  • 2013-09-19,
  • 上傳者: Kuann Hung,
  •  0
8c78477df6e48b782b665dccaa968ed6.png
最近新的專案要使用 MongoDB ,所以開始來熟悉一下了。
第一步就是要先安裝起來,以下為安裝步驟:
 
步驟
1.
下載 MongoDB
在這個網址中提供各個不同版本的 MongoDB,因為現階段還在本機開發,所以我使用 Windows 的版本,方便測試。不過設定上對 Linux 來說也是大同小異的。
http://http://www.mongodb.org/downloads
 
2.
解開 MongoDB
下載好後就直接把他解開,比如放到 C:\MongoDB 下面。
3.
建立資料目錄
預設的安裝檔案中並沒有預設存放資料的目錄,所以要自己建立(多建一個 Logs 存放 Log 檔):
cd \mongodb
mkdir data
mkdir logs
4.
編輯設定檔案 (mongo.conf)
為了方便將來設定,最好產生一個設定檔,讓服務去啟動該設定檔案,可命名為 mongo.conf
# mongo.conf
 
rest = true
nojournal = true
 
#where to log
logpath=C:\MongoDB\logs\mongo.log
logappend=true
 
#fork = true
port = 27017
 
dbpath=C:\MongoDB\data
 
#master=true
#auth = true
 
#verbose = true
replSet = your_replset
 
其中需要注意的幾個設定:
1. 在 windows 上不能設定 fork,否則會錯誤
2. port 預設是 27017,如果改掉的話,你的 AP 在存取時也需要調整。
3. logpath、dbpath 請根據你的環境作設定。
4. 預設是沒有安全性的,所以請設定好你的防火牆。
5. replSet 請自訂名稱,將來可用來作多機同步的。
 
5.
安裝服務
設定好後就是跟 Windows 註冊服務了,先進到 MongoDB 的目錄 ( c:\MongoDB\bin )

mongod -f C:\MongoDB\bin\mongo.conf --install

這邊要注意,-f 給的 Config 檔案位置要給絕對路徑,否則服務會無法啟動喔!
6.
啟動服務
設定無誤後,就直接執行以下指令啟動服務:

net start MongoDB
 
7.
測試及初始化
要確定有正常啟動服務的話,就執行 mongo,應該會進入 MongoDB 的 console 畫面:
C:\MongoDB\bin>mongo
MongoDB shell version: 2.0.4
connecting to: test
> help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        rs.help()                    help on replica set methods
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce
 
        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell
第一次進來後,要先初始化 MongoDB,可能會跑一下,要耐心等候!
C:\MongoDB\bin>mongo
MongoDB shell version: 2.0.4
connecting to: test
> rs.initiate();
{
        "info2" : "no configuration explicitly specified -- making one",
        "me" : "YOUR_COMPUTER_NAME",
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
PRIMARY>

最後看到出現 PRIMARY> 就成功囉!接下來就可以使用 MongoDB 了!
 
Facebook 討論區載入中...
資料夾 :
發表時間 :
2013-09-19 02:55:00
觀看數 :
5,231
發表人 :
Kuann Hung
部門 :
老洪的 IT 學習系統
QR Code :