2011年6月21日 星期二

大芒果資料庫 - MongoDB

MongoDb 是一套用 C++ 寫成的,高擴充性,高效能,文件導向 (JSON) 的資料庫系統,而且它還是 開放源軟體 (Open Source) 

基本上它支援多數的作業系統:OSX / Linux / Windows / Solaris 等等, 而且也提供了多種程式語言的 連接驅動程式 :Java / Python / PHP / Ruby / Perl / C++ 等等

MongoDB 中的 mongo 是由 "humongous" 這單字而來, 代表它是能處理巨大資訊, 之所以稱它為芒果, 完全是根據它的讀音

不需安裝, 立即使用 (Ubuntu 為例)

1. 下載 MongoDB 壓縮檔,然後解壓縮至家目錄
網址 : http://www.mongodb.org/downloads

2.先建立資料庫存放目錄,預設會在 /data/db 底下
$ sudo mkdir -p /data/db

3. 下載後解壓縮
$ wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.2.tgz
$ tar xvfz mongodb-linux-i686-1.8.2.tgz
mongodb-linux-i686-1.8.2/
mongodb-linux-i686-1.8.2/THIRD-PARTY-NOTICES
mongodb-linux-i686-1.8.2/GNU-AGPL-3.0
mongodb-linux-i686-1.8.2/README
mongodb-linux-i686-1.8.2/bin/
mongodb-linux-i686-1.8.2/bin/mongofiles
mongodb-linux-i686-1.8.2/bin/mongostat
mongodb-linux-i686-1.8.2/bin/bsondump
mongodb-linux-i686-1.8.2/bin/mongos
mongodb-linux-i686-1.8.2/bin/mongodump
mongodb-linux-i686-1.8.2/bin/mongoimport
mongodb-linux-i686-1.8.2/bin/mongosniff
mongodb-linux-i686-1.8.2/bin/mongo
mongodb-linux-i686-1.8.2/bin/mongod
mongodb-linux-i686-1.8.2/bin/mongoexport
mongodb-linux-i686-1.8.2/bin/mongorestore

4. 啟動 mongodb 服務
$ cd mongodb-linux-i686-1.8.2/
$ cd bin
$ sudo ./mongod -dbpath /data/db &

[註] MongoDB 啟動之後預設會佔用兩個 TCP Port, 如下 :

  27017 ﹣用來等待連線
  28017 ﹣提供 web admin interface(可以用瀏覽器來看MongoDB目前的狀態)

5. 執行 MongDB Shell
$ ./mongo
MongoDB shell version: 1.8.2
connecting to: test
Wed Jun 22 12:18:56 [initandlisten] connection accepted from 127.0.0.1:60485 #1
> help
    db.help()                    help on db methods
    db.mycoll.help()             help on collection methods
    rs.help()                    help on replica set methods
    help connect                 connecting to a db help
    help admin                   administrative help
    help misc                    misc things to know
    help mr                      mapreduce help

    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
    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
>

6.停止 mongodb 服務
> use admin
switched to db admin
> db.shutdownServer()
Tue Jun 28 14:23:31 [conn2] terminating, shutdown command received
Tue Jun 28 14:23:31 dbexit: shutdown called
Tue Jun 28 14:23:31 [conn2] shutdown: going to close listening sockets...
Tue Jun 28 14:23:31 [conn2] closing listening socket: 5
Tue Jun 28 14:23:31 [conn2] closing listening socket: 6
Tue Jun 28 14:23:31 [conn2] closing listening socket: 7
Tue Jun 28 14:23:31 [conn2] closing listening socket: 8

MongoDB官網 : http://www.mongodb.org/

No Response to "大芒果資料庫 - MongoDB"

張貼留言