カイワレの大冒険 Third

技術的なことや他愛もないことをたまに書いてます

MongoDB2.4系で、セカンダリのノードをパッシブにしてみた

MongoDBのバージョンは2.4.10。セカンダリになってるやつをパッシブにしてみるにはどうしたらいいかわからなかったので、試してみた。

まず起動。

mkdir -p /data/{mongo10101,mongo10102,mongo10103,mongo10104,config,logs}

# mongod
/usr/local/mongodb/bin/mongod --port 10101 --replSet=test-bk-repl --dbpath /data/mongo10101 --logpath /data/logs/mongo10101.log --fork
/usr/local/mongodb/bin/mongod --port 10102 --replSet=test-bk-repl --dbpath /data/mongo10102 --logpath /data/logs/mongo10102.log --fork
/usr/local/mongodb/bin/mongod --port 10103 --replSet=test-bk-repl --dbpath /data/mongo10103 --logpath /data/logs/mongo10103.log --fork
/usr/local/mongodb/bin/mongod --port 10104 --replSet=test-bk-repl --dbpath /data/mongo10104 --logpath /data/logs/mongo10104.log --fork

# mongoc
/usr/local/mongodb/bin/mongod --configsvr --port 10105 --dbpath /data/config --logpath /data/logs/config.log --fork

# mongos
/usr/local/mongodb/bin/mongos --configdb localhost:10105 --port 10100 --logpath /data/logs/mongos.log --chunkSize 1 --fork

レプリケーションの設定。

config = {
  _id : "test-bk-repl",
  members : [
    { _id : 0, host : "localhost:10101", votes: 1 },
    { _id : 1, host : "localhost:10102", votes: 1 },
    { _id : 2, host : "localhost:10103", votes: 0 },
    { _id : 3, host : "localhost:10104", votes: 1, arbiterOnly : true } ] }

> rs.initiate(config)

ひと通りレプリカセットの状態確認。db.isMaster()でパッシブにはどのプロセスもなってないことを確認する。

test-bk-repl:PRIMARY> rs.status()
{
        "set" : "test-bk-repl",
        "date" : ISODate("2014-04-21T09:22:45Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:10101",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 166,
                        "optime" : Timestamp(1398072042, 1),
                        "optimeDate" : ISODate("2014-04-21T09:20:42Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:10102",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 116,
                        "optime" : Timestamp(1398072042, 1),
                        "optimeDate" : ISODate("2014-04-21T09:20:42Z"),
                        "lastHeartbeat" : ISODate("2014-04-21T09:22:43Z"),
                        "lastHeartbeatRecv" : ISODate("2014-04-21T09:22:43Z"),
                        "pingMs" : 0,
                        "syncingTo" : "localhost:10101"
                },
                {
                        "_id" : 2,
                        "name" : "localhost:10103",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 116,
                        "optime" : Timestamp(1398072042, 1),
                        "optimeDate" : ISODate("2014-04-21T09:20:42Z"),
                        "lastHeartbeat" : ISODate("2014-04-21T09:22:43Z"),
                        "lastHeartbeatRecv" : ISODate("2014-04-21T09:22:43Z"),
                        "pingMs" : 0,
                        "syncingTo" : "localhost:10101"
                },
                {
                        "_id" : 3,
                        "name" : "localhost:10104",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 116,
                        "lastHeartbeat" : ISODate("2014-04-21T09:22:43Z"),
                        "lastHeartbeatRecv" : ISODate("2014-04-21T09:22:44Z"),
                        "pingMs" : 0
                }
        ],
        "ok" : 1
}


test-bk-repl:PRIMARY> rs.config()
{
        "_id" : "test-bk-repl",
        "version" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:10101"
                },
                {
                        "_id" : 1,
                        "host" : "localhost:10102"
                },
                {
                        "_id" : 2,
                        "host" : "localhost:10103",
                        "votes" : 0
                },
                {
                        "_id" : 3,
                        "host" : "localhost:10104",
                        "arbiterOnly" : true
                }
        ]
}

test-bk-repl:PRIMARY> db.isMaster()
{
        "setName" : "test-bk-repl",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "localhost:10101",
                "localhost:10103",
                "localhost:10102"
        ],
        "arbiters" : [
                "localhost:10104"
        ],
        "primary" : "localhost:10101",
        "me" : "localhost:10101",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2014-04-21T09:22:59.025Z"),
        "ok" : 1
}

rs.config()の内容を変数に入れる。

test-bk-repl:PRIMARY> var config = rs.config()
test-bk-repl:PRIMARY> config
{
        "_id" : "test-bk-repl",
        "version" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:10101"
                },
                {
                        "_id" : 1,
                        "host" : "localhost:10102"
                },
                {
                        "_id" : 2,
                        "host" : "localhost:10103",
                        "votes" : 0
                },
                {
                        "_id" : 3,
                        "host" : "localhost:10104",
                        "arbiterOnly" : true
                }
        ]
}

レプリカセットに対して設定できる変数は色々あるのだけれど、パッシブにしたいので今回はpriorityを使う。 votesが0のやつにさらにpriority:0を与えて、reconfigして設定を再反映する。 reconfigするとRSとの通信はすべて遮断される。

test-bk-repl:PRIMARY> config.members[2].priority = 0
0
test-bk-repl:PRIMARY> config
{
        "_id" : "test-bk-repl",
        "version" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:10101"
                },
                {
                        "_id" : 1,
                        "host" : "localhost:10102"
                },
                {
                        "_id" : 2,
                        "host" : "localhost:10103",
                        "votes" : 0,
                        "priority" : 0
                },
                {
                        "_id" : 3,
                        "host" : "localhost:10104",
                        "arbiterOnly" : true
                }
        ]
}
test-bk-repl:PRIMARY> rs.reconfig(config)
Mon Apr 21 18:26:21.467 DBClientCursor::init call() failed
Mon Apr 21 18:26:21.467 trying reconnect to 127.0.0.1:10101
Mon Apr 21 18:26:21.468 reconnect 127.0.0.1:10101 ok
reconnected to server after rs command (which is normal)

パッシブに加わったことを確認。

test-bk-repl:PRIMARY> db.isMaster()
{
        "setName" : "test-bk-repl",
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "localhost:10101",
                "localhost:10102"
        ],
        "passives" : [
                "localhost:10103"
        ],
        "arbiters" : [
                "localhost:10104"
        ],
        "primary" : "localhost:10101",
        "me" : "localhost:10101",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "localTime" : ISODate("2014-04-21T09:26:40.602Z"),
        "ok" : 1
}

以上。Mongoシェルは補完してくれるから助かるけど、コマンドなかなか覚えられぬ。