mongodbでsharding
レプリカセット作るのは、簡単なのに、
shardingが割と大変なので、メモしておこう。。。
mac os x lion(じゃなくてもいいけど)に
mongodb2.0.4入れてる前提。(2.x系なら大きく変わらないかと。)
db入れるディレクトリを準備
mkdir -p /data/db/shard1
mkdir -p /data/db/shard2
mkdir -p /data/db/shard3
mkdir -p /data/db/shardc
mongodを3台
mongod --fork --port 28001 --logpath /data/db/shard1/server.out --logappend --dbpath /data/db/shard1 --shardsvr
mongod --fork --port 28002 --logpath /data/db/shard2/server.out --logappend --dbpath /data/db/shard2 --shardsvr
mongod --fork --port 28003 --logpath /data/db/shard3/server.out --logappend --dbpath /data/db/shard3 --shardsvr
config serverとmongos
mongod --fork --port 28004 --logpath /data/db/shardc/server.out --logappend --dbpath /data/db/shardc --configsvr
mongos --fork --port 28005 --logpath /data/db/mongos.log --logappend --configdb localhost:28004
mongosだけコマンドがmongodでなくて、mongosなので、注意。
ここまでやると、こんな感じになってるはず。
$ ps aux | grep mongo
mongosが1個
mongodが4個(shardsvrが3台、configsvrが1台)
ここからが色々はまる所。
やらなくてはいけないのは。
mongosのadminディレクトリに繋いで、shardingのサーバーを追加する必要がある。
$ mongo localhost:28005/admin
MongoDB shell version: 2.0.4
connecting to: localhost:28005/admin
mongos> db
admin
mongos> db.runCommand({addshard:"localhost:28001"});
{
"ok" : 0,
"errmsg" : "can't add shard localhost:28001 because a local database 'test' exists in another shard0000:localhost:28001"
}
俺は、まずコレが出た。
なんやねん。
どうやら、どこかのタイミングでtestデータベースが出来てしまったので、削除する必要がある
$ mongo localhost:28001
MongoDB shell version: 2.0.4
connecting to: localhost:28001/test
> show dbs
local (empty)
test 0.203125GB> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }> show dbs
local (empty)
test 0.203125GB
それでは、改めて
$ mongo localhost:28005/admin
MongoDB shell version: 2.0.4
connecting to: localhost:28005/admin
mongos> sh.addShard('localhost:28001')
mongos> sh.addShard('localhost:28002')
mongos> sh.addShard('localhost:28003')
これで、sharding終了
テストデータ入れてみる
mongos> sh.enableSharding('test')
mongos> sh.shardCollection('test.ids', {_id:1},true)mongos> for(i=1;i<100000;i++){db.ids.insert({name:"name-" + i})}
mongos> db.ids.find().count()
100000
さて、動作を確認しよう。
mongos> db.printShardingStatus();
- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "shard0000", "host" : "localhost:28001" }
{ "_id" : "shard0001", "host" : "localhost:28002" }
{ "_id" : "shard0002", "host" : "localhost:28003" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
test.ids chunks:
shard0000 2
shard0001 1
{ "_id" : { $minKey : 1 } } -->> { "_id" : ObjectId("50c7340d9c9b8e5fd82edfb4") } on : shard0000 { "t" : 2000, "i" : 1 }
{ "_id" : ObjectId("50c7340d9c9b8e5fd82edfb4") } -->> { "_id" : ObjectId("50c734439c9b8e5fd82f143f") } on : shard0000 { "t" : 1000, "i" : 3 }
{ "_id" : ObjectId("50c734439c9b8e5fd82f143f") } -->> { "_id" : { $maxKey : 1 } } on : shard0001 { "t" : 2000, "i" : 0 }
shard_0とshard_1で分かれているっぽい
ていうか、shard2が何故か、無い。なんだっけ?
mongos> sh.setBalancerState(true)
mongos> db.printShardingStatus();
- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "shard0000", "host" : "localhost:28001" }
{ "_id" : "shard0001", "host" : "localhost:28002" }
{ "_id" : "shard0002", "host" : "localhost:28003" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
test.ids chunks:
shard0000 2
shard0001 1
{ "_id" : { $minKey : 1 } } -->> { "_id" : ObjectId("50c7340d9c9b8e5fd82edfb4") } on : shard0000 { "t" : 2000, "i" : 1 }
{ "_id" : ObjectId("50c7340d9c9b8e5fd82edfb4") } -->> { "_id" : ObjectId("50c734439c9b8e5fd82f143f") } on : shard0000 { "t" : 1000, "i" : 3 }
{ "_id" : ObjectId("50c734439c9b8e5fd82f143f") } -->> { "_id" : { $maxKey : 1 } } on : shard0001 { "t" : 2000, "i" : 0 }
かわんねー
サイズが小さいから、まだ、shard2に振り分ける必要がないのかな?
もしかしたら、chunksizeを小さくすると、違うかも。
今日はこのぐらいにしておこう。
mongos> db.ids.drop()
true
mongos> db.ids.find().count()
0