2012年2月4日土曜日

MongoDB 環境構築(その3: ReplicaSet + Sharding)。

先日Tokyo.R第20回勉強会に参加してきた。
西新宿のNifty新社屋での開催で自宅から近かったのでちょっと見に行ったのだが思ったよりずっと面白くて今後も継続して参加する予定。外に出て人の話を聞くのは新鮮で気持ちが引き締まるし勉強のモチベーション向上に良いと思った。


今回は前回前々回の内容を合わせた ReplicaSet + Sharding 環境の構築を行う。
例のごとく複数台のサーバを用意する余裕(お金)は無いので1台のPCの複数ポートを用いる事で擬似的な分散サーバ環境を実現。


今回の目標: Sharding を行いつつ

  • mongod11 & mongod12 & arbiter13
  • mongod21 & mongod22 & arbiter23

という2組の ReplicaSet を構成。


config サーバは3台で冗長構成を実現する。
*config は1 or 3台でしか構成できないっぽい
*実際に2台で冗長構成を構築しようとしたら怒られた
$ mongos --help
…
Sharding options:
  --configdb arg        1 or 3 comma separated config servers
…

$ mongos --configdb localhost:10031,localhost:10032
Sat Feb  4 03:59:55 need either 1 or 3 configdbs

サーバを起動
// Repl01
$ mongod --replSet repl01 --shardsvr --port 10011 --dbpath data/shrd11
$ mongod --replSet repl01 --shardsvr --port 10012 --dbpath data/shrd12
$ mongod --replSet repl01 --port 10013 --dbpath data/arb1

// Repl02
$ mongod --replSet repl02 --shardsvr --port 10021 --dbpath data/shrd21
$ mongod --replSet repl02 --shardsvr --port 10022 --dbpath data/shrd22
$ mongod --replSet repl02 --port 10023 --dbpath data/arb2

// config
$ mongod --configsvr --port 10031 --dbpath data/conf1
$ mongod --configsvr --port 10032 --dbpath data/conf2
$ mongod --configsvr --port 10033 --dbpath data/conf3

// mongos
// config 3つをカンマ区切りで指定
$ mongos --configdb localhost:10031,localhost:10032,localhost:10033 --port 10000

mongod11 に接続して Repl01 の設定を実施。
$ mongo localhost:10011
MongoDB shell version: 2.0.2
connecting to: localhost:10011/test

> config = {_id: 'repl01', members: [{_id: 0, host: 'localhost:10011'}, {_id: 1, host: 'localhost:10012'}]};
{
 "_id" : "repl01",
 "members" : [
  {
   "_id" : 0,
   "host" : "localhost:10011"
  },
  {
   "_id" : 1,
   "host" : "localhost:10012"
  }
 ]
}
> rs.initiate(config)
{
 "info" : "Config now saved locally.  Should come online in about a minute.",
 "ok" : 1
}

PRIMARY> rs.addArb('localhost:10013')
{ "ok" : 1 }

PRIMARY> rs.status()
{
 "set" : "repl01",
 "date" : ISODate("2012-02-03T19:28:22Z"),
 "myState" : 1,
 "members" : [
  {
   "_id" : 0,
   "name" : "localhost:10011",
   "health" : 1,
   "state" : 1,
   "stateStr" : "PRIMARY",
   "optime" : {
    "t" : 1328297270000,
    "i" : 1
   },
   "optimeDate" : ISODate("2012-02-03T19:27:50Z"),
   "self" : true
  },
  {
   "_id" : 1,
   "name" : "localhost:10012",
   "health" : 1,
   "state" : 2,
   "stateStr" : "SECONDARY",
   "uptime" : 122,
   "optime" : {
    "t" : 1328297270000,
    "i" : 1
   },
   "optimeDate" : ISODate("2012-02-03T19:27:50Z"),
   "lastHeartbeat" : ISODate("2012-02-03T19:28:20Z"),
   "pingMs" : 988
  },
  {
   "_id" : 2,
   "name" : "localhost:10013",
   "health" : 1,
   "state" : 7,
   "stateStr" : "ARBITER",
   "uptime" : 32,
   "optime" : {
    "t" : 0,
    "i" : 0
   },
   "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
   "lastHeartbeat" : ISODate("2012-02-03T19:28:20Z"),
   "pingMs" : 0
  }
 ],
 "ok" : 1
}

mongod21 に接続して Repl02 の設定を実施。
$ mongo localhost:10021
MongoDB shell version: 2.0.2
connecting to: localhost:10021/test

> config = {_id: 'repl02', members: [{_id: 0, host: 'localhost:10021'}, {_id: 1, host: 'localhost:10022'}]};
{
 "_id" : "repl02",
 "members" : [
  {
   "_id" : 0,
   "host" : "localhost:10021"
  },
  {
   "_id" : 1,
   "host" : "localhost:10022"
  }
 ]
}
> rs.initiate(config)
{
 "info" : "Config now saved locally.  Should come online in about a minute.",
 "ok" : 1
}

> rs.addArb('localhost:10023')
{ "ok" : 1 }

PRIMARY> rs.status()
{
 "set" : "repl02",
 "date" : ISODate("2012-02-03T19:33:59Z"),
 "myState" : 1,
 "members" : [
  {
   "_id" : 0,
   "name" : "localhost:10021",
   "health" : 1,
   "state" : 1,
   "stateStr" : "PRIMARY",
   "optime" : {
    "t" : 1328297619000,
    "i" : 1
   },
   "optimeDate" : ISODate("2012-02-03T19:33:39Z"),
   "self" : true
  },
  {
   "_id" : 1,
   "name" : "localhost:10022",
   "health" : 1,
   "state" : 2,
   "stateStr" : "SECONDARY",
   "uptime" : 36,
   "optime" : {
    "t" : 1328297619000,
    "i" : 1
   },
   "optimeDate" : ISODate("2012-02-03T19:33:39Z"),
   "lastHeartbeat" : ISODate("2012-02-03T19:33:59Z"),
   "pingMs" : 559423
  },
  {
   "_id" : 2,
   "name" : "localhost:10023",
   "health" : 1,
   "state" : 7,
   "stateStr" : "ARBITER",
   "uptime" : 20,
   "optime" : {
    "t" : 0,
    "i" : 0
   },
   "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
   "lastHeartbeat" : ISODate("2012-02-03T19:33:59Z"),
   "pingMs" : 0
  }
 ],
 "ok" : 1
}

mongos に接続して sharding の設定を実施。
$ mongo localhost:10000
MongoDB shell version: 2.0.2
connecting to: localhost:10000/test

mongos> use admin
switched to db admin

// Repl01 と Repl02 を Sharding 対象に指定
// *RepicalSet名/db[,db]
mongos> db.runCommand({addshard: 'repl01/localhost:10011,localhost:10012', name: 'shard01'})
{ "shardAdded" : "shard01", "ok" : 1 }
mongos> db.runCommand({addshard: 'repl02/localhost:10021,localhost:10022', name: 'shard02'})
{ "shardAdded" : "shard02", "ok" : 1 }

mongos> db.runCommand({listshards: 1})
{
 "shards" : [
  {
   "_id" : "shard01",
   "host" : "repl01/localhost:10011,localhost:10012"
  },
  {
   "_id" : "shard02",
   "host" : "repl02/localhost:10021,localhost:10022"
  }
 ],
 "ok" : 1
}

mongos> db.runCommand({enablesharding: 'testdb'})
{ "ok" : 1 }
mongos> db.runCommand({shardcollection: 'testdb.logs', key: {ymd: 1, uid: 1}})
{ "collectionsharded" : "testdb.logs", "ok" : 1 }

mongos> db.printShardingStatus()
--- Sharding Status --- 
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
 {  "_id" : "shard01",  "host" : "repl01/localhost:10011,localhost:10012" }
 {  "_id" : "shard02",  "host" : "repl02/localhost:10021,localhost:10022" }
  databases:
 {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
 {  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard01" }
  testdb.logs chunks:
    shard01 1
   { "ymd" : { $minKey : 1 }, "uid" : { $minKey : 1 } } -->> { "ymd" : { $maxKey : 1 }, "uid" : { $maxKey : 1 } } on : shard01 { "t" : 1000, "i" : 0 }

おわり。

0 件のコメント:

コメントを投稿