Skip to content

Instantly share code, notes, and snippets.

@lxyuma
Last active December 17, 2015 16:59
Show Gist options
  • Save lxyuma/5642433 to your computer and use it in GitHub Desktop.
Save lxyuma/5642433 to your computer and use it in GitHub Desktop.
mongodb replicaset basic memo

dataとlogディレクトリの準備

mkdir -p /srv/mongodb/rs0/ /srv/mongodb/rs1/ /srv/mongodb/rs2/

mkdir /var/log/mongo

mongodの起動

mongod --port 27017 --dbpath /srv/mongodb/rs0 --replSet jikken --fork --logpath /var/log/mongo/rs0.log --logappend --smallfiles

mongod --port 27018 --dbpath /srv/mongodb/rs1 --replSet jikken --fork --logpath /var/log/mongo/rs1.log --logappend --smallfiles

mongod --port 27019 --dbpath /srv/mongodb/rs2 --replSet jikken --fork --logpath /var/log/mongo/rs2.log --logappend --smallfiles

mongo clientでreplicaを追加

$ mongo

MongoDB shell version: 2.4.1
connecting to: test

> rs.initiate()

> rs.add("host:27018")

> rs.addArb("host:27019")

確認

> use admin

> db.isMaster()
{
  "setName" : "jikken",
	"ismaster" : true,
	"secondary" : false,
	"hosts" : [
		"***:27017",
		"***:27019",
		"***:27018"
	],
	"primary" : "***:27017",
	"me" : "***:27017",
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"localTime" : ISODate("2013-05-24T02:50:33.628Z"),
	"ok" : 1
}


> rs.status()


{
	"set" : "jikken",
	"date" : ISODate("2013-05-24T02:50:18Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "***:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 42668,
			"optime" : {
				"t" : 1369363606,
				"i" : 1
			},
			"optimeDate" : ISODate("2013-05-24T02:46:46Z"),
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "***:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 245,
			"optime" : {
				"t" : 1369363606,
				"i" : 1
			},
			"optimeDate" : ISODate("2013-05-24T02:46:46Z"),
			"lastHeartbeat" : ISODate("2013-05-24T02:50:17Z"),
			"lastHeartbeatRecv" : ISODate("2013-05-24T02:50:18Z"),
			"pingMs" : 0
		},
		{
			"_id" : 2,
			"name" : "***:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 212,
			"optime" : {
				"t" : 1369363606,
				"i" : 1
			},
			"optimeDate" : ISODate("2013-05-24T02:46:46Z"),
			"lastHeartbeat" : ISODate("2013-05-24T02:50:18Z"),
			"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
			"pingMs" : 0
		}
	],
	"ok" : 1
}

rs.status概要

> m = rs.status().members; for(var i=0; i < m.length; i++){print(m[i].name); print(m[i].health); print(m[i].stateStr)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment