nosql七种武器之长生剑 mongodb的使用介绍

25
盛盛盛盛 成成成 @snda DBA http:// www.goziwa.com QQ:1913908 2010-10 NOSQL 盛盛盛盛盛盛盛盛 MongoDB 盛盛盛盛盛

Upload: yczealot

Post on 05-Dec-2014

7.981 views

Category:

Technology


3 download

DESCRIPTION

TRANSCRIPT

Page 1: Nosql七种武器之长生剑 mongodb的使用介绍

盛大网络

成江东 @snda DBAhttp://www.goziwa.com

QQ:19139082010-10

NOSQL七种武器之长生剑MongoDB的使用介绍

Page 2: Nosql七种武器之长生剑 mongodb的使用介绍

一条曲线

Page 3: Nosql七种武器之长生剑 mongodb的使用介绍

思考

一.并发压力快速提高?二.需求变化快?三.数据水平拆分?

Page 4: Nosql七种武器之长生剑 mongodb的使用介绍

内 容

MongoDB 是什么

MongoDB 的特性

MongoDB 的适用场景

MongoDB 的与其它数据库的对比

Page 5: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 是什么一.NOSQL 数据库是什么?非关系型的数据库,主要用于社区类 WEB2.0 网站。主要解决:

对数据库高并发的需求 对海量数据的高效率存储和访问的需求 对数据库的高可扩展性和高可用性的需求

Page 6: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 是什么

二.性能与一致性 -- 鱼与熊掌可兼得 ?

Page 7: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 是什么

CAP 理论-鱼与熊掌不可得兼

一致性( C )

可用性 (A)

分区容忍性 (P) 一个分布式系统最多只能同时满足两个。CA :传统关系数据库 AP : key-value 数据库

Page 8: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 是什么

所以, MongoDB 不能解决: 数据库事务一致性需求 数据库的写实时性和读实时性需求 对复杂的 SQL 查询,特别是多表关联查询的需求

Page 9: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 是什么

三. MongoDB 数据库是什么?取自“ humongous” (海量的) , 是由 10gen 开发并维护的一种开源,高

性能,可扩展,无模式,面向文档 (document-oriented) 的数据库,其内存储的是一种 JSON-like 结构化数据。它介于关系数据库和非关系数据库之间,是非关系数据库中最像关系数据库的。

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

下载: http://www.mongodb.org/downloads

最新版本: 1.6.3(2010-09-23)

支持操作系统: Os X Linux Windows Solaris

Page 10: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性

一. JSON 格式文档数据库

Page 11: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性document!=row 文档{

"_id" : ObjectId("4caeb59438336e36fcdb2b59"),

"uid" : 11909,

"uname " : " bird007 " ,

"address" : {

"province" : " 湖北 ",

"city" : " 武汉“ ,

},

}

{

"_id" : ObjectId("4caeb59438336e36fc9306f4"),

"uid" : 11910,

"uname " : " magicman " ,

“sex " : 0

}

Page 12: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性

二.自由数据模式 支持在对象和数组内嵌入其它的对象和数组 Mongo 模式设计中的一个关键问题就是“是值得为这个对象新建一个集合呢,还是把这个对象嵌入

到其它的集合中”。

Page 13: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性

三.全面索引支持 基本索引: db.t_user.ensureIndex({uname:1})

唯一索引: db.t_user.ensureIndex({uname:1},{unique:true})

内嵌文档中的 key : db.t_user.ensureIndex({"address.city":1})

文档本身: db.t_user.ensureIndex({"address":1})

复合索引:db.t_user.ensureIndex({"address.province":1,"address.city":1,"address.postcode":1,"address.room

":1})

在线索引: db.t_user.ensureIndex({uname:1}, {background:true});

Page 14: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性

四.复制和自动分片带来的高可用性

Page 15: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性 MongoDB 分片集群由 2 个以上的 shards, 1个以上的 config servers, 和任意数量的 mongos servers

组成,应用程序连接 mongos servers mongod 数据库服务器进程,类似于 mysqld replica set 是 N 个一组的 mongod 节点,协同工作可提供自动失效转移,是 replica pairs 的升级版 MongoDB v1.6 开始可以使用于生产环境

Page 16: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性

测试 :Auto sharding: http://www.goziwa.com/?p=1015

Replica Set:http://www.goziwa.com/?p=1040

3 shards:192.168.0.15 ,192.168.0.16, 192.168.0.17

1 config:192.168.0.14

1 mongos:192.168.0.13

Page 17: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性安装 php 驱动: pecl install mongo

Php 测试角本 test.php

<?php

// 连接 localhost:27017$conn = new Mongo();

// 选择数据库 test$db = $conn->test;

// 选择结果集$collection = $db->app;

for($i=1;$i<10000000;$i++){

$data1 = mktime(0,0,0,1,1,1950);

$data2 = mktime(0,0,0,1,1,2000);

$rand_time = rand($data1,$data2);

$credate=date(“Y-m-d H:i:s”,$rand_time);

$userid= rand(100000000,900000000);

$appid= rand(1,50);

$new = array(‘appid’ => $appid, ‘userid’ => $userid, ‘credate’ => $credate);$collection->insert($new);}

?>

Page 18: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性 发现在初期 , 分布不均匀 , 第一台 DB 上有 100 多万条 , 而第 2,3 台上各有 30 万条 ,20 万条。 增加一台服务器测试,继续插入数据到 500 万条后看发现数据已经变为

test.app chunks: { “appid” : { $minKey : 1 } } – >> { “ appid” : 1 } on : shard0003 { “ t” : 34000, “ i” : 0 } { “ appid” : 1 } – >> { “ appid” : 3 } on : shard0003 { “ t” : 31000, “ i” : 0 } { “ appid” : 3 } – >> { “ appid” : 4 } on : shard0003 { “ t” : 33000, “ i” : 0 } { “ appid” : 4 } – >> { “ appid” : 5 } on : shard0003 { “ t” : 36000, “ i” : 0 } { “ appid” : 5 } – >> { “ appid” : 6 } on : shard0003 { “ t” : 39000, “ i” : 0 } { “ appid” : 6 } – >> { “ appid” : 7 } on : shard0003 { “ t” : 37000, “ i” : 0 } { “ appid” : 7 } – >> { “ appid” : 8 } on : shard0003 { “ t” : 40000, “ i” : 0 } { “ appid” : 8 } – >> { “ appid” : 9 } on : shard0002 { “ t” : 6000, “ i” : 0 } { “ appid” : 9 } – >> { “ appid” : 10 } on : shard0002 { “ t” : 7000, “ i” : 0 } { “ appid” : 10 } – >> { “ appid” : 11 } on : shard0003 { “ t” : 42000, “ i” : 0 } { “ appid” : 11 } – >> { “ appid” : 12 } on : shard0002 { “ t” : 9000, “ i” : 0 } { “ appid” : 12 } – >> { “ appid” : 13 } on : shard0001 { “ t” : 10000, “ i” : 0 } { “ appid” : 13 } – >> { “ appid” : 14 } on : shard0002 { “ t” : 11000, “ i” : 0 } { “ appid” : 14 } – >> { “ appid” : 15 } on : shard0001 { “ t” : 12000, “ i” : 0 } { “ appid” : 15 } – >> { “ appid” : 16 } on : shard0002 { “ t” : 13000, “ i” : 0 } { “ appid” : 16 } – >> { “ appid” : 17 } on : shard0001 { “ t” : 14000, “ i” : 0 } { “ appid” : 17 } – >> { “ appid” : 18 } on : shard0002 { “ t” : 15000, “ i” : 0 } { “ appid” : 18 } – >> { “ appid” : 19 } on : shard0001 { “ t” : 16000, “ i” : 0 } { “ appid” : 19 } – >> { “ appid” : 20 } on : shard0002 { “ t” : 17000, “ i” : 0 } { “ appid” : 20 } – >> { “ appid” : 21 } on : shard0001 { “ t” : 18000, “ i” : 0 } { “ appid” : 21 } – >> { “ appid” : 22 } on : shard0002 { “ t” : 19000, “ i” : 0 } { “ appid” : 22 } – >> { “ appid” : 23 } on : shard0001 { “ t” : 20000, “ i” : 0 } { “ appid” : 23 } – >> { “ appid” : 25 } on : shard0002 { “ t” : 21000, “ i” : 0 } { “ appid” : 25 } – >> { “ appid” : 26 } on : shard0001 { “ t” : 22000, “ i” : 0 } { “ appid” : 26 } – >> { “ appid” : 27 } on : shard0002 { “ t” : 23000, “ i” : 0 } { “ appid” : 27 } – >> { “ appid” : 28 } on : shard0001 { “ t” : 24000, “ i” : 0 } { “ appid” : 28 } – >> { “ appid” : 29 } on : shard0002 { “ t” : 25000, “ i” : 0 } { “ appid” : 29 } – >> { “ appid” : 30 } on : shard0001 { “ t” : 26000, “ i” : 0 } { “ appid” : 30 } – >> { “ appid” : 31 } on : shard0002 { “ t” : 27000, “ i” : 0 } { “ appid” : 31 } – >> { “ appid” : 32 } on : shard0001 { “ t” : 30000, “ i” : 2 } { “ appid” : 32 } – >> { “ appid” : 33 } on : shard0001 { “ t” : 42000, “ i” : 1 } { “ appid” : 33 } – >> { “ appid” : 34 } on : shard0001 { “ t” : 29000, “ i” : 0 } { “ appid” : 34 } – >> { “ appid” : 35 } on : shard0002 { “ t” : 40000, “ i” : 1 } { “ appid” : 35 } – >> { “ appid” : 36 } on : shard0003 { “ t” : 32000, “ i” : 0 } { “ appid” : 36 } – >> { “ appid” : 37 } on : shard0003 { “ t” : 35000, “ i” : 0 } { “ appid” : 37 } – >> { “ appid” : 38 } on : shard0003 { “ t” : 38000, “ i” : 0 } { “ appid” : 38 } – >> { “ appid” : 39 } on : shard0003 { “ t” : 41000, “ i” : 0 } { “ appid” : 39 } – >> { “ appid” : 40 } on : shard0000 { “ t” : 3000, “ i” : 181 } { “ appid” : 40 } – >> { “ appid” : 41 } on : shard0000 { “ t” : 3000, “ i” : 182 } { “ appid” : 41 } – >> { “ appid” : 42 } on : shard0000 { “ t” : 5000, “ i” : 12 } { “ appid” : 42 } – >> { “ appid” : 43 } on : shard0000 { “ t” : 6000, “ i” : 1 } { “ appid” : 43 } – >> { “ appid” : 44 } on : shard0000 { “ t” : 3000, “ i” : 184 } { “ appid” : 44 } – >> { “ appid” : 45 } on : shard0000 { “ t” : 6000, “ i” : 2 } { “ appid” : 45 } – >> { “ appid” : 46 } on : shard0000 { “ t” : 41000, “ i” : 1 } { “ appid” : 46 } – >> { “ appid” : 47 } on : shard0000 { “ t” : 4000, “ i” : 108 } { “ appid” : 47 } – >> { “ appid” : 48 } on : shard0000 { “ t” : 4000, “ i” : 109 } { “ appid” : 48 } – >> { “ appid” : 49 } on : shard0000 { “ t” : 4000, “ i” : 110 } { “ appid” : 49 } – >> { “ appid” : 50 } on : shard0000 { “ t” : 5000, “ i” : 1 } { “ appid” : 50 } – >> { “ appid” : { $maxKey : 1 } } on : shard0000 { “ t” : 3000, “ i” : 1 } test.people chunks: { “ name” : { $minKey : 1 } } – >> { “ name” : { $maxKey : 1 } } on : shard0000 { “ t” : 1000, “ i” : 0 }

Page 19: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性

五.丰富的查询语句

In 查询 排序

查询分片

Count

Exists

正则 游标

数组元素个数

类型匹配

Javascript shell

Page 20: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性六. Map/Reduce是聚合和过滤数据的工具m=function(){emit(this.sex,1);}

r=function(key,value){

var count=0;

for(i in value){

count+=value[i];

}

return count;

}

res=db.t_user.mapReduce(m,r);

db[res.result].find();

{ "_id" : 0, "value" : 134211 }

{ "_id" : 1, "value" : 323445 }

Page 21: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的特性

七 . 性能优异 并发写的性能有 1.5 万每秒 无外键约束,无事务 异步写磁盘

八.其它特性 GridFS 使用方便, MongoDB 会自动创建数据库 (db) 和集合 (collection) ,无需显式执行

Page 22: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的适用场景

一.适用场景1 ,结构不固定,有数据嵌套2 ,要求高并发性3 ,经常需要做数据水平拆分4 ,内存大于数据量(推荐)

二.不足之处1 ,比较占用硬盘空间,性能受内存影响2 ,性能依赖内存,同时无法指定内存大小,容易被其它程序占用3 , MongonDB 不支持事务,不支持 join

4 ,每个 Document 的限制是最大不超过 4MB

Page 23: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的适用场景

三. Why MongoDB 性能优异 扩展力强 面向文档 部署简单 功能全面 易于开发 支持全面

邮件组 :http://groups.google.com/group/mongodb-user豆瓣小组 :http://www.douban.com/group/mongodb/

Page 24: Nosql七种武器之长生剑 mongodb的使用介绍

MongoDB 的与其它数据库的对比

一. VS

二. VS

三. VS

Page 25: Nosql七种武器之长生剑 mongodb的使用介绍