高可用

Table of Contents

主节点选举

主节点选举指的是在一个复制集中,当主节点发生异常或宕机后,集群中其他节点会选举出一个新的主节点,从而保障集群继续可用,这也是 MongoDB 复制集高可用主要保障机制。

通常选举的流程如下:

  1. 第一个发现主节点失败的从节点会发起选举,它会通知其他节点选举开始,并且选举它自己为主节点

  2. 第一个发现主节点失败的从节点如果顺利被选举为主节点,则选举结束

  3. 第一个发现主节点失败的从节点如果没有被选举为主节点,则第二个发现主节点失败的从节点发起选举,并通知其他节点选举它自己为主节点

  4. 这样循环知道选举结束

复制集选举所必须遵循的原则:

  1. 更新到最新的 optime 节点会优先选为主节点

  2. 复制集中存活的几点数大于或等于大多数节点数

  3. 节点中 priority 属性会影响主节点的选举,通常 priority 高会被选为主节点

复制集节点总数为奇数也与选举有关,为奇数可以确保在脑裂(复制集被隔离成了两部分)的情况下,也可以正常选举。

Oplog

Oplog 示例

db.test.insert({_id: 1, value: 1})
{
	"ts" : Timestamp(1570588262, 2),
	"t" : NumberLong(1),
	"h" : NumberLong("-3162705877901309764"),
	"v" : 2,
	"op" : "i",
	"ns" : "test.test",
	"ui" : UUID("5ea14ede-676a-4be2-9334-eb54042b762b"),
	"wall" : ISODate("2019-10-09T02:31:02.766Z"),
	"o" : {
		"_id" : 1,
		"value" : 1
	}
}
db.test.update({_id: 1}, {$inc: {value: 10}})
{
	"ts" : Timestamp(1570588425, 1),
	"t" : NumberLong(1),
	"h" : NumberLong("-1203673023540594040"),
	"v" : 2,
	"op" : "u",
	"ns" : "test.test",
	"ui" : UUID("5ea14ede-676a-4be2-9334-eb54042b762b"),
	"o2" : {
		"_id" : 1
	},
	"wall" : ISODate("2019-10-09T02:33:45.404Z"),
	"o" : {
		"$v" : 1,
		"$set" : {
			"value" : 11
		}
	}
}
db.test.update({}, {$set: {name: "foo"}})
{
	"ts" : Timestamp(1570588832, 1),
	"t" : NumberLong(1),
	"h" : NumberLong("6515094169908862103"),
	"v" : 2,
	"op" : "u",
	"ns" : "test.test",
	"ui" : UUID("5ea14ede-676a-4be2-9334-eb54042b762b"),
	"o2" : {
		"_id" : 1
	},
	"wall" : ISODate("2019-10-09T02:40:32.722Z"),
	"o" : {
		"$v" : 1,
		"$set" : {
			"name" : "foo"
		}
	}
}
db.test.update({}, {$set: {name: "bar"}}, {multi: true})
{ "ts" : Timestamp(1570589237, 1), "t" : NumberLong(1), "h" : NumberLong("-1220821550137650221"), "v" : 2, "op" : "u", "ns" : "test.test", "ui" : UUID("5ea14ede-676a-4be2-9334-eb54042b762b"), "o2" : { "_id" : 1 }, "wall" : ISODate("2019-10-09T02:47:17.242Z"), "o" : { "$v" : 1, "$set" : { "name" : "bar" } } }
{ "ts" : Timestamp(1570589237, 2), "t" : NumberLong(1), "h" : NumberLong("3321497765907076362"), "v" : 2, "op" : "u", "ns" : "test.test", "ui" : UUID("5ea14ede-676a-4be2-9334-eb54042b762b"), "o2" : { "_id" : 2 }, "wall" : ISODate("2019-10-09T02:47:17.242Z"), "o" : { "$v" : 1, "$set" : { "name" : "bar" } } }
{ "ts" : Timestamp(1570589237, 3), "t" : NumberLong(1), "h" : NumberLong("2492957690417542532"), "v" : 2, "op" : "u", "ns" : "test.test", "ui" : UUID("5ea14ede-676a-4be2-9334-eb54042b762b"), "o2" : { "_id" : 3 }, "wall" : ISODate("2019-10-09T02:47:17.242Z"), "o" : { "$v" : 1, "$set" : { "name" : "bar" } } }
db.test.update({_id: 1}, {$push: {array: "a"}}
{
	"ts" : Timestamp(1570589551, 1),
	"t" : NumberLong(1),
	"h" : NumberLong("6092504800295097491"),
	"v" : 2,
	"op" : "u",
	"ns" : "test.test",
	"ui" : UUID("5ea14ede-676a-4be2-9334-eb54042b762b"),
	"o2" : {
		"_id" : 1
	},
	"wall" : ISODate("2019-10-09T02:52:31.794Z"),
	"o" : {
		"$v" : 1,
		"$set" : {
			"array" : [
				"a"
			]
		}
	}
}
db.test.update({_id: 1}, {$push: {array: "b"}})
{
	"ts" : Timestamp(1570589838, 1),
	"t" : NumberLong(1),
	"h" : NumberLong("-3567419863364947058"),
	"v" : 2,
	"op" : "u",
	"ns" : "test.test",
	"ui" : UUID("5ea14ede-676a-4be2-9334-eb54042b762b"),
	"o2" : {
		"_id" : 1
	},
	"wall" : ISODate("2019-10-09T02:57:18.724Z"),
	"o" : {
		"$v" : 1,
		"$set" : {
			"array.1" : "b"
		}
	}
}
db.test.update({_id: 1}, {$addToSet: {array: "e"}})
{
	"ts" : Timestamp(1570589955, 1),
	"t" : NumberLong(1),
	"h" : NumberLong("2739338538944730109"),
	"v" : 2,
	"op" : "u",
	"ns" : "test.test",
	"ui" : UUID("5ea14ede-676a-4be2-9334-eb54042b762b"),
	"o2" : {
		"_id" : 1
	},
	"wall" : ISODate("2019-10-09T02:59:15.200Z"),
	"o" : {
		"$v" : 1,
		"$set" : {
			"array" : [
				"a",
				"b",
				"c",
				"e"
			]
		}
	}
}
db.test.createIndex({value: 1})
{
	"ts" : Timestamp(1570590200, 2),
	"t" : NumberLong(1),
	"h" : NumberLong("5745853766514684235"),
	"v" : 2,
	"op" : "c",
	"ns" : "test.$cmd",
	"ui" : UUID("5ea14ede-676a-4be2-9334-eb54042b762b"),
	"wall" : ISODate("2019-10-09T03:03:20.170Z"),
	"o" : {
		"createIndexes" : "test",
		"v" : 2,
		"key" : {
			"value" : 1
		},
		"name" : "value_1"
	}
}
**
**
**
**

results matching ""

    No results matching ""