您的位置:首页 > 数据库 > Mongodb

NoSQL数据库:MongoDB初探

2011-05-21 10:55 453 查看
原文地址:http://www.searchtb.com/2010/12/a-probe-into-the-mongodb.html

跟着时下炒得火热的NOSQL潮流,学习了一下mongodb,记录在此,希望与感兴趣的同学一起研究!

MongoDB概述

mongodb由C++写就,其名字来自humongous这个单词的中间部分,是由10gen开发并维护的,关于它的一个最简洁描述为:scalable, high-performance, open source, schema-free, document-oriented database。MongoDB的主要目标是在键/值存储方式(提供了高性能和高度伸缩性)以及传统的RDBMS系统(丰富的功能)架起一座桥梁,集两者的优势于一身。

MongoDB特性:

l 面向文档存储

l 全索引支持,扩展到内部对象和内嵌数组

l 复制和高可用

l 自动分片支持云级扩展性

l 查询记录分析

l 动态查询

l 快速,就地更新

l 支持Map/Reduce操作

l GridFS文件系统

l 商业支持,培训和咨询

官网: http://www.mongodb.org/

配置

Master-slaves 模式



机器IP角色
test001192.168.1.1master
test002192.168.1.2slave
test003192.168.1.3slave
test004192.168.1.4slave
test005192.168.1.5slave
test006192.168.1.6slave
启动master:

添加repl用户:

启动slaves:

添加repl用户:

autoresync 参数会在系统发生意外情况造成主从数据不同步时,自动启动复制操作 (同步复制 10 分钟内仅执行一次)。除此之外,还可以用 –slavedelay 设定更新频率(秒)。

通常我们会使用主从方案实现读写分离,但需要设置 Slave_OK。

slaveOk

When querying a replica pair or replica set, drivers route their requests to the master mongod by default; to perform a query against an (arbitrarily-selected) slave, the query can be run with the slaveOk option. Here’s how to do so in the shell:

db.getMongo().setSlaveOk(); // enable querying a slave
db.users.find(...)

Note: some language drivers permit specifying the slaveOk option on each find(), others make this a connection-wide setting. See your language’s driver for details.

Replica Set模式



Replica Sets 使用 n 个 Mongod 节点,构建具备自动容错转移(auto-failover)、自动恢复(auto-recovery) 的高可用方案。

机器IP角色
test001192.168.1.1secondary
test002192.168.1.2secondary
test003192.168.1.3primary
test004192.168.1.4secondary
test005192.168.1.5secondary
test006192.168.1.6secondary
test007192.168.1.7secondary
启动:

添加repl用户:

配置:

查看:

访问 http://test001 :28017/_replSet

或者

在Replica Sets上做操作后调用getlasterror使写操作同步到至少3台机器后才返回

db.runCommand( { getlasterror : 1 , w : 3 } )

注:该模式不支持auth功能,需要auth功能请选择m-s模式

Sharding模式



要构建一个 MongoDB Sharding Cluster,需要三种角色:

Shard Server: mongod 实例,用于存储实际的数据块。

Config Server: mongod 实例,存储了整个 Cluster Metadata,其中包括 chunk 信息。

Route Server: mongos 实例,前端路由,客户端由此接入,且让整个集群看上去像单一进程数据库。

机器IP角色
test002192.168.1.2mongod shard11:27017
test003192.168.1.3mongod shard21:27017
test004192.168.1.4mongod shard31:27017
test005192.168.1.5mongod config1:20000
mongs1:30000
test006192.168.1.6mongod config2:20000
mongs2:30000
test007192.168.1.7mongod config3:20000
mongs3:30000
test008192.168.1.8mongod shard12:27017
test009192.168.1.9mongod shard22:27017
test010192.168.1.10mongod shard32:27017

Shard配置

Shard1

[test002; test008]

test002:

test008:

初始化shard1

Shard2

[test003; test009]

test003:

test009:

初始化shard2

Shard3

[test004; test010]

test004:

test010:

初始化shard3

config server配置

[test005; test006; test007]

Mongos配置

[test005; test006; test007]

Route 转发请求到实际的目标服务进程,并将多个结果合并回传给客户端。Route 本身并不存储任何数据和状态,仅在启动时从 Config Server 获取信息。Config Server 上的任何变动都会传递给所有的 Route Process。

Configuring the Shard Cluster

1. 连接admin数据库

2. 加入shards

3. Listing shards

如果列出了以上3个shards,表示shards已经配置成功

4. 激活数据库和表分片

使用

shell操作数据库

超级用户相关:

1) 进入数据库admin

2) 增加或修改用户密码

3) 查看用户列表

4) 用户认证

5) 删除用户

6) 查看所有用户

7) 查看所有数据库

8) 查看所有的collection

9) 查看各collection的状态

10) 查看主从复制状态

11) 修复数据库

12) 设置记录profiling,0=off 1=slow 2=all

13) 查看profiling

14) 拷贝数据库

15) 删除collection

16) 删除当前的数据库

增加删除修改:

1) Insert

嵌套对象:

数组对象:

2) delete

删除name=’dump’的用户信息:

删除foo表所有信息:

3) update

//update foo set xx=4 where yy=6

//如果不存在则插入,允许修改多条记录

查询:

其他:

索引:

1(ascending),-1(descending)

MongoDB Drivers

C

C#

C++

Haskell

Java

Javascript

Perl

PHP

Python

Ruby

Scala (via Casbah)

Mongodb支持的client 编程api非常多,由于dump中心是建立在hadoop的基础上的,所以着重介绍java api,后面的测试程序采用的也是java api.

MongoDB in Java

下载MongoDB的Java驱动,把jar包(mongo-2.3.jar)扔到项目里去就行了,

Java中,Mongo对象是线程安全的,一个应用中应该只使用一个Mongo对象。Mongo对象会自动维护一个连接池,默认连接数为10。

MongoDB 测试

测试版本: 1.6.3

采用单线程分别插入100万,300万,500万,1000万数据和多个线程,每线程插入100万数据.

插入数据格式:

1) Master slaves模式

Insert

Per-thread rowsrun timePer-thread insertTotal-insertTotal rowsthreads
100000020500005000010000001
300000060500005000030000001
500000099505055050550000001
8000000159503145031480000001
100000002084807648076100000001
100000064156253125020000002
Mongodb只有主节点才能进行插入和更新操作.

Update

数据格式:

Per-thread rowsrun timePer-thread updateTotal-updateTotal rowsthreads
100000096104161041610000001
3000000287104521045230000001
100000018853191595730000003
100000035128491424550000005
Select

以”_id”字段为key,返回整条记录

a) 客户端:单机多线程

Per-thread rowsrun timePer-thread selectTotal-selectTotal rowsthreads
100000072138881388810000001
10000001297751775191000000010
10000005541805902525000000050
1000000112189289206100000000100
1000000225644388652200000000200
b) 客户端:分布式多线程

程序部署在39台机器上

Per-thread rowsrun timePer-thread selectTotal-selectTotal rowsthreads
100000017357805780*39=2234701000000*391
100000014027137132*39=27814810000000*3910
50000014063557112*39=27736810000000*3920
20000014331396978*39=27214210000000*3950
2) Replica Set 模式

Insert

Per-thread rowsrun timePer-thread insertTotal-insertTotal rowsthreads
100000040250002500010000001
3000000117256412564130000001
5000000211236962369650000001
8000000289276812768180000001
100000003882577325773100000001
100000083120482409620000002
100000021047622380950000005
Update

Per-thread rowsrun timePer-thread updateTotal-updateTotal rowsthreads
100000028357143571410000001
300000083361443614430000001
100000014668492054730000003
100000026238161908350000005
Select

以”_id”字段为key,返回整条记录

a) 客户端:单机多线程

Per-thread rowsrun timePer-thread selectTotal-selectTotal rowsthreads
10000001985050505010000001
10000002643787378781000000010
100000043622931146785000000050
10000007541326132625100000000100
10000001526655131061200000000200
b) 客户端:分布式多线程

程序部署在39台机器上

Per-thread rowsrun timePer-thread selectTotal-selectTotal rowsthreads
100000021646294629*39=1805311000000*391
100000013757297293*39=28442710000000*3910
50000014693406807*39=26547310000000*3920
20000015611286406*39=24983410000000*3950
3) Sharding 模式

Insert

Per-thread rowsrun timePer-thread insertTotal-insertTotal rowsthreads
100000058172411724110000001
3000000180166661666630000001
5000000373134041340450000001
200000023485471709440000002
2000000447447422371100000005
Update

Per-thread rowsrun timePer-thread updateTotal-updateTotal rowsthreads
100000038263152631510000001
3000000115260862608630000001
100000064156254687530000003
100000093107525376350000005
Select

以”_id”字段为key,返回整条记录

a) 客户端:单机多线程

Per-thread rowsrun timePer-thread selectTotal-selectTotal rowsthreads
10000002773610361010000001
10000004562192219291000000010
10000001158863431775000000050
1000000229943443497100000000100
b) 客户端:分布式多线程

程序部署在39台机器上

Per-thread rowsrun timePer-thread selectTotal-selectTotal rowsthreads
100000065915171517*39= 591631000000*391
100000085401171170*39=4563010000000*3910
小结:

Mongodb在M-S和Repl-Set模式下查询效率还是不错的,区别在于Repl-Set模式如果有primary节点挂掉,系统自己会选举出另一个primary节点,不会影响后续的使用,原来的主节点恢复后自动成为secondary节点,而M-S模式一旦master 节点挂掉需要手工将别的slaves 节点修改成master,另外Repl-Set模式最多只能有7个节点.

由于sharding模式查询速度下降明显,耗时太长,所以只测试了2轮,估计他的威力应该在数据量非常大的环境下才能体现出来吧,以上数据仅供参考,现在只是简单的进行了测试,接下来会对源码进行一下研究,欢迎和感兴趣的同学多多交流!


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: