CentOS7 安装MongoDB 3.0服务
1,下載&安裝
MongoDB 3.0 正式版本發(fā)布!這標(biāo)志著 MongoDB 數(shù)據(jù)庫進(jìn)入了一個(gè)全新的發(fā)展階段,提供強(qiáng)大、靈活而且易于管理的數(shù)據(jù)庫管理系統(tǒng)。MongoDB宣稱,3.0新版本不只提升7到10倍的寫入效率以及增加80%的數(shù)據(jù)壓縮率,還能減少95%的運(yùn)維成本。
MongoDB 3.0主要新特性包括:
·可插入式的存儲(chǔ)引擎 API
·支持 WiredTiger 存儲(chǔ)引擎
·MMAPv1 提升
·復(fù)制集全面提升
·集群方面的改進(jìn)
·提升了安全性
·工具的提升
WiredTiger 存儲(chǔ)引擎是一項(xiàng)難以置信的技術(shù)實(shí)現(xiàn),提供無門閂、非堵塞算法來利用先進(jìn)的硬件平臺(tái)(如大容量芯片緩存和線程化架構(gòu))來提升性能。通過 WiredTiger,MongoDB 3.0 實(shí)現(xiàn)了文檔級(jí)別的并發(fā)控制,因此大幅提升了大并發(fā)下的寫負(fù)載。
MongoDB 提供了centos yum安裝方式。
參考:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/
pdf 手冊(cè):
http://docs.mongodb.org/manual/MongoDB-manual.pdf
vi /etc/yum.repos.d/mongodb-org-3.0.repo
[mongodb-org-3.0] name=MongoDB Repository baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/ gpgcheck=0 enabled=1- 1
- 2
- 3
- 4
- 5
安裝mongodb
yum install -y mongodb-org- 1
安裝了所有相關(guān)服務(wù)。
...... Running transactionInstalling : mongodb-org-shell-3.0.2-1.el7.x86_64 1/5 Installing : mongodb-org-tools-3.0.2-1.el7.x86_64 2/5 Installing : mongodb-org-mongos-3.0.2-1.el7.x86_64 3/5 Installing : mongodb-org-server-3.0.2-1.el7.x86_64 4/5 Installing : mongodb-org-3.0.2-1.el7.x86_64 5/5 Verifying : mongodb-org-3.0.2-1.el7.x86_64 1/5 Verifying : mongodb-org-server-3.0.2-1.el7.x86_64 2/5 Verifying : mongodb-org-mongos-3.0.2-1.el7.x86_64 3/5 Verifying : mongodb-org-tools-3.0.2-1.el7.x86_64 4/5 Verifying : mongodb-org-shell-3.0.2-1.el7.x86_64 5/5- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
配置文件在:/etc/mongod.conf
數(shù)據(jù)文件在:/var/lib/mongo
日志文件在:/var/log/mongodb
mongodb服務(wù)使用
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2,MongoDB CRUD
參考:
http://docs.mongodb.org/manual/core/crud-introduction/
連接到MongoDB,很簡(jiǎn)單,執(zhí)行mongo就可以了。
# mongo MongoDB shell version: 3.0.2 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2015-04-29T18:03:17.544+0800 I STORAGE [initandlisten] 2015-04-29T18:03:17.544+0800 I STORAGE [initandlisten] ** WARNING: Readahead for /var/lib/mongo is set to 4096KB 2015-04-29T18:03:17.544+0800 I STORAGE [initandlisten] ** We suggest setting it to 256KB (512 sectors) or less 2015-04-29T18:03:17.544+0800 I STORAGE [initandlisten] ** http://dochub.mongodb.org/core/readahead 2015-04-29T18:03:17.679+0800 I CONTROL [initandlisten] 2015-04-29T18:03:17.679+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2015-04-29T18:03:17.679+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-04-29T18:03:17.679+0800 I CONTROL [initandlisten] 2015-04-29T18:03:17.679+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2015-04-29T18:03:17.679+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-04-29T18:03:17.679+0800 I CONTROL [initandlisten] 2015-04-29T18:03:17.679+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files. 2015-04-29T18:03:17.679+0800 I CONTROL [initandlisten] >- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
2.1,創(chuàng)建數(shù)據(jù):
http://docs.mongodb.org/manual/tutorial/insert-documents/
http://docs.mongodb.org/manual/reference/method/db.collection.insert/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
數(shù)據(jù)可以沒有主鍵_id,如果沒有,會(huì)自動(dòng)生成一個(gè)。如果設(shè)置了_id主鍵,就必須不重復(fù)。
否則報(bào)主鍵沖突:“E11000 duplicate key error index: test.users.$_id_ dup key: { : 1.0 }”
2.2,更新數(shù)據(jù):
http://docs.mongodb.org/manual/tutorial/modify-documents/
> db.users.update( ... {_id:2}, ... { ... $set: { ... city:"guang zhou" ... } ... } ... ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.update( ... {_id:3}, ... { ... $set: { ... city:"si chuan" ... } ... }, ... { upsert: true } ... ) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 3 })- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
更新使用update,如果增加{ upsert: true },則表示沒有查詢到數(shù)據(jù)直接插入。
2.3,刪除:
http://docs.mongodb.org/manual/tutorial/remove-documents/
> db.users.remove({_id:3}) WriteResult({ "nRemoved" : 1 }) > db.users.remove({_id:4}) WriteResult({ "nRemoved" : 0 })- 1
- 2
- 3
- 4
查詢到數(shù)據(jù)才進(jìn)行刪除,并且返回刪除數(shù)量。
2.4,查詢:
http://docs.mongodb.org/manual/tutorial/query-documents/
> db.users.find({age:{ $gt: 26}}) { "_id" : 2, "name" : "li si", "age" : 28, "city" : "guang zhou" } > db.users.find({age:{ $gt: 25}}) { "_id" : ObjectId("5540adf29b0f52a6786de216"), "name" : "zhang san", "age" : 26, "city" : "bei jing" } { "_id" : 1, "name" : "zhang san", "age" : 26, "city" : "bei jing" } { "_id" : 2, "name" : "li si", "age" : 28, "city" : "guang zhou" } #查詢?nèi)繑?shù)據(jù) > db.users.find() { "_id" : ObjectId("5540adf29b0f52a6786de216"), "name" : "zhang san", "age" : 26, "city" : "bei jing" } { "_id" : 1, "name" : "zhang san", "age" : 26, "city" : "bei jing" } { "_id" : 2, "name" : "li si", "age" : 28, "city" : "guang zhou" }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2.5,更多方法
db.collection.aggregate()
db.collection.count()
db.collection.copyTo()
db.collection.createIndex()
db.collection.getIndexStats()
db.collection.indexStats()
db.collection.dataSize()
db.collection.distinct()
db.collection.drop()
db.collection.dropIndex()
db.collection.dropIndexes()
db.collection.ensureIndex()
db.collection.explain()
db.collection.find()
db.collection.findAndModify()
db.collection.findOne()
db.collection.getIndexes()
db.collection.getShardDistribution()
db.collection.getShardVersion()
db.collection.group()
db.collection.insert()
db.collection.isCapped()
db.collection.mapReduce()
db.collection.reIndex()
db.collection.remove()
db.collection.renameCollection()
db.collection.save()
db.collection.stats()
db.collection.storageSize()
db.collection.totalSize()
db.collection.totalIndexSize()
db.collection.update()
db.collection.validate()
3,MongoDB可視化工具
http://www.robomongo.org/
使用可視化工具,方便使用MongoDB管理。
首先要修改下端口和ip
vi /etc/mongod.conf
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
然后重啟MongoDB
service mongod restart- 1
接下來就可以創(chuàng)建一個(gè)mongodb連接:
連接成功之后效果:
4,總結(jié)
本文原文連接: http://blog.csdn.net/freewebsys/article/details/45368809 轉(zhuǎn)載請(qǐng)注明出處!
總結(jié)
以上是生活随笔為你收集整理的CentOS7 安装MongoDB 3.0服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一致性hash介绍
- 下一篇: process调用protothread