1,MongoDB簡單介紹
1.1,NoSQL數(shù)據(jù)庫
數(shù)據(jù)庫:進(jìn)行高效的、有規(guī)則的進(jìn)行數(shù)據(jù)持久化存儲的軟件
NoSQL數(shù)據(jù)庫:Not only sql,指代非關(guān)系型數(shù)據(jù)庫
優(yōu)點:高可擴展性、分布式計算、低成本、靈活架構(gòu)、半結(jié)構(gòu)化數(shù)據(jù)、簡化關(guān)聯(lián)關(guān)系
缺點:沒有標(biāo)準(zhǔn)化、有限查詢、不直觀常用NoSQL數(shù)據(jù)庫
| 類型 | 常用NoSQL數(shù)據(jù)庫 | 典型代表 | 簡介 |
|---|---|---|---|
| 列存儲 | Hbase、Cassandra、Hypertable | Hbase | 顧名思義,是按列存儲數(shù)據(jù)的。最大的特點是方便存儲結(jié)構(gòu)化和半結(jié)構(gòu)化數(shù)據(jù),方便做數(shù)據(jù)壓縮,對針對某一列或者某幾列的查詢有非常大的IO優(yōu)勢。 |
| 文檔存儲 | MongoDB、CouchDB | MongoDB | 文檔存儲一般用類似json的格式存儲,存儲的內(nèi)容是文檔型的。這樣也就有有機會對某些字段建立索引,實現(xiàn)關(guān)系數(shù)據(jù)庫的某些功能。 |
| key-value存儲 | TokyoCabinet、BerkeleyDB、MemcacheDB、redis | Memcache Redis | 可以通過key快速查詢到其value。一般來說,存儲不管value的格式,照單全收。(Redis包含了其他功能) |
| 圖存儲 | FlockDB | FlockDB | 圖形關(guān)系的最佳存儲。使用傳統(tǒng)關(guān)系數(shù)據(jù)庫來解決的話性能低下,而且設(shè)計使用不方便。 |
| 對象存儲 | Neo4J、Versant | Versant | 通過類似面向?qū)ο笳Z言的語法操作數(shù)據(jù)庫,通過對象的方式存取數(shù)據(jù)。 |
| xml數(shù)據(jù)庫 | BerkeleyDB、BaseX | BaseX | 高效的存儲XML數(shù)據(jù),并支持XML的內(nèi)部查詢語法,比如XQuery,Xpath。 |
1.2,MongoDB介紹
MongoDB是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。它支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似 json 的 bson 格式,因此可以存儲比較復(fù)雜的數(shù)據(jù)類型。
MongoDB是一個基于分布式文件存儲的數(shù)據(jù)庫。由 C++ 語言編寫。旨在為 WEB 應(yīng)用提供可擴展的高性能數(shù)據(jù)存儲解決方案。
它的特點是高性能、易部署、易使用,存儲數(shù)據(jù)非常方便。
主要功能特性有:
- C++編寫的運行穩(wěn)定性能高的數(shù)據(jù).
- 面向集合存儲,易存儲對象類型的數(shù)據(jù)。
- 模式自由。
- 支持動態(tài)查詢。
- 支持完全索引,包含內(nèi)部對象。
- 支持查詢。
- 支持復(fù)制和故障恢復(fù)。
- 使用高效的二進(jìn)制數(shù)據(jù)存儲,包括大型對象(如視頻等)。
- 自動處理碎片,以支持云計算層次的擴展性。
- 支持RUBY,PYTHON,JAVA,C++,PHP,C#等多種語言。
- 文件存儲格式為BSON(一種JSON的擴展)。
- 可通過網(wǎng)絡(luò)訪問。
2,安裝說明
2.1,安裝
- MongoDB官網(wǎng): https://www.mongodb.com/ ,注意:偶數(shù)為穩(wěn)定版,如1.6,奇數(shù)為開發(fā)版,如1.7
- MongoDB支持Windows、Linux安裝,安裝很簡單,網(wǎng)上資料很多,這里就不贅言。
- Ubuntu下,安裝mongodb服務(wù)
sudo apt-get install mongodb
mongo -version
mongo
- 安裝python的mongodb包
pip install pymongo
2.2,啟動命令
- 通過service啟動或停止mongodb
sudo service mongodb start
sudo service mongodb stop
sudo service mongodb restart
sudo service mongodb status
- 通過mongod命令直接執(zhí)行
sudo /usr/bin/mongod --config /etc/mongodb.conf &
sudo /usr/bin/mongod --dbpath /home/mongodb/data &
# 此處的路徑表示你要創(chuàng)建數(shù)據(jù)庫的文件夾,其中data文件夾需要自己在MongoDB路徑下創(chuàng)建,系統(tǒng)不會在動幫你生成
2.3,配置文件和數(shù)據(jù)文件
配置文件地址:/etc/mongodb.conf
mongodb的文件結(jié)構(gòu),單個collection是三個文件,例如person.0、person.1、person.ns(和Mysql的數(shù)據(jù)文件結(jié)構(gòu)相似)
.
├── hero.0
├── hero.1
├── hero.ns
├── journal
│ ├── j._0
│ ├── prealloc.1
│ └── prealloc.2
├── local.0
├── local.ns
├── mongod.lock
├── person.0
├── person.1
├── person.ns
├── stu.0
├── stu.1
└── stu.ns
3,用戶交互
3.1,命令行交互
- MongoDB后臺管理Shell,直接輸入mongo即可
$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test
Welcome to the MongoDB shell.
> show dbs
local 0.078125GB
test 0.203125GB
> use test
switched to db test
> show collections
system.indexes
users
> db.users.find()
{ "_id" : ObjectId("59ef19ff87e6b6c5f2b3d215"), "name" : "kevin" }
- 以下信息并沒有測試成功,需要再研究下
數(shù)據(jù)庫的信息存儲在集合中,他們統(tǒng)一使用系統(tǒng)的命名空間:DBNAME.system.*
DBNAME 可用 db 或數(shù)據(jù)庫名替代
DBNAME.system.namespaces :列出所有名字空間
DBNAME.system.indexs :列出所有索引
DBNAME.system.profile :列出數(shù)據(jù)庫概要信息
DBNAME.system.users :列出訪問數(shù)據(jù)庫的用戶
DBNAME.system.sources :列出服務(wù)器信息
3.2,MongoDb Web用戶界面
MongoDB 提供了簡單的 HTTP 用戶界面。如果你想啟用該功能,需要在啟動的時候指定參數(shù) --rest 。
MongoDB 的 Web 界面訪問端口比服務(wù)的端口多1000。
如果你的MongoDB運行端口使用默認(rèn)的27017,你可以在端口號為28017訪問web用戶界面,即地址為:http://192.168.1.89:28017。
- 啟動命令
$ sudo mongod --dbpath=/var/lib/mongodb --rest
-
可視化頁面
mongodb.png
4,軟件開發(fā)
4.1,Python
#-*- coding:utf-8 -*-
import pymongo
def system():
print('◆您將進(jìn)入數(shù)據(jù)庫管理系統(tǒng),數(shù)據(jù)無價、謹(jǐn)慎操作!◆')
print('◇1:查看數(shù)據(jù)◇')
print('◇2:增加數(shù)據(jù)◇')
print('◇3:修改數(shù)據(jù)◇')
print('◇4:刪除數(shù)據(jù)◇')
print('◇5:搜索數(shù)據(jù)◇')
print('●6:退出數(shù)據(jù)庫管理系統(tǒng)●')
# 建立與mongodb的連接
client = pymongo.MongoClient('192.168.1.89', 27017)
# 得到數(shù)據(jù)庫
stu = client['person']
# 得到一個數(shù)據(jù)集合
message = stu['message']
while True:
order = int(raw_input('請輸入相關(guān)指令:'))
if order==1:
exit = message.count()
if exit==0:
print('抱歉,數(shù)據(jù)庫中目前沒有相關(guān)數(shù)據(jù)!')
else:
for data in message.find():
content = data['name']+data['age']+data['sex']
print(content)
elif order ==2:
name = raw_input('請輸入學(xué)生姓名:')
age = raw_input('請輸入學(xué)生年齡:')
sex = raw_input('請輸入學(xué)生性別(男/女):')
data = {
'name':name,
'age':age,
'sex':sex,
}
message.insert_one(data)
print ('添加成功!')
elif order == 3:
name = raw_input('請輸入要修改的學(xué)生姓名:')
exit = message.count({'name': name})
if exit != 0:
age = raw_input('請輸入修改后的學(xué)生年齡:')
message.update({'name':name},{'$set':{'age':age}})
print('修改成功')
else:
print '抱歉,數(shù)據(jù)庫中沒有這個學(xué)生的信息!'
elif order == 4:
name = raw_input('請輸入要刪除的學(xué)生姓名:')
exit = message.count({'name': name})
if exit != 0:
message.remove({'name':name})
print('刪除成功')
else:
print '抱歉,數(shù)據(jù)庫中沒有這個學(xué)生的信息!'
elif order == 5:
name = raw_input('請輸入要查詢的學(xué)生姓名:')
exit = message.count({'name':name})
if exit!=0:
data = message.find_one({'name':name})
content = data['name']+data['age']+data['sex']
print content
else:
print '抱歉,數(shù)據(jù)庫中沒有這個學(xué)生的信息!'
elif order == 6:
print('感謝您的使用!')
break
else:
print('您的輸入有誤,請輸入有效指令(1/2/3/4/5)')
if __name__ == '__main__':
system()
4.2,Js
- 暫略
5,高級應(yīng)用
基于MongoDB的python日志功能
http://www.open-open.com/lib/view/open1482979886103.htmlMongoDB高可用集群配置的幾種方案
http://www.open-open.com/lib/view/open1476329503902.html
