CentOS7 安装MongoDB 3.0服务

1,下载&安装

MongoDB 3.0 正式版本发布!这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大、灵活而且易于管理的数据库管理系统。MongoDB宣称,3.0新版本不只提升7到10倍的写入效率以及增加80%的数据压缩率,还能减少95%的运维成本。
  MongoDB 3.0主要新特性包括:
  ·可插入式的存储引擎 API
  ·支持 WiredTiger 存储引擎
  ·MMAPv1 提升
  ·复制集全面提升
  ·集群方面的改进
  ·提升了安全性
  ·工具的提升
WiredTiger 存储引擎是一项难以置信的技术实现,提供无门闩、非堵塞算法来利用先进的硬件平台(如大容量芯片缓存和线程化架构)来提升性能。通过 WiredTiger,MongoDB 3.0 实现了文档级别的并发控制,因此大幅提升了大并发下的写负载。

MongoDB 提供了CentOS yum安装方式。
参考:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/
pdf 手册:
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

安装mongodb

yum install -y mongodb-org

安装了所有相关服务。

......
Running transaction
  Installing : 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

配置文件在:/etc/mongod.conf
数据文件在:/var/lib/mongo
日志文件在:/var/log/mongodb
mongodb服务使用

#启动
service mongod start
#停止
service mongod stop
#重启
service mongod restart
#增加开机启动
chkconfig mongod on

2,MongoDB CRUD

参考:
http://docs.mongodb.org/manual/core/crud-introduction/

连接到MongoDB,很简单,执行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] 
>

2.1,创建数据:

http://docs.mongodb.org/manual/tutorial/insert-documents/
http://docs.mongodb.org/manual/reference/method/db.collection.insert/

> db.users.insert(
... {
... name:"zhang san",
... age:26,
... city:"bei jing"
... }
... )
WriteResult({ "nInserted" : 1 })
> db.users.insert(
... {
... _id:1,
... name:"zhang san",
... age:26,
... city:"bei jing"
... }
... )
WriteResult({ "nInserted" : 1 })
> db.users.insert(
... {
... _id:1,
... name:"zhang san",
... age:26,
... city:"bei jing"
... }
... )
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 11000,
                "errmsg" : "E11000 duplicate key error index: test.users.$_id_ dup key: { : 1.0 }"
        }
})
> db.users.insert(
... {
... _id:2,
... name:"li si",
... age:28,
... city:"shang hai"
... }
... )
WriteResult({ "nInserted" : 1 })

数据可以没有主键_id,如果没有,会自动生成一个。如果设置了_id主键,就必须不重复。
否则报主键冲突:“E11000 duplicate key error index: test.users.$_id_ dup key: { : 1.0 }”

2.2,更新数据:

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

更新使用update,如果增加{ upsert: true },则表示没有查询到数据直接插入。

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

查询到数据才进行删除,并且返回删除数量。

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" }
#查询全部数据
> 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" }

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/
CentOS7 安装MongoDB 3.0服务

使用可视化工具,方便使用MongoDB管理。
首先要修改下端口和ip
vi /etc/mongod.conf

port=27017

dbpath=/var/lib/mongo

# location of pidfile
pidfilepath=/var/run/mongodb/mongod.pid

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip=192.168.1.36

然后重启MongoDB

service mongod restart

接下来就可以创建一个mongodb连接:
CentOS7 安装MongoDB 3.0服务
连接成功之后效果:
CentOS7 安装MongoDB 3.0服务

4,总结

MongoDB 3.0操作起来还是很方便的。能高效的使用。
同时MongoDB扩展也很方便。接下来研究。
对应互联网业务来说没有复杂的join查询。只追求高效,快速访问。

更多MongoDB相关教程见以下内容

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

相关推荐