【Mongodb】Sharding 叢集配置

楊奇龍發表於2011-11-03
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
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> 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-710195/,如需轉載,請註明出處,否則將追究法律責任。

相關文章