mongodb/replicasetを試してみた

replicasetは冗長化、shardingは負荷分散を実現するための仕組みとのこと。

今回はreplicasetを試してみた。

mongod replicaの起動

-> % mkdir -p tmp/r1 tmp/r2 tmp/r3
-> % mongod --dbpath tmp/r1 --port 27018 --logpath tmp/r1.log --fork --replSet ReplicaSet001
about to fork child process, waiting until server is ready for connections.
forked process: 91617
all output going to: /Users/kanetann/project/sandbox/mongo-sample/tmp/r1.log
child process started successfully, parent exiting
-> % mongod --dbpath tmp/r2 --port 27019 --logpath tmp/r2.log --fork --replSet ReplicaSet001
about to fork child process, waiting until server is ready for connections.
forked process: 91637
all output going to: /Users/kanetann/project/sandbox/mongo-sample/tmp/r2.log
child process started successfully, parent exiting
-> % mongod --dbpath tmp/r3 --port 27020 --logpath tmp/r3.log --fork --replSet ReplicaSet001
about to fork child process, waiting until server is ready for connections.
forked process: 91657
all output going to: /Users/kanetann/project/sandbox/mongo-sample/tmp/r3.log
child process started successfully, parent exiting

mongo shellからinitiate

-> % mongo --port 27018
MongoDB shell version: 2.4.4
connecting to: 127.0.0.1:27018/test
Server has startup warnings:
Mon Jul  1 18:39:13.849 [initandlisten]
Mon Jul  1 18:39:13.849 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
ReplicaSet001:SECONDARY> var config = {
...     "_id" : "ReplicaSet001",
...     "version" : 1,
...     "members" : [
...         {
...             "_id" : 0,
...             "host" : "localhost:27018",
...         },
...         {
...             "_id" : 1,
...             "host" : "localhost:27019",
...         },
...         {
...             "_id" : 2,
...             "host" : "localhost:27020",
...         },
...     ]
... }
ReplicaSet001:PRIMARY> rs.initiate(config)
{
    "info" : "try querying local.system.replset to see current configuration",
    "ok" : 0,
    "errmsg" : "already initialized"
}

rs.status()の確認

ReplicaSet001:PRIMARY> rs.status()
{
    "set" : "ReplicaSet001",
    "date" : ISODate("2013-07-01T09:39:42Z"),
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "localhost:27018",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 29,
            "optime" : {
                "t" : 1372671316,
                "i" : 1
            },
            "optimeDate" : ISODate("2013-07-01T09:35:16Z"),
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "localhost:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 27,
            "optime" : {
                "t" : 1372671316,
                "i" : 1
            },
            "optimeDate" : ISODate("2013-07-01T09:35:16Z"),
            "lastHeartbeat" : ISODate("2013-07-01T09:39:41Z"),
            "lastHeartbeatRecv" : ISODate("2013-07-01T09:39:42Z"),
            "pingMs" : 0,
            "lastHeartbeatMessage" : "syncing to: localhost:27018",
            "syncingTo" : "localhost:27018"
        },
        {
            "_id" : 2,
            "name" : "localhost:27020",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 27,
            "optime" : {
                "t" : 1372671316,
                "i" : 1
            },
            "optimeDate" : ISODate("2013-07-01T09:35:16Z"),
            "lastHeartbeat" : ISODate("2013-07-01T09:39:41Z"),
            "lastHeartbeatRecv" : ISODate("2013-07-01T09:39:42Z"),
            "pingMs" : 0,
            "lastHeartbeatMessage" : "syncing to: localhost:27018",
            "syncingTo" : "localhost:27018"
        }
    ],
    "ok" : 1
}
ReplicaSet001:PRIMARY> exit
bye

primary(pidport:27018)を落としてみる

-> % ps ax | grep mongod
91617   ??  S      0:00.90 /usr/local/Cellar/mongodb/2.4.4-x86_64/mongod --dbpath tmp/r1 --port 27018 --logpath tmp/r1.log --fork --replSet ReplicaSet001 --config /usr/local/etc/mongod.conf
91637   ??  S      0:00.86 /usr/local/Cellar/mongodb/2.4.4-x86_64/mongod --dbpath tmp/r2 --port 27019 --logpath tmp/r2.log --fork --replSet ReplicaSet001 --config /usr/local/etc/mongod.conf
91657   ??  S      0:00.86 /usr/local/Cellar/mongodb/2.4.4-x86_64/mongod --dbpath tmp/r3 --port 27020 --logpath tmp/r3.log --fork --replSet ReplicaSet001 --config /usr/local/etc/mongod.conf
91666 s003  R+     0:00.00 grep mongod
-> % kill 91617
-> % ps ax | grep mongod
91637   ??  S      0:01.15 /usr/local/Cellar/mongodb/2.4.4-x86_64/mongod --dbpath tmp/r2 --port 27019 --logpath tmp/r2.log --fork --replSet ReplicaSet001 --config /usr/local/etc/mongod.conf
91657   ??  S      0:01.15 /usr/local/Cellar/mongodb/2.4.4-x86_64/mongod --dbpath tmp/r3 --port 27020 --logpath tmp/r3.log --fork --replSet ReplicaSet001 --config /usr/local/etc/mongod.conf
91674 s003  R+     0:00.00 grep mongod
-> % echo "rs.status()" | mongo --port 27019
MongoDB shell version: 2.4.4
connecting to: 127.0.0.1:27019/test
{
    "set" : "ReplicaSet001",
    "date" : ISODate("2013-07-01T09:41:13Z"),
    "myState" : 2,
    "syncingTo" : "localhost:27020",
    "members" : [
        {
            "_id" : 0,
            "name" : "localhost:27018",
            "health" : 0,
            "state" : 8,
            "stateStr" : "(not reachable/healthy)",
            "uptime" : 0,
            "optime" : {
                "t" : 1372671316,
                "i" : 1
            },
            "optimeDate" : ISODate("2013-07-01T09:35:16Z"),
            "lastHeartbeat" : ISODate("2013-07-01T09:41:12Z"),
            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
            "pingMs" : 0
        },
        {
            "_id" : 1,
            "name" : "localhost:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 119,
            "optime" : {
                "t" : 1372671316,
                "i" : 1
            },
            "optimeDate" : ISODate("2013-07-01T09:35:16Z"),
            "errmsg" : "syncing to: localhost:27020",
            "self" : true
        },
        {
            "_id" : 2,
            "name" : "localhost:27020",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 117,
            "optime" : {
                "t" : 1372671316,
                "i" : 1
            },
            "optimeDate" : ISODate("2013-07-01T09:35:16Z"),
            "lastHeartbeat" : ISODate("2013-07-01T09:41:12Z"),
            "lastHeartbeatRecv" : ISODate("2013-07-01T09:41:12Z"),
            "pingMs" : 0,
            "syncingTo" : "localhost:27018"
        }
    ],
    "ok" : 1
}
bye

元primary(pidport:27018)を起動してみる

-> % mongod --dbpath tmp/r1 --port 27018 --logpath tmp/r1.log --fork --replSet ReplicaSet001
about to fork child process, waiting until server is ready for connections.
forked process: 91699
all output going to: /Users/kanetann/project/sandbox/mongo-sample/tmp/r1.log
child process started successfully, parent exiting
-> % echo "rs.status()" | mongo --port 27019
MongoDB shell version: 2.4.4
connecting to: 127.0.0.1:27019/test
{
    "set" : "ReplicaSet001",
    "date" : ISODate("2013-07-01T09:42:09Z"),
    "myState" : 2,
    "syncingTo" : "localhost:27020",
    "members" : [
        {
            "_id" : 0,
            "name" : "localhost:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1,
            "optime" : {
                "t" : 1372671316,
                "i" : 1
            },
            "optimeDate" : ISODate("2013-07-01T09:35:16Z"),
            "lastHeartbeat" : ISODate("2013-07-01T09:42:08Z"),
            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
            "pingMs" : 0
        },
        {
            "_id" : 1,
            "name" : "localhost:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 175,
            "optime" : {
                "t" : 1372671316,
                "i" : 1
            },
            "optimeDate" : ISODate("2013-07-01T09:35:16Z"),
            "errmsg" : "syncing to: localhost:27020",
            "self" : true
        },
        {
            "_id" : 2,
            "name" : "localhost:27020",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 173,
            "optime" : {
                "t" : 1372671316,
                "i" : 1
            },
            "optimeDate" : ISODate("2013-07-01T09:35:16Z"),
            "lastHeartbeat" : ISODate("2013-07-01T09:42:08Z"),
            "lastHeartbeatRecv" : ISODate("2013-07-01T09:42:08Z"),
            "pingMs" : 0,
            "syncingTo" : "localhost:27018"
        }
    ],
    "ok" : 1
}
bye

元primaryはsecondaryのままです。

mongoidの前にmongodbの基本を抑えておきたい感じです。

次はshardingを試してみます。