目次

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" }
mongos> db.runCommand( { enablesharding : 'shardtest' } ); 
{ "ok" : 1 }
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