Mongodb

1,739 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,739
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
33
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Mongodb

  1. 1. 面向文档存储
  2. 2. <ul><li>介绍 - Mongodb </li></ul><ul><li>动态查询 </li></ul><ul><li>主从复制 - Master/Slave </li></ul><ul><li>可扩展 - sharding </li></ul><ul><li>网格文件系统 </li></ul><ul><li>性能 </li></ul>
  3. 3. 介绍 <ul><li>|-- bin </li></ul><ul><li>| |-- mongo (the database shell) </li></ul><ul><li>| |-- mongod (the core database server) </li></ul><ul><li>| |-- mongos (auto-sharding process) </li></ul><ul><li>| |-- mongodump (dump/export utility) </li></ul><ul><li>| `-- mongorestore (restore/import utility) </li></ul><ul><li>|-- include (c++ driver include files) </li></ul><ul><li>| `-- mongo </li></ul><ul><li>| |-- client </li></ul><ul><li>| |-- db </li></ul><ul><li>| |-- grid </li></ul><ul><li>| `-- util </li></ul><ul><li>|-- lib </li></ul><ul><li>|-- lib64 </li></ul>
  4. 4. 介绍 <ul><li>> 面向集合 , 数据节点话 </li></ul><ul><li>// 内置顶层对象 db ,如同 xml 的 root </li></ul><ul><li>> 数据文档型 - json </li></ul><ul><li>// 无维度限制 </li></ul><ul><li>> 主从配置,扩展简单 </li></ul><ul><li>> 支持 MapReduce </li></ul><ul><li>> 开源 </li></ul>
  5. 5. 动态查询 - 类 SQL(1) <ul><li>1 . select * from user where name='foobar' </li></ul><ul><li> $> db.user. find ({ 'name' : 'foobar' }) </li></ul><ul><li>2 . insert into user('name','age')values('foobar',25) </li></ul><ul><li>. alter table user ... add userid ..... </li></ul><ul><li>. update user set userid=1 where name='foobar' </li></ul><ul><li>$> db.user. insert ({ 'name':'foobar' , 'age':25 }); </li></ul><ul><li>$> db.user. update ({'name':'foobar'}, </li></ul><ul><li>{$set:{ 'userid':1 }} ) ; </li></ul><ul><li>3 . delete from user where name='foobar' </li></ul><ul><li>$> db.user. remove ({ 'name':'foobar' }); </li></ul>
  6. 6. 动态查询 - 类 SQL(2) <ul><li>1 $> db.user.find(). count (); </li></ul><ul><li>$> db.user. distinct ('name': {$lt:20} ) </li></ul><ul><li>// $gt > ; $gte >= ;$lt < ;$lte <= ; $ne != ; </li></ul><ul><li>$> db.user.find({ 'age':{ $in:[ 25,35 ] } }) </li></ul><ul><li>$> db.user.find(). skip (10). limit (20); </li></ul><ul><li>$> db.user.find(). sort ({ 'age': -1 }) </li></ul><ul><li>2 . select name,sum(marks) from user group by name </li></ul><ul><li>$> db.user. group ( { </li></ul><ul><li>key : { 'name':true } , </li></ul><ul><li>initial: { msum:0 } , </li></ul><ul><li>reduce : function(obj,prev){ prev.msum+= obj.age ; } </li></ul><ul><li>} ) </li></ul>
  7. 7. 主 从 <ul><li>Master/Slave 1<>n </li></ul><ul><li>$> mongod -- master --port=27017 --dbpath ...... </li></ul><ul><li>$> mongod -- slave --port=27018 --source=localhost:27017 --dbpath ..... </li></ul><ul><li>Master/Master </li></ul><ul><li>$> ./mongod --slave --master --source localhost:10000 </li></ul><ul><li>$> ./mongod --slave --master --dbpath /data/slave --port 10000 --source localhost </li></ul>
  8. 8. <ul><li>$> ./mongod --port 27020 --dbpath /data/1 --master & </li></ul><ul><li>$> ./mongod --port 27021 --dbpath /data/2 --master & </li></ul><ul><li>$> ./mongod --port 27022 --dbpath /data/3 --slave & </li></ul><ul><li>//27022 slave </li></ul><ul><li>> use local </li></ul><ul><li>>db.sources.insert({host:&quot;localhost:27020&quot;}) </li></ul><ul><li>>db.sources.insert({host:&quot;localhost:27021&quot;}) </li></ul>主 n<>1 从
  9. 9. 水平扩展 - sharding
  10. 10. sharding - 1 <ul><li>Shard1 : 27020 # 存储 1 服务器 </li></ul><ul><li>Shard2 : 27021 # 存储 2 服务器 </li></ul><ul><li>Config : 27022 # 路由服务器 </li></ul><ul><li># 启动存储服务器 </li></ul><ul><li>./mongod --dbpath /tmp/mongodata/27020 --port 207020 </li></ul><ul><li>./mongod --dbpath /tmp/mongodata/27021 --port 207021 </li></ul><ul><li># 启动路由服务器 </li></ul><ul><li>./mongod --dbpath / tmp /mongodata/27022 --port 27022 </li></ul><ul><li>./mongos --configdb localhost:27022 </li></ul>
  11. 11. sharding - 2 <ul><li>进路由 客户端 </li></ul><ul><li>$> use admin </li></ul><ul><li>// 加入 shard 节点 </li></ul><ul><li>$> db.runCommand( { addshard : &quot;localhost:27020&quot;, allowLocal : true } ) </li></ul><ul><li>$> db.runCommand( { addshard : &quot;localhost:27021&quot;, allowLocal : true } ) </li></ul><ul><li>$> db.runCommand({listshards:1}); // 查看 shard 节点列表 </li></ul><ul><li>// 新建自动切片的库 user001 </li></ul><ul><li>$> config = connect(&quot;localhost:27022&quot;) </li></ul><ul><li>$> config = config.getSisterDB(&quot;config&quot;) </li></ul><ul><li>$> user001=db.getSisterDB(&quot;user001&quot;); </li></ul><ul><li>$> db.runCommand({enablesharding:&quot;user001&quot;}) </li></ul><ul><li>//user001 中新建表,插入数据 </li></ul><ul><li>$> use user001 ; </li></ul><ul><li>$> db.createCollection(&quot;user_001&quot;) </li></ul><ul><li>$> db.user_001.insert({uid:1,username:&quot;Falcon.C&quot;,sex:&quot;m&quot;,age:25}); </li></ul>
  12. 12. sharding - 3 <ul><li>$> ls -R </li></ul><ul><li>.: </li></ul><ul><li>27020 27021 27022 mongos.log </li></ul><ul><li>./27020: </li></ul><ul><li>27020.log mongod.lock test.0 test.1 test.ns _tmp </li></ul><ul><li>./27020/_tmp: </li></ul><ul><li>./27021: </li></ul><ul><li>27021.log mongod.lock _tmp user.0 user001.0 user001.1 user001.ns user.1 user.ns </li></ul><ul><li>./27021/_tmp: </li></ul><ul><li>./27022: </li></ul><ul><li>27022.log config.0 config.ns mongod.lock mongos.log _tmp </li></ul><ul><li>./27022/_tmp: </li></ul>
  13. 13. sharding - 4 <ul><li>官方 sharding : 参考 </li></ul><ul><li>测试 : 参考 </li></ul>
  14. 14. sharding - MapReduce <ul><li>db.runCommand({ </li></ul><ul><li>mapreduce : <collection>, </li></ul><ul><li>map : <mapfunction>, </li></ul><ul><li>reduce : <reducefunction> </li></ul><ul><li>[, query : <query filter object>] </li></ul><ul><li>[, sort : <sort the query. useful for optimization>] </li></ul><ul><li>[, limit : <number of objects to return from collection>] </li></ul><ul><li>[, out : <output-collection name>] </li></ul><ul><li>[, keeptemp: <true|false>] </li></ul><ul><li>[, finalize : <finalizefunction>] </li></ul><ul><li>[, scope : <object where fields go into javascript global scope >] </li></ul><ul><li>[, verbose : true] }); </li></ul><ul><li>r=function(key,values){ .. } </li></ul><ul><li>m = function(){ .. } </li></ul><ul><li>res=db.things.mapReduce(m,r); </li></ul>
  15. 15. 大量实例 <ul><li>http://github.com/mongodb/mongo/tree/master/jstests/ </li></ul>

×