mongodb的sharding集群由以下3个服务组成:
Shards Server: 每个shard由一个或多个mongod进程组成,用于存储数据
Config Server: 用于存储集群的Metadata信息,包括每个Shard的信息和chunks信息
Route Server: 用于提供路由服务,由Client连接,使整个Cluster看起来像单个DB服务器
另外,Chunks是指MongoDB中一段连续的数据块,默认大小是200M,一个Chunk位于其中一台Shard服务器上
下面,搭建一个Cluster,它由4台服务器组成,包括3个Shard,3个Config,1个Route
搭建mongodb sharding 配置过程
1 创建数据存放目录
/opt/mongodata/r1
/opt/mongodata/r2
/opt/mongodata/r3
--注意配置顺序
rac1
mkdir -p /opt/mongodata/config1
rac2
mkdir -p /opt/mongodata/config2
rac3
mkdir -p /opt/mongodata/config3
rac4
mkdir -p /opt/mongodata/mongos
2 配置config
rac1
[mongodb@rac1 bin]$ ./mongod --configsvr --dbpath=/opt/mongodata/config1 --port 28001 --logpath=/opt/mongodata/config1/config.log &
[1] 19996
[mongodb@rac1 bin]$ all output going to: /opt/mongodata/config1/config.log
rac2
[mongodb@rac2 bin]$ ./mongod --configsvr --dbpath=/opt/mongodata/config2 --port 28002 --logpath=/opt/mongodata/config2/config.log &
[1] 27223
[mongodb@rac2 bin]$ all output going to: /opt/mongodata/config2/config.log
rac3
[mongodb@rac3 bin]$ ./mongod --configsvr --dbpath=/opt/mongodata/config3 --port 28003 --logpath=/opt/mongodata/config3/config.log &
[1] 31020
[mongodb@rac3 bin]$ all output going to: /opt/mongodata/config3/config.log
3 配置路由设置
rac4
[mongodb@rac4 bin]$ ./mongos --chunkSize 1 --configdb "rac1:28001,rac2:28002,rac3:28003" --logpath /opt/mongodata/mongos/mongos.log &
NOTE:mongos 不需要-dbpath
4 配置sharding 节点
rac1
[mongodb@rac1 bin]$ ./mongod -shardsvr -dbpath=/opt/mongodata/r1 -port 27018 -logpath=/opt/mongodata/r1/27018.log &
rac2
[mongodb@rac2 bin]$ ./mongod -shardsvr -dbpath=/opt/mongodata/r2 -port 27019 -logpath=/opt/mongodata/r2/27019.log &
rac3
[mongodb@rac3 bin]$ ./mongod -shardsvr -dbpath=/opt/mongodata/r3 -port 27020 -logpath=/opt/mongodata/r3/27020.log &
5 在路由服务器上进行添加shard配置:
addshard : 添加 Shard Server,相关的命令还有 listshards 和 removeshard。如果是要添加replica set 的shard 其语法是:db.runCommand({addshard:'replica set 的名字/IP:PORT,[IP:PORT,...]'});
enablesharding : 用于设置可以被分布存储的数据库。
激活指定数据库的分片功能,这样数据库中的不同的collections会被分配到不同的shard上面,然而同一个collection会在同一个shard上面。
shardcollection : 用于设置具体被切块的集合名称,且必须指定 Shard Key,系统会自动创建索引。要进行collection级别的shard,必须执行
db.runCommand( { shardcollection : “”,key : });
namespace : 是collection的名字
shardkeypatternobject:片健,对一个集合进行分片时要设置一个字段或者说键作为拆分的依据。
Note:
1 要进行collection级别的shard 必须先激活数据库级别的shard。
2 Sharded Collection 只能有一个 unique index,且必须是 shard key。
官方文档:a sharded collection can have only one unique index, which must exist on the shard key.NO other unique indexes can exist on the collection.
mongos> db.runCommand({addshard:'10.250.7.225:27018'});
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({addshard:'10.250.7.249:27019'});
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> db.runCommand({addshard:'10.250.7.241:27020'});
{ "shardAdded" : "shard0002", "ok" : 1 }
当然如果是replica set + sharding 架构也可以使用db.runCommand({addshard:'replica set 的名字/IP:PORT,[IP:PORT,...]',name:"shard的名字",maxSize;N});
Name:用于指定每个shard的名字,不指定的话系统将自动分配
maxSize:指定各个shard可使用的最大磁盘空间,单位megabytes
maxSize:指定各个shard可使用的最大磁盘空间,单位megabytes
mongos>
mongos> db.runCommand({"enablesharding": "test"})
{ "ok" : 1 }
mongos> db.runCommand({listshards:1});
{
"shards" : [
{
"_id" : "shard0000",
"host" : "10.250.7.225:27018"
},
{
"_id" : "shard0001",
"host" : "10.250.7.249:27019"
},
{
"_id" : "shard0002",
"host" : "10.250.7.241:27020"
}
],
"ok" : 1
}
mongos> printShardingStatus();
--- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "shard0000", "host" : "10.250.7.225:27018" }
{ "_id" : "shard0001", "host" : "10.250.7.249:27019" }
{ "_id" : "shard0002", "host" : "10.250.7.241:27020" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
mongos> db.runCommand({shardcollection:'test.yql',key:{_id:1}, unique : true});
{ "collectionsharded" : "test.yql", "ok" : 1 }
mongos> use test
switched to db test
mongos>
mongos> db.yql.insert({id:1,val:"this is a message on rac4 mongos !"});
mongos> db.yql.insert({id:2,val:"this is a message on rac4:27020 --2011-11-02 9:47!"});
mongos> db.yql.insert({id:2,val:"this is a message on rac4:27020 --2011-11-02 9:49!"});
mongos> db.yql.insert({id:3,val:"this is a message on rac4:27020 --2011-11-02 9:50!"});
mongos> db.yql.insert({id:4,val:"this is a message on rac4:27020 --2011-11-02 9:52!"});
mongos> db.yql.insert({id:5,val:"this is a message on rac4:27020 --2011-11-02 9:53!"});
mongos> db.yql.insert({id:6,val:"this is a message on rac4:27020 --2011-11-02 9:55!"});
mongos> db.yql.insert({id:7,val:"this is a message on rac4:27020 --2011-11-02 9:56!"});
mongos> db.yql.insert({id:8,val:"this is a message on rac4:27020 --2011-11-02 9:56!"});
mongos> db.yql.insert({id:9,val:"this is a message on rac4:27020 --2011-11-02 9:57!"});
mongos> db.yql.insert({id:10,val:"this is a message on rac4:27020 --2011-11-02 9:58!"});
mongos> db.yql.insert({id:11,val:"this is a message on rac4:27020 --2011-11-02 9:59!"});
mongos> db.yql.insert({id:12,val:"this is a message on rac4:27020 --2011-11-02 10:00!"});
mongos> db.yql.insert({id:13,val:"this is a message on rac4:27020 --2011-11-02 10:01!"});
mongos> db.yql.insert({id:14,val:"this is a message on rac4:27020 --2011-11-02 10:02!"});
mongos> db.yql.insert({id:15,val:"this is a message on rac4:27020 --2011-11-02 10:03!"});
mongos> db.yql.insert({id:16,val:"this is a message on rac4:27020 --2011-11-02 10:04!"});
mongos> db.yql.find();
{ "_id" : ObjectId("4eb298b3adbd9673afee95e3"), "id" : 1, "val" : "this is a message on rac4 mongos !" }
{ "_id" : ObjectId("4eb2995badbd9673afee95e4"), "id" : 2, "val" : "this is a message on rac4:27020 --2011-11-02 9:47!" }
{ "_id" : ObjectId("4eb29962adbd9673afee95e5"), "id" : 2, "val" : "this is a message on rac4:27020 --2011-11-02 9:49!" }
{ "_id" : ObjectId("4eb29970adbd9673afee95e6"), "id" : 3, "val" : "this is a message on rac4:27020 --2011-11-02 9:50!" }
{ "_id" : ObjectId("4eb2997badbd9673afee95e7"), "id" : 4, "val" : "this is a message on rac4:27020 --2011-11-02 9:52!" }
{ "_id" : ObjectId("4eb29985adbd9673afee95e8"), "id" : 5, "val" : "this is a message on rac4:27020 --2011-11-02 9:53!" }
{ "_id" : ObjectId("4eb299eaadbd9673afee95e9"), "id" : 6, "val" : "this is a message on rac4:27020 --2011-11-02 9:55!" }
{ "_id" : ObjectId("4eb299f3adbd9673afee95ea"), "id" : 7, "val" : "this is a message on rac4:27020 --2011-11-02 9:56!" }
{ "_id" : ObjectId("4eb29a59adbd9673afee95eb"), "id" : 8, "val" : "this is a message on rac4:27020 --2011-11-02 9:56!" }
{ "_id" : ObjectId("4eb29a59adbd9673afee95ec"), "id" : 9, "val" : "this is a message on rac4:27020 --2011-11-02 9:57!" }
{ "_id" : ObjectId("4eb29a59adbd9673afee95ed"), "id" : 10, "val" : "this is a message on rac4:27020 --2011-11-02 9:58!" }
{ "_id" : ObjectId("4eb29a59adbd9673afee95ee"), "id" : 11, "val" : "this is a message on rac4:27020 --2011-11-02 9:59!" }
{ "_id" : ObjectId("4eb29a59adbd9673afee95ef"), "id" : 12, "val" : "this is a message on rac4:27020 --2011-11-02 10:00!" }
{ "_id" : ObjectId("4eb29a59adbd9673afee95f0"), "id" : 13, "val" : "this is a message on rac4:27020 --2011-11-02 10:01!" }
{ "_id" : ObjectId("4eb29a59adbd9673afee95f1"), "id" : 14, "val" : "this is a message on rac4:27020 --2011-11-02 10:02!" }
{ "_id" : ObjectId("4eb29a59adbd9673afee95f2"), "id" : 15, "val" : "this is a message on rac4:27020 --2011-11-02 10:03!" }
{ "_id" : ObjectId("4eb29a59adbd9673afee95f3"), "id" : 16, "val" : "this is a message on rac4:27020 --2011-11-02 10:04!" }
用 db..stats() 可以查看具体的 Shard 存储信息
mongos> db.yql.stats();
{
"sharded" : true,
"flags" : 1,
"ns" : "test.yql",
"count" : 17,
"numExtents" : 1,
"size" : 1616,
"storageSize" : 8192,
"totalIndexSize" : 8176,
"indexSizes" : {
"_id_" : 8176
},
"avgObjSize" : 95.05882352941177,
"nindexes" : 1,
"nchunks" : 1,
"shards" : {
"shard0000" : { --表示yql被拆分到了
10.250.7.225这台机器上了
"ns" : "test.yql",
"count" : 17,
"size" : 1616,
"avgObjSize" : 95.05882352941177,
"storageSize" : 8192,
"numExtents" : 1,
"nindexes" : 1,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"flags" : 1,
"totalIndexSize" : 8176,
"indexSizes" : {
"_id_" : 8176
},
"ok" : 1
}
},
"ok" : 1
}
mongos>