当前位置: 首页 > mongodb, 分布式系统, 缓存系统 > 正文

MongoDB 自动分片(Auto-Sharding)

1 星2 星3 星4 星5 星 (2 次投票, 评分: 5.00, 总分: 5)
Loading ... Loading ...
baidu_share

启两个shard进程

1
mongod --shardsvr -dbpath=/data/mongodb/shard1/ -port=27020 --logpath=/data/mongodb/logs/shardsvr1.log --logappend --fork
1
mongod --shardsvr -dbpath=/data/mongodb/shard2/ -port=27021 --logpath=/data/mongodb/logs/shardsvr2.log --logappend --fork

启config进程

1
mongod --configsvr --dbpath=/data/mongodb/configdb/ -port=27022 --logpath=/data/mongodb/logs/configsvr.log --logappend --fork

启Mongos进程

1
mongos --configdb 127.0.0.1:27022 -port=27023 --logpath=/data/mongodb/logs/mongos.log --logappend --fork --chunkSize 1

BTW: mongos启动参数中,chunkSize这一项是用来指定chunk的大小的,单位是MB,默认大小为200MB,为了方便测试Sharding效果,把chunkSize指定为 1MB。

登录到mongos,添加Shard节点,为DataBase “mongodbtest”启用Sharding,并将其中的 Collection “mongotest” 的 shard key设置为”{_id: 1}”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@mms ~]# mongo --port 27023
MongoDB shell version: 2.0.3
connecting to: 127.0.0.1:27023/test
mongos> show dbs
config 0.046875GB
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"127.0.0.1:27020"})
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({addshard:"127.0.0.1:27021"})
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> db.runCommand({enablesharding:'mongodbtest'})
{ "ok" : 1 }
mongos> db.runCommand({shardcollection:"mongodbtest.mongotest", key:{_id:1}});
{ "collectionsharded" : "mongodbtest.mongotest", "ok" : 1 }

跑10个进程将100W条数据入库

1
2
3
4
5
6
7
$m = new Mongo('localhost:27023');
$collection = $collection = $m->selectDB('mongodbtest')->selectCollection('mongotest');
for( $i = 0; $i < 100000; $i++ ){
$data = array('title' => 'this is auto-sharding test', 'addtime' => time());
$collection->insert($data);
echo $i . "\n";
}

查看分片结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"sharded" : true,
"flags" : 1,
"ns" : "mongodbtest.mongotest",
"count" : 1000000,
"numExtents" : 19,
"size" : 76000108,
"storageSize" : 113065984,
"totalIndexSize" : 37715888,
"indexSizes" : {
"_id_" : 37715888
},
"avgObjSize" : 76.000108,
"nindexes" : 1,
"nchunks" : 57,
"shards" : {
"shard0000" : {
"ns" : "mongodbtest.mongotest",
"count" : 541898,
"size" : 41184304,
"avgObjSize" : 76.00010334048105,
"storageSize" : 65232896,
"numExtents" : 10,
"nindexes" : 1,
"lastExtentSize" : 17399808,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 21977088,
"indexSizes" : {
"_id_" : 21977088
},
"ok" : 1
},
"shard0001" : {
"ns" : "mongodbtest.mongotest",
"count" : 458102,
"size" : 34815804,
"avgObjSize" : 76.00011351183798,
"storageSize" : 47833088,
"numExtents" : 9,
"nindexes" : 1,
"lastExtentSize" : 14499840,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 15738800,
"indexSizes" : {
"_id_" : 15738800
},
"ok" : 1
}
},
"ok" : 1
}

本文固定链接: http://www.chepoo.com/mongodb-auto-sharding.html | IT技术精华网

MongoDB 自动分片(Auto-Sharding):等您坐沙发呢!

发表评论