MonfoDB是開來源文件資料庫,提供共效能、高可用、自動擴充等
MongoDB中記錄是文檔,其是欄位和值組成的對結構。mongodb文檔類似JSON對象,欄位的值可以包含其它文檔、數組、文檔的數組。記錄組織成collection,相當於表。參考下圖:
使用文檔的優點是:
文檔對應很多程式設計語言的內生資料對象
內嵌文檔和數組減少了join的開銷
動態schema支援順暢多態
關鍵功能:
高效能:
mongodb提供高效能資料持久。特別是:
支援內嵌資料模型減少了資料庫系統的I/O
索引支援快速查詢且內嵌文檔和數組可以包含鍵
高可用:
mongodb提供高可用的是replica sets
自動失敗切換
資料冗餘
replica set是一組mongodb伺服器,它們維護這同一個資料集,提供冗餘,增加資料可用性。
自動水平擴充:
水平擴充是mongodb的核心功能。
在叢集上自動共用分布式資料
replica sets能為低延遲,高輸送量的應用提供最終一致性讀。
安裝:
0
環境
[root@host2 ~]# uname -a
Linux host2 2.6.32-504.3.3.el6.x86_64 #1 SMP Wed Dec 17 01:55:02 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@host2 ~]# cat /etc/issue
CentOS release 6.5 (Final)
Kernel \r on an \m
1
下載:
curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.6.7.tgz
2
[root@mgdbsvr mongodb]# tar -zxvf mongodb-linux-x86_64-2.6.7.tgz
mongodb-linux-x86_64-2.6.7/README
mongodb-linux-x86_64-2.6.7/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-2.6.7/GNU-AGPL-3.0
mongodb-linux-x86_64-2.6.7/bin/mongodump
mongodb-linux-x86_64-2.6.7/bin/mongorestore
mongodb-linux-x86_64-2.6.7/bin/mongoexport
mongodb-linux-x86_64-2.6.7/bin/mongoimport
mongodb-linux-x86_64-2.6.7/bin/mongostat
mongodb-linux-x86_64-2.6.7/bin/mongotop
mongodb-linux-x86_64-2.6.7/bin/mongooplog
mongodb-linux-x86_64-2.6.7/bin/mongofiles
mongodb-linux-x86_64-2.6.7/bin/bsondump
mongodb-linux-x86_64-2.6.7/bin/mongoperf
mongodb-linux-x86_64-2.6.7/bin/mongod
mongodb-linux-x86_64-2.6.7/bin/mongos
mongodb-linux-x86_64-2.6.7/bin/mongo
[root@mgdbsvr mongodb]#
3
拷貝的規劃的檔案夾
[root@mgdbsvr mongodb]# mv mongodb-linux-x86_64-2.6.7 /opt/mongodb
4
配置環境變數
[root@mgdbsvr ~]# vi .bash_profile
PATH=$PATH:/opt/mongodb/bin
5
資料所在檔案夾
[root@mgdbsvr mongodb]# mkdir dbdata
6
使使用者 mongod可以讀寫資料所在檔案夾
[root@mgdbsvr mongodb]# useradd -r mongod
[root@mgdbsvr mongodb]# chown mongod:mongod dbdata/
7
啟動
[root@mgdbsvr mongodb]# mongod --dbpath dbdata
2015-02-13T17:56:23.704+0800 [initandlisten] MongoDB starting : pid=2035 port=27017 dbpath=dbdata 64-bit host=mgdbsvr
2015-02-13T17:56:23.708+0800 [initandlisten] db version v2.6.7
2015-02-13T17:56:23.708+0800 [initandlisten] git version: a7d57ad27c382de82e9cb93bf983a80fd9ac9899
2015-02-13T17:56:23.708+0800 [initandlisten] build info: Linux build7.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-02-13T17:56:23.708+0800 [initandlisten] allocator: tcmalloc
2015-02-13T17:56:23.708+0800 [initandlisten] options: { storage: { dbPath: "dbdata" } }
2015-02-13T17:56:23.738+0800 [initandlisten] journal dir=dbdata/journal
2015-02-13T17:56:23.741+0800 [initandlisten] recover : no journal files present, no recovery needed
2015-02-13T17:56:24.520+0800 [initandlisten] preallocateIsFaster=true 13.46
2015-02-13T17:56:25.273+0800 [initandlisten] preallocateIsFaster=true 12.5
2015-02-13T17:56:27.166+0800 [initandlisten] preallocateIsFaster=true 8.76
2015-02-13T17:56:27.166+0800 [initandlisten] preallocateIsFaster check took 3.423 secs
2015-02-13T17:56:27.167+0800 [initandlisten] preallocating a journal file dbdata/journal/prealloc.0
2015-02-13T17:56:30.089+0800 [initandlisten] File Preallocator Progress: 891289600/1073741824 83%
2015-02-13T17:56:31.216+0800 [initandlisten] preallocating a journal file dbdata/journal/prealloc.1
2015-02-13T17:56:34.070+0800 [initandlisten] File Preallocator Progress: 629145600/1073741824 58%
2015-02-13T17:56:35.779+0800 [initandlisten] preallocating a journal file dbdata/journal/prealloc.2
2015-02-13T17:56:38.054+0800 [initandlisten] File Preallocator Progress: 859832320/1073741824 80%
2015-02-13T17:56:39.630+0800 [initandlisten] allocating new ns file dbdata/local.ns, filling with zeroes...
2015-02-13T17:56:39.762+0800 [FileAllocator] allocating new datafile dbdata/local.0, filling with zeroes...
2015-02-13T17:56:39.763+0800 [FileAllocator] creating directory dbdata/_tmp
2015-02-13T17:56:39.797+0800 [FileAllocator] done allocating datafile dbdata/local.0, size: 64MB, took 0.002 secs
2015-02-13T17:56:39.832+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2015-02-13T17:56:39.855+0800 [initandlisten] added index to empty collection
2015-02-13T17:56:39.894+0800 [initandlisten] command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYields:0 reslen:37 281ms
2015-02-13T17:56:39.897+0800 [initandlisten] waiting for connections on port 27017
8
串連,簡單使用
[root@mgdbsvr ~]# mongo
MongoDB shell version: 2.6.7
connecting to: test
#查看當前資料庫
> db
test
#切到資料庫 mydb
> use mydb
switched to db mydb
> db
mydb
#用javascript建立兩個文檔
> j = { name : "mongo" }
{ "name" : "mongo" }
> k = { x : 3 }
{ "x" : 3 }
#建立mydb資料庫和testdata collection
> db.testdata.insert(j)
WriteResult({ "nInserted" : 1 })
> db.testdata.insert(k);
WriteResult({ "nInserted" : 1 })
> show collections
system.indexes
testdata
> db.testdata.find()
{ "_id" : ObjectId("54ddd717d692cfe0bd20d983"), "name" : "mongo" }
{ "_id" : ObjectId("54ddd728d692cfe0bd20d984"), "x" : 3 }
>
>
> show dbs
admin (empty)
local 0.078GB
mydb 0.078GB
>
> use hahadb
switched to db hahadb
> show dbs
admin (empty)
local 0.078GB
mydb 0.078GB
> db
hahadb
> j = { name : "mongo" }
{ "name" : "mongo" }
> show dbs
admin (empty)
local 0.078GB
mydb 0.078GB
> db.testdata.insert(j)
WriteResult({ "nInserted" : 1 })
> show dbs
admin (empty)
hahadb 0.078GB
local 0.078GB
mydb 0.078GB
>
> use mydb
switched to db mydb
> show collections
system.indexes
testdata
>
> db.testdata.insert(j)
WriteResult({ "nInserted" : 1 })
> db.testdata.find()
{ "_id" : ObjectId("54ddd717d692cfe0bd20d983"), "name" : "mongo" }
{ "_id" : ObjectId("54ddd728d692cfe0bd20d984"), "x" : 3 }
{ "_id" : ObjectId("54ddda64d692cfe0bd20d986"), "name" : "mongo" }
> db.testdata.insert(k)
WriteResult({ "nInserted" : 1 })
>
>
> db.testdata.find()
{ "_id" : ObjectId("54ddd717d692cfe0bd20d983"), "name" : "mongo" }
{ "_id" : ObjectId("54ddd728d692cfe0bd20d984"), "x" : 3 }
{ "_id" : ObjectId("54ddda64d692cfe0bd20d986"), "name" : "mongo" }
{ "_id" : ObjectId("54ddda7dd692cfe0bd20d987"), "x" : 3 }
>
> db.testdata.find({x:3})
{ "_id" : ObjectId("54ddd728d692cfe0bd20d984"), "x" : 3 }
{ "_id" : ObjectId("54ddda7dd692cfe0bd20d987"), "x" : 3 }
>
>
> exit;
[root@mgdbsvr ~]#
參考:
docs.mongodb.org/manual/tutorial/install-mongodb-on-linux/
-----------------
轉載請著明出處:
blog.csdn.net/beiigang