15
面面面面面面

Mongodb

  • Upload
    bj

  • View
    1.533

  • Download
    16

Embed Size (px)

Citation preview

Page 1: Mongodb

面向文档存储

Page 2: Mongodb

• 介绍 - Mongodb • 动态查询• 主从复制 - Master/Slave • 可扩展 - sharding• 网格文件系统• 性能

Page 3: Mongodb

介绍 |-- bin| |-- mongo (the database shell)| |-- mongod (the core database server)| |-- mongos (auto-sharding process)| |-- mongodump (dump/export utility)| `-- mongorestore (restore/import utility)|-- include (c++ driver include files)| `-- mongo| |-- client| |-- db| |-- grid| `-- util|-- lib|-- lib64

Page 4: Mongodb

介绍

> 面向集合 , 数据节点话 // 内置顶层对象 db ,如同 xml 的 root> 数据文档型 - json // 无维度限制> 主从配置,扩展简单> 支持 MapReduce> 开源

Page 5: Mongodb

动态查询 - 类 SQL(1)

1 . select * from user where name='foobar'

$> db.user.find({ 'name' : 'foobar' })

2 . insert into user('name','age')values('foobar',25)

. alter table user ... add userid ..... . update user set userid=1 where name='foobar' $> db.user.insert({ 'name':'foobar' , 'age':25 }); $> db.user.update({'name':'foobar'},

{$set:{ 'userid':1 }} ) ;

3 . delete from user where name='foobar'

$> db.user.remove({ 'name':'foobar' });

Page 6: Mongodb

动态查询 - 类 SQL(2)

1 $> db.user.find().count(); $> db.user.distinct('name': {$lt:20} ) // $gt > ; $gte >= ;$lt < ;$lte <= ; $ne != ; $> db.user.find({ 'age':{ $in:[ 25,35 ] } }) $> db.user.find().skip(10).limit(20); $> db.user.find().sort({ 'age': -1 })

2 . select name,sum(marks) from user group by name $> db.user.group( { key : { 'name':true } , initial: { msum:0 } , reduce : function(obj,prev){ prev.msum+= obj.age ; } } )

Page 7: Mongodb

主 从

• Master/Slave 1<>n $> mongod --master --port=27017 --dbpath ......

$> mongod --slave --port=27018 --source=localhost:27017 --dbpath .....

• Master/Master $> ./mongod --slave --master --source localhost:10000

$> ./mongod --slave --master --dbpath /data/slave --port 10000 --source localhost

Page 8: Mongodb

$> ./mongod --port 27020 --dbpath /data/1 --master &

$> ./mongod --port 27021 --dbpath /data/2 --master &

$> ./mongod --port 27022 --dbpath /data/3 --slave &

//27022 slave > use local

>db.sources.insert({host:"localhost:27020"})

>db.sources.insert({host:"localhost:27021"})

主 n<>1 从

Page 9: Mongodb

水平扩展 - sharding

Page 10: Mongodb

sharding - 1

• Shard1 : 27020 # 存储 1 服务器• Shard2 : 27021 # 存储 2 服务器• Config : 27022 # 路由服务器

# 启动存储服务器./mongod --dbpath /tmp/mongodata/27020 --port 207020./mongod --dbpath /tmp/mongodata/27021 --port 207021 # 启动路由服务器./mongod --dbpath /tmp/mongodata/27022 --port 27022 ./mongos --configdb localhost:27022

Page 11: Mongodb

sharding - 2

• 进路由 客户端$> use admin// 加入 shard 节点$> db.runCommand( { addshard : "localhost:27020", allowLocal : true } )$> db.runCommand( { addshard : "localhost:27021", allowLocal : true } )$> db.runCommand({listshards:1}); // 查看 shard 节点列表// 新建自动切片的库 user001$> config = connect("localhost:27022")$> config = config.getSisterDB("config")$> user001=db.getSisterDB("user001");$> db.runCommand({enablesharding:"user001"})//user001 中新建表,插入数据$> use user001 ;$> db.createCollection("user_001")$> db.user_001.insert({uid:1,username:"Falcon.C",sex:"m",age:25});

Page 12: Mongodb

sharding - 3

$> ls -R.:27020 27021 27022 mongos.log

./27020:27020.log mongod.lock test.0 test.1 test.ns _tmp

./27020/_tmp:

./27021:27021.log mongod.lock _tmp user.0 user001.0 user001.1 user001.ns user.1 user.ns

./27021/_tmp:

./27022:27022.log config.0 config.ns mongod.lock mongos.log _tmp

./27022/_tmp:

Page 13: Mongodb

sharding - 4

• 官方 sharding : 参考• 测试 : 参考

Page 14: Mongodb

sharding - MapReduce

db.runCommand({ mapreduce : <collection>, map : <mapfunction>, reduce : <reducefunction> [, query : <query filter object>] [, sort : <sort the query. useful for optimization>] [, limit : <number of objects to return from collection>] [, out : <output-collection name>] [, keeptemp: <true|false>] [, finalize : <finalizefunction>] [, scope : <object where fields go into javascript global scope >] [, verbose : true] });

r=function(key,values){ .. }m = function(){ .. }res=db.things.mapReduce(m,r);

Page 15: Mongodb

大量实例

• http://github.com/mongodb/mongo/tree/master/jstests/