MongoDB ReplicaSetを試した

#環境
ホストマシーン：mac
検証環境はゲストOS、vagrant上。
MongoDB 2.6.3

#ReplicaSet検証
## 1.mongod を立ち上げる

```
% mongod --replSet set01 --shardsvr --port 10001 --dbpath /data/mongodb/rs01 --logpath /var/log/mongodb/rs01.log --logappend --pidfilepath /data/mongodb/rs01.pid --directoryperdb --fork --rest --nojournal

% mongod --replSet set01 --shardsvr --port 10002 --dbpath /data/mongodb/rs02 --logpath /var/log/mongodb/rs02.log --logappend --pidfilepath /data/mongodb/rs02.pid --directoryperdb --fork --rest --nojournal

% mongod --replSet set01 --shardsvr --port 10003 --dbpath /data/mongodb/rs03 --logpath /var/log/mongodb/rs03.log --logappend --pidfilepath /data/mongodb/rs03.pid --directoryperdb --fork --rest --nojournal

```

mongodを立ち上げると2GBのjournalファイルが出来上がるが、
vagrant上で容量不足で立ち上げられなくなるので
--nojournal オプションをつける。

## 2. masterとするポートに接続する

```
mongo --port=10001
```

## 3. configを設定する

```
> config = {_id:'set01',members:[{_id:0, host:'localhost:10001'},{_id:1, host:'localhost:10002'},{_id:2, host:'localhost:10003'}]}
```

## 4. 設定したconfigで初期化と確認

```
> rs.initiate(config)
```

実行結果


> {
>  "info" : "Config now saved locally.  Should come online in > about a minute.",
>  "ok" : 1
> }


ステータスを確認する

```
> set01:PRIMARY> rs.status()
```

実行結果

> {
  "set" : "set01",
  "date" : ISODate("2014-07-08T07:48:35Z"),
  "myState" : 1,
  "members" : [
    {
      "_id" : 0,
      "name" : "localhost:10001",
      "health" : 1,
      "state" : 1,
      "stateStr" : "PRIMARY",
      "uptime" : 2737,
      "optime" : Timestamp(1404805494, 10),
      "optimeDate" : ISODate("2014-07-08T07:44:54Z"),
      "electionTime" : Timestamp(1404803208, 1),
      "electionDate" : ISODate("2014-07-08T07:06:48Z"),
      "self" : true
    },
    {
      "_id" : 1,
      "name" : "localhost:10002",
      "health" : 1,
      "state" : 2,
      "stateStr" : "SECONDARY",
      "uptime" : 2515,
      "optime" : Timestamp(1404805494, 10),
      "optimeDate" : ISODate("2014-07-08T07:44:54Z"),
      "lastHeartbeat" : ISODate("2014-07-08T07:48:34Z"),
      "lastHeartbeatRecv" : ISODate("2014-07-08T07:48:34Z"),
      "pingMs" : 0,
      "syncingTo" : "localhost:10001"
    },
    {
      "_id" : 2,
      "name" : "localhost:10003",
      "health" : 1,
      "state" : 5,
      "stateStr" : "STARTUP2",
      "uptime" : 2515,
      "optime" : Timestamp(0, 0),
      "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
      "lastHeartbeat" : ISODate("2014-07-08T07:48:34Z"),
      "lastHeartbeatRecv" : ISODate("2014-07-08T07:48:34Z"),
      "pingMs" : 0,
      "lastHeartbeatMessage" : "syncThread: 14031 Can't take a write lock while out of disk space"
    }
  ],
  "ok" : 1
}


## mongoose + node.jsでデータ投入


mongoose/examples/replicasets/replica-sets.jsを編集

```
var opts = {
  replSet : { rs_name : "rs0" }
};

mongoose.connect('mongodb://127.0.0.1:10001/person,127.0.0.1:10002,127.0.0.1:10003', opts, function (err) {
  ~省略
    prom.then(function (people) {
        console.log("young people: %s", people);
    });//.then(cleanup); ここコメントアウト

});
```

## データが入っているか確認

Slaveへ接続

```
% mongo --port=10002

MongoDB shell version: 2.6.3
connecting to: 127.0.0.1:10002/test
Server has startup warnings: 
2014-07-08T07:04:54.045+0000 ** WARNING: --rest is specified without --httpinterface,
2014-07-08T07:04:54.045+0000 **          enabling http interface

```

データ確認

```
set01:SECONDARY> show databases;
admin   (empty)
local   1.031GB
person  0.078GB

set01:SECONDARY> use person
switched to db person

set01:SECONDARY> show collections
2014-07-08T07:51:18.438+0000 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131
```

slaveで参照できるようにする

```
set01:SECONDARY> db.getMongo().setSlaveOk()
```

データ確認する

```
set01:SECONDARY> use person
switched to db person
set01:SECONDARY> show collections
people
system.indexes
set01:SECONDARY> db.people.find()

{ "_id" : ObjectId("53bba580653ebd8b4bf35eba"), "name" : "bill", "age" : 25, "birthday" : ISODate("1989-07-08T08:02:08.637Z"), "__v" : 0 }
{ "_id" : ObjectId("53bba580653ebd8b4bf35ebb"), "name" : "mary", "age" : 30, "birthday" : ISODate("1984-07-08T08:02:08.637Z"), "__v" : 0 }
{ "_id" : ObjectId("53bba580653ebd8b4bf35ebc"), "name" : "bob", "age" : 21, "birthday" : ISODate("1993-07-08T08:02:08.637Z"), "__v" : 0 }
{ "_id" : ObjectId("53bba580653ebd8b4bf35ebd"), "name" : "lilly", "age" : 26, "birthday" : ISODate("1988-07-08T08:02:08.637Z"), "__v" : 0 }
{ "_id" : ObjectId("53bba580653ebd8b4bf35ebe"), "name" : "alucard", "age" : 1000, "birthday" : ISODate("1014-07-08T08:02:08.637Z"), "__v" : 0 }
```