当前位置:数据库 > DB2 >>

mongodb sharding基本概念

mongodb sharding基本概念
 
这里先介绍sharding的架构和几个基本概念术语。
shard server :shard server可以使一个mongod实例,也可以是replica set。
config sever:为了将指定collection存储在多个shard中,那么就需要个key来进行分割,config server存储各个节点的配置信息。shard key的范围,以及分布情况。
route process:由此介入客户端,通过询问config server,确定到那个shard上面查询,在连接相应的shard操作,不保存数据和配置信息。
由于资源限制,在一台机子上做一下实验
Shard Server 1:30000
Shard Server 2:30001
Config Server :40000
Route Process:50000
步骤:
启动shard server 1和2
[mongo@172_16_3_216 mongo]$ mkdir -p /mongo/shard/data0
[mongo@172_16_3_216 mongo]$ mkdir -p /mongo/shard/data1
[mongo@172_16_3_216 mongo]$ touch shard.log
[mongo@172_16_3_216 mongo]$ mongod --shardsvr --port 30000 --dbpath /mongo/shard/data0 --fork --logpath shard.log --directoryperdb
[mongo@172_16_3_216 mongo]$ touch shard1.log
[mongo@172_16_3_216 mongo]$ mongod --shardsvr --port 30001 --dbpath /mongo/shard/data1 --fork --logpath shard1.log --directoryperdb
启动config server
[mongo@172_16_3_216 mongo]$ mkdir -p /mongo/shard/config
[mongo@172_16_3_216 mongo]$ touch config.log
[mongo@172_16_3_216 mongo]$ mongod --configsvr --port 40000 --dbpath /mongo/shard/config --fork --logpath config.log --directoryperdb
启动route process
[mongo@172_16_3_216 mongo]$ touch route.log
[mongo@172_16_3_216 mongo]$ mongos --port 50000 --configdb localhost:40000 --fork --logpath route.log --chunkSize 2
初始化sharding
mongo admin --port 50000
MongoDB shell version: 1.8.4
connecting to: 127.0.0.1:50000/admin
> db.runCommand({addshard:"localhost:30000"})       ----添加shard1
{ "shardAdded" : "shard0000", "ok" : 1 }
> db.runCommand({addshard:"localhost:30001"})        -----添加shard2
{ "shardAdded" : "shard0001", "ok" : 1 }
> db.runCommand({enablesharding:"test"})            ---对数据库test分片
{ "ok" : 1 }
> db.runCommand({shardcollection:"test.tb1",key:{_id:1}})          ---对数据库test中tb1按_id作为key
{ "collectionsharded" : "test.tb1", "ok" : 1 }
验证sharding
> for (var i=1;i<=50000;i++) db.tb1.insert({age:i,name:"hank",addr:"HangZhou"})
> db.tb1.stats()
{
        "sharded" : true,
        "ns" : "test.tb1",
        "count" : 50000,
        "size" : 3600016,
        "avgObjSize" : 72.00032,
        "storageSize" : 13975552,
        "nindexes" : 1,
        "nchunks" : 4,
        "shards" : {
                "shard0000" : {
                        "ns" : "test.tb1",
                        "count" : 17888,
                        "size" : 1287944,
                        "avgObjSize" : 72.00044722719142,
                        "storageSize" : 2793472,
                        "numExtents" : 5,
                        "nindexes" : 1,
                        "lastExtentSize" : 2097152,
                        "paddingFactor" : 1,
                        "flags" : 1,
                        "totalIndexSize" : 753664,
                        "indexSizes" : {
                                "_id_" : 753664
                        },
                        "ok" : 1
                },
                "shard0001" : {
                        "ns" : "test.tb1",
                        "count" : 32112,
                        "size" : 2312072,
                        "avgObjSize" : 72.00024912805182,
                        "storageSize" : 11182080,
                        "numExtents" : 6,
                        "nindexes" : 1,
                        "lastExtentSize" : 8388608,
                        "paddingFactor" : 1,
                        "flags" : 1,
                        "totalIndexSize" : 1343488,
                        "indexSizes" : {
                                "_id_" : 1343488
                        },
                        "ok" : 1
                }
        },
        "ok" : 1
}
查看sharding信息:
> db.runCommand({listshards:1})
{
        "shards" : [
                {
                    &nb
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,