ユーザ用ツール

サイト用ツール


mongodb:sharding

sharding

サーバ起動

  1. 分散サーバ
    /usr/bin/mongod --shardsvr --port 27017 --logappend --logpath /data/mongo1/logs/mongod.log --pidfilepath=/data/mongo1/logs/mongod.pid --dbpath=/data/mongo1/db --fork
    /usr/bin/mongod --shardsvr --port 27018 --logappend --logpath /data/mongo2/logs/mongod.log --pidfilepath=/data/mongo2/logs/mongod.pid --dbpath=/data/mongo2/db --fork
  2. Configサーバ
    /usr/bin/mongod --configsvr --port 27019 --logappend --logpath /data/mongo3/logs/mongod.log --pidfilepath=/data/mongo3/logs/mongod.pid --dbpath=/data/mongo3/db --fork
  3. Mongosサーバ
    /usr/bin/mongos --configdb localhost:27019 --port 27020 --logappend --logpath /data/mongo4/logs/mongod.log --pidfilepath /data/mongo4/logs/mongod.pid --fork --chunkSize 1

Mongosサーバにログインして、分散設定を行う

# mongo --port 27020 admin
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:27020/admin
mongos> db.runCommand( { addshard : "localhost:27017" } ); 
{ "shardAdded" : "shard0000", "ok" : 1 }

mongos> db.runCommand( { addshard : "localhost:27018" } );
{ "shardAdded" : "shard0001", "ok" : 1 }

mongos> db.printShardingStatus()
--- Sharding Status --- 
 sharding version: { "_id" : 1, "version" : 3 }
 shards:
       {  "_id" : "shard0000",  "host" : "localhost:27017" }
       {  "_id" : "shard0001",  "host" : "localhost:27018" }
 databases:
       {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }

mongos> db.runCommand( { removeshard : "localhost:27017" } ); 
{
       "msg" : "draining started successfully",
       "state" : "started",
       "shard" : "shard0000",
       "ok" : 1
}

mongos> db.printShardingStatus()
--- Sharding Status --- 
 sharding version: { "_id" : 1, "version" : 3 }
 shards:
       {  "_id" : "shard0000",  "draining" : true,  "host" : "localhost:27017" }
       {  "_id" : "shard0001",  "host" : "localhost:27018" }
 databases:
       {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }

mongos> db.runCommand( { removeshard : "localhost:27017" } );
{
       "msg" : "removeshard completed successfully",
       "state" : "completed",
       "shard" : "shard0000",
       "ok" : 1
}

mongos> db.printShardingStatus()
--- Sharding Status --- 
 sharding version: { "_id" : 1, "version" : 3 }
 shards:
       {  "_id" : "shard0001",  "host" : "localhost:27018" }
 databases:
       {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
  • shardtest データベースを分散する
mongos> db.runCommand( { enablesharding : 'shardtest' } ); 
{ "ok" : 1 }
  • shardtest データベースの skey コレクションを name を分散キーとして分散する
mongos> db.runCommand( { shardcollection : 'shardtest.skey', key : { name : 1 } } );
{ "collectionsharded" : "shardtest.skey", "ok" : 1 }
  • 分散定義を確認する
mongos> db.printShardingStatus()
--- Sharding Status --- 
 sharding version: { "_id" : 1, "version" : 3 }
 shards:
       {  "_id" : "shard0001",  "host" : "localhost:27018" }
       {  "_id" : "shard0002",  "host" : "localhost:27017" }
 databases:
       {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
       {  "_id" : "shardtest",  "partitioned" : true,  "primary" : "shard0002" }
               shardtest.skey chunks:
                               shard0002       1
                       { "name" : { $minKey : 1 } } -->> { "name" : { $maxKey : 1 } } on : shard0002 Timestamp(1000, 0) 
  • テストデータを投入してみる
mongos> use shardtest
mongos> for(var i=1;i<=150000;i++) db.skey.save({entryId:i, createdDatetime:new Date(), name:i});
mongos> db.printShardingStatus()
--- Sharding Status --- 
 sharding version: { "_id" : 1, "version" : 3 }
 shards:
       {  "_id" : "shard0001",  "host" : "localhost:27018" }
       {  "_id" : "shard0002",  "host" : "localhost:27017" }
 databases:
       {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
       {  "_id" : "shardtest",  "partitioned" : true,  "primary" : "shard0002" }
               shardtest.skey chunks:
                               shard0001       2
                               shard0002       2
                       { "name" : { $minKey : 1 } } -->> { "name" : "test01" } on : shard0001 Timestamp(3000, 0) 
                       { "name" : "test01" } -->> { "name" : "test03258" } on : shard0002 Timestamp(3000, 1) 
                       { "name" : "test03258" } -->> { "name" : "test0999" } on : shard0002 Timestamp(2000, 3) 
                       { "name" : "test0999" } -->> { "name" : { $maxKey : 1 } } on : shard0001 Timestamp(2000, 0) 

mongos> printShardingSizes()
--- Sharding Status --- 
 sharding version: { "_id" : 1, "version" : 3 }
 shards:
     { "_id" : "shard0001", "host" : "localhost:27018" }
     { "_id" : "shard0002", "host" : "localhost:27017" }
 databases:
       { "_id" : "admin", "partitioned" : false, "primary" : "config" }
       { "_id" : "shardtest", "partitioned" : true, "primary" : "shard0002" }
               shardtest.skey chunks:
                       { "name" : { $minKey : 1 } } -->> { "name" : "test01" } on : shard0001 { "estimate" : false, "size" : 0, "numObjects" : 0 }
                       { "name" : "test01" } -->> { "name" : "test03258" } on : shard0002 { "estimate" : false, "size" : 650928, "numObjects" : 7511 }
                       { "name" : "test03258" } -->> { "name" : "test0999" } on : shard0002 { "estimate" : false, "size" : 628180, "numObjects" : 7478 }
                       { "name" : "test0999" } -->> { "name" : { $maxKey : 1 } } on : shard0001 { "estimate" : false, "size" : 924, "numObjects" : 11 }
  • 分散サーバにログインして状態を確認
# mongo --port 27017 shardtest
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:27017/shardtest
> db.skey.find().count()
14989
# mongo --port 27018 shardtest
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:27018/shardtest
> db.skey.find().count()
11
mongodb/sharding.txt · 最終更新: 2014/02/23 16:31 by clownclown

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki