目次

ReplicaSets

mkdir -p /data/mongo{1,2,3}/{db,logs}

直接

/usr/bin/mongod --replSet replset --journal --port 27017 --logappend --logpath /data/mongo1/logs/mongod.log --pidfilepath=/data/mongo1/logs/mongod.pid --dbpath=/data/mongo1/db --fork --rest
/usr/bin/mongod --replSet replset --journal --port 27018 --logappend --logpath /data/mongo2/logs/mongod.log --pidfilepath=/data/mongo2/logs/mongod.pid --dbpath=/data/mongo2/db --fork --rest
/usr/bin/mongod --replSet replset --journal --port 27019 --logappend --logpath /data/mongo3/logs/mongod.log --pidfilepath=/data/mongo3/logs/mongod.pid --dbpath=/data/mongo3/db --fork --rest

設定ファイル作成

# vi /data/mongo1/mongod.conf
replSet = replset
journal = true
port = 27017
logappend = true
logpath = /data/mongo1/logs/mongod.log
pidfilepath = /data/mongo1/logs/mongod.pid
dbpath = /data/mongo1/db
fork = true
rest = true
# sed -e "s/mongo1/mongo2/" /data/mongo1/mongod.conf > /data/mongo2/mongod.conf
# vi /data/mongo2/mongod.conf
:%s/27017/27018/g
# sed -e "s/mongo1/mongo3/" /data/mongo1/mongod.conf > /data/mongo3/mongod.conf
# vi /data/mongo3/mongod.conf
:%s/27017/27019/g
/usr/bin/mongod --config /data/mongo1/mongod.conf
/usr/bin/mongod --config /data/mongo2/mongod.conf
/usr/bin/mongod --config /data/mongo3/mongod.conf

新規構築 Ver

# mongo
MongoDB shell version: 2.2.2
connecting to: test
> config = {_id:'replset',members:
... [
... {_id:0, host:'localhost:27017', priority:1},
... {_id:1, host:'localhost:27018', priority:1},
... {_id:2, host:'localhost:27019', priority:1},
... ]}
{
       "_id" : "replset",
       "members" : [
               {
                       "_id" : 0,
                       "host" : "localhost:27017",
                       "priority" : 1
               },
               {
                       "_id" : 1,
                       "host" : "localhost:27018",
                       "priority" : 1
               },
               {
                       "_id" : 2,
                       "host" : "localhost:27019",
                       "priority" : 1
               }
       ]
}
> rs.initiate(config)
{
       "info" : "Config now saved locally.  Should come online in about a minute.",
       "ok" : 1
}
replset:STARTUP2> rs.status()
{
       "set" : "replset",
       "date" : ISODate("2013-01-29T05:15:12Z"),
       "myState" : 1,
       "members" : [
               {
                       "_id" : 0,
                       "name" : "localhost:27017",
                       "health" : 1,
                       "state" : 1,
                       "stateStr" : "PRIMARY",
                       "uptime" : 469,
                       "optime" : Timestamp(1359436451000, 1),
                       "optimeDate" : ISODate("2013-01-29T05:14:11Z"),
                       "self" : true
               },
               {
                       "_id" : 1,
                       "name" : "localhost:27018",
                       "health" : 1,
                       "state" : 2,
                       "stateStr" : "SECONDARY",
                       "uptime" : 51,
                       "optime" : Timestamp(1359436451000, 1),
                       "optimeDate" : ISODate("2013-01-29T05:14:11Z"),
                       "lastHeartbeat" : ISODate("2013-01-29T05:15:11Z"),
                       "pingMs" : 3
               },
               {
                       "_id" : 2,
                       "name" : "localhost:27019",
                       "health" : 1,
                       "state" : 2,
                       "stateStr" : "SECONDARY",
                       "uptime" : 51,
                       "optime" : Timestamp(1359436451000, 1),
                       "optimeDate" : ISODate("2013-01-29T05:14:11Z"),
                       "lastHeartbeat" : ISODate("2013-01-29T05:15:11Z"),
                       "pingMs" : 61
               }
       ],
       "ok" : 1
}
replset:PRIMARY> use testdb
switched to db testdb
replset:PRIMARY> db.testcol.insert( { "key1": "string", "key2": 123 } )
replset:PRIMARY> db.testcol.insert( { "key1": "integer", "key2": 456 } )
replset:PRIMARY> db.testcol.find()
{ "_id" : ObjectId("51075bbca90ac28dac68e0f5"), "key1" : "string", "key2" : 123 }
{ "_id" : ObjectId("51075bd7a90ac28dac68e0f6"), "key1" : "integer", "key2" : 456 }
replset:PRIMARY> exit
bye
# mongo --port 27018
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:27018/test
replset:SECONDARY> show dbs
local   4.201171875GB
testdb  0.203125GB
replset:SECONDARY> use testdb
switched to db testdb
replset:SECONDARY> db.testcol.find()
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
replset:SECONDARY> db.getMongo().setSlaveOk()
replset:SECONDARY> show collections
system.indexes
testcol
replset:SECONDARY> db.testcol.find()
{ "_id" : ObjectId("51075bbca90ac28dac68e0f5"), "key1" : "string", "key2" : 123 }
{ "_id" : ObjectId("51075bd7a90ac28dac68e0f6"), "key1" : "integer", "key2" : 456 }
replset:SECONDARY> db.getMongo().slaveOk = false
false
replset:SECONDARY> exit
bye
# mongo
MongoDB shell version: 2.2.2
connecting to: test
replset:PRIMARY> use admin
switched to db admin
replset:PRIMARY> db.shutdownServer()
Mon Jan 28 21:26:19 DBClientCursor::init call() failed
Mon Jan 28 21:26:19 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1:27017
server should be down...
Mon Jan 28 21:26:19 trying reconnect to 127.0.0.1:27017
Mon Jan 28 21:26:19 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017
> exit
bye
# ps aux | grep mongo
root      5101  0.5  3.1 9582360 32512 ?       Sl   21:07   0:06 /usr/bin/mongod --replSet replset --journal --port 27018 --logappend --logpath /data/mongo2/logs/mongod.log --pidfilepath=/data/mongo2/logs/mongod.pid --dbpath=/data/mongo2/db --fork
root      5148  0.5  3.1 9586480 32480 ?       Sl   21:07   0:05 /usr/bin/mongod --replSet replset --journal --port 27019 --logappend --logpath /data/mongo3/logs/mongod.log --pidfilepath=/data/mongo3/logs/mongod.pid --dbpath=/data/mongo3/db --fork
root      5726  0.0  0.0  61212   748 pts/0    S+   21:26   0:00 grep mongo
# mongo --port 27018
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:27018/test
replset:SECONDARY> rs.status()
{
       "set" : "replset",
       "date" : ISODate("2013-01-29T05:27:40Z"),
       "myState" : 2,
       "syncingTo" : "localhost:27019",
       "members" : [
               {
                       "_id" : 0,
                       "name" : "localhost:27017",
                       "health" : 0,
                       "state" : 8,
                       "stateStr" : "(not reachable/healthy)",
                       "uptime" : 0,
                       "optime" : Timestamp(1359436759000, 1),
                       "optimeDate" : ISODate("2013-01-29T05:19:19Z"),
                       "lastHeartbeat" : ISODate("2013-01-29T05:26:18Z"),
                       "pingMs" : 0,
                       "errmsg" : "socket exception [CONNECT_ERROR] for localhost:27017"
               },
               {
                       "_id" : 1,
                       "name" : "localhost:27018",
                       "health" : 1,
                       "state" : 2,
                       "stateStr" : "SECONDARY",
                       "uptime" : 1217,
                       "optime" : Timestamp(1359436759000, 1),
                       "optimeDate" : ISODate("2013-01-29T05:19:19Z"),
                       "errmsg" : "syncing to: localhost:27019",
                       "self" : true
               },
               {
                       "_id" : 2,
                       "name" : "localhost:27019",
                       "health" : 1,
                       "state" : 1,
                       "stateStr" : "PRIMARY",
                       "uptime" : 795,
                       "optime" : Timestamp(1359436759000, 1),
                       "optimeDate" : ISODate("2013-01-29T05:19:19Z"),
                       "lastHeartbeat" : ISODate("2013-01-29T05:27:40Z"),
                       "pingMs" : 0
               }
       ],
       "ok" : 1
}

シンプルVer

rs.initiate({
  _id: "replset",
  members: [
    { _id: 0, host: "localhost:27017" },
    { _id: 1, host: "localhost:27018" },
  ]
})
rs.add('localhost:27019')
rs.addArb('localhost:27019')
rs.status();
rs.remove('localhost:27019')

追加 Ver

# mongo
MongoDB shell version: 2.2.2
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
       http://docs.mongodb.org/
Questions? Try the support group
       http://groups.google.com/group/mongodb-user
> config = {_id: 'replset', members: [ {_id:0, host:'localhost:27017'} ]}
{
       "_id" : "replset",
       "members" : [
               {
                       "_id" : 0,
                       "host" : "localhost:27017"
               }
       ]
}
> rs.initiate(config)
{
       "info" : "Config now saved locally.  Should come online in about a minute.",
       "ok" : 1
}
> rs.status()
{
       "set" : "replset",
       "date" : ISODate("2013-01-29T07:13:05Z"),
       "myState" : 1,
       "members" : [
               {
                       "_id" : 0,
                       "name" : "localhost:27017",
                       "health" : 1,
                       "state" : 1,
                       "stateStr" : "PRIMARY",
                       "uptime" : 567,
                       "optime" : Timestamp(1359443566000, 1),
                       "optimeDate" : ISODate("2013-01-29T07:12:46Z"),
                       "self" : true
               }
       ],
       "ok" : 1
}
replset:PRIMARY> rs.add( {_id:1, host:'localhost:27018'} )
{ "ok" : 1 }
replset:PRIMARY> rs.status()
{
       "set" : "replset",
       "date" : ISODate("2013-01-29T07:16:32Z"),
       "myState" : 1,
       "members" : [
               {
                       "_id" : 0,
                       "name" : "localhost:27017",
                       "health" : 1,
                       "state" : 1,
                       "stateStr" : "PRIMARY",
                       "uptime" : 774,
                       "optime" : Timestamp(1359443636000, 1),
                       "optimeDate" : ISODate("2013-01-29T07:13:56Z"),
                       "self" : true
               },
               {
                       "_id" : 1,
                       "name" : "localhost:27018",
                       "health" : 1,
                       "state" : 2,
                       "stateStr" : "SECONDARY",
                       "uptime" : 156,
                       "optime" : Timestamp(1359443636000, 1),
                       "optimeDate" : ISODate("2013-01-29T07:13:56Z"),
                       "lastHeartbeat" : ISODate("2013-01-29T07:16:31Z"),
                       "pingMs" : 0
               }
       ],
       "ok" : 1
}
replset:PRIMARY> use testdb
switched to db testdb
replset:PRIMARY> db.testcol.insert( { "key1": "string", "key2": 123 } )
replset:PRIMARY> db.testcol.insert( { "key1": "integer", "key2": 456 } )
replset:PRIMARY> db.testcol.find()
{ "_id" : ObjectId("51077831f418a1407ef827b5"), "key1" : "string", "key2" : 123 }
{ "_id" : ObjectId("51077836f418a1407ef827b6"), "key1" : "integer", "key2" : 456 }
replset:PRIMARY> rs.add( {_id:2, host:'localhost:27019'} )
{ "ok" : 1 }
replset:PRIMARY> exit
bye
# mongo --port 27019
MongoDB shell version: 2.2.2
connecting to: 127.0.0.1:27019/test
replset:SECONDARY> db.getMongo().setSlaveOk()
replset:SECONDARY> use testdb
switched to db testdb
replset:SECONDARY> db.testcol.find()
{ "_id" : ObjectId("51077831f418a1407ef827b5"), "key1" : "string", "key2" : 123 }
{ "_id" : ObjectId("51077836f418a1407ef827b6"), "key1" : "integer", "key2" : 456 }
replset:SECONDARY> db.getMongo().slaveOk = false
false
replset:SECONDARY> exit
bye

その他

replset:PRIMARY> db.isMaster()
{
       "setName" : "replset",
       "ismaster" : true,
       "secondary" : false,
       "hosts" : [
               "localhost:27017",
               "localhost:27019",
               "localhost:27018"
       ],
       "primary" : "localhost:27017",
       "me" : "localhost:27017",
       "maxBsonObjectSize" : 16777216,
       "localTime" : ISODate("2013-01-29T07:28:27.607Z"),
       "ok" : 1
}

その他

replset:PRIMARY> config=rs.conf()
{
       "_id" : "replset",
       "version" : 3,
       "members" : [
               {
                       "_id" : 0,
                       "host" : "localhost:27017"
               },
               {
                       "_id" : 1,
                       "host" : "localhost:27018"
               },
               {
                       "_id" : 2,
                       "host" : "localhost:27019"
               }
       ]
}
replset:PRIMARY> config.members[2].host = "127.0.0.1:27019"
127.0.0.1:27019
replset:PRIMARY> config
{
       "_id" : "replset",
       "version" : 3,
       "members" : [
               {
                       "_id" : 0,
                       "host" : "localhost:27017"
               },
               {
                       "_id" : 1,
                       "host" : "localhost:27018"
               },
               {
                       "_id" : 2,
                       "host" : "127.0.0.1:27019"
               }
       ]
}
replset:SECONDARY> rs.reconfig(config)
{ "ok" : 1 }
replset:PRIMARY> rs.status()
{
       "set" : "replset",
       "date" : ISODate("2013-01-29T07:38:48Z"),
       "myState" : 1,
       "members" : [
               {
                       "_id" : 0,
                       "name" : "localhost:27017",
                       "health" : 1,
                       "state" : 1,
                       "stateStr" : "PRIMARY",
                       "uptime" : 2110,
                       "optime" : Timestamp(1359445059000, 1),
                       "optimeDate" : ISODate("2013-01-29T07:37:39Z"),
                       "self" : true
               },
               {
                       "_id" : 1,
                       "name" : "localhost:27018",
                       "health" : 1,
                       "state" : 2,
                       "stateStr" : "SECONDARY",
                       "uptime" : 99,
                       "optime" : Timestamp(1359445059000, 1),
                       "optimeDate" : ISODate("2013-01-29T07:37:39Z"),
                       "lastHeartbeat" : ISODate("2013-01-29T07:38:47Z"),
                       "pingMs" : 0,
                       "errmsg" : "syncing to: localhost:27017"
               },
               {
                       "_id" : 2,
                       "name" : "127.0.0.1:27019",
                       "health" : 1,
                       "state" : 2,
                       "stateStr" : "SECONDARY",
                       "uptime" : 101,
                       "optime" : Timestamp(1359445059000, 1),
                       "optimeDate" : ISODate("2013-01-29T07:37:39Z"),
                       "lastHeartbeat" : ISODate("2013-01-29T07:38:47Z"),
                       "pingMs" : 0,
                       "errmsg" : "syncing to: localhost:27017"
               }
       ],
       "ok" : 1
}

WEBコンソール

ポート+1000番でWEBコンソールが見れる。
起動時に「–rest」オプションを定義していれば、WEBコンソールでReplica Setsのステータスが見れる。

i.g. http://localhost:28017