mongodb主从复制

地址规划
在这里插入图片描述

关闭27017防火墙端口
在这里插入图片描述

更改mongodb主配文件
在这里插入图片描述

互相连接查看是否连接成功
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

定义变量
首先指定 id,id 就是副本集名称,members 里面是一组数组,数组中是各 MongoDB 实例的地址和监听端口
在这里插入图片描述

打开admin库,使用 rs.initiate 方法,引用刚刚定义的变量 config,来初始化集群
在这里插入图片描述

当副本集创建并初始化完成后,终端提示符会由">“变成"test:PRIMARY>”,tset 是副本集名称,PRIMARY 代表当前实例为 Primary 主节点。一个 MongoDB 副 本集中只有一个 Primary,其它都是 Secondary。并且只有 Primary 能写入数据,Secondary 只能读但不能写。
在这里插入图片描述

当副本集初始化完成后,还需要在 Secondary 上声明自己是从节点,才能查看数据
两台从节点上声明
在这里插入图片描述

查看从节点延时情况
在这里插入图片描述

查看集群状态
在主从节点输入
rs.status()
主:

test:PRIMARY> rs.status()
{
	"set" : "test",
	"date" : ISODate("2022-01-13T02:24:28.304Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"votingMembersCount" : 3,
	"writableVotingMembersCount" : 3,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1642040661, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2022-01-13T02:24:21.535Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1642040661, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1642040661, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1642040661, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2022-01-13T02:24:21.535Z"),
		"lastDurableWallTime" : ISODate("2022-01-13T02:24:21.535Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1642040641, 1),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2022-01-13T02:17:21.396Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(1642040230, 1),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1642040230, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2022-01-13T02:17:21.430Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2022-01-13T02:17:21.531Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.179.172:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 999,
			"optime" : {
				"ts" : Timestamp(1642040661, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2022-01-13T02:24:21Z"),
			"lastAppliedWallTime" : ISODate("2022-01-13T02:24:21.535Z"),
			"lastDurableWallTime" : ISODate("2022-01-13T02:24:21.535Z"),
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1642040241, 1),
			"electionDate" : ISODate("2022-01-13T02:17:21Z"),
			"configVersion" : 1,
			"configTerm" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.179.173:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 438,
			"optime" : {
				"ts" : Timestamp(1642040661, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1642040661, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2022-01-13T02:24:21Z"),
			"optimeDurableDate" : ISODate("2022-01-13T02:24:21Z"),
			"lastAppliedWallTime" : ISODate("2022-01-13T02:24:21.535Z"),
			"lastDurableWallTime" : ISODate("2022-01-13T02:24:21.535Z"),
			"lastHeartbeat" : ISODate("2022-01-13T02:24:28.041Z"),
			"lastHeartbeatRecv" : ISODate("2022-01-13T02:24:26.598Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "192.168.179.172:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.179.174:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 438,
			"optime" : {
				"ts" : Timestamp(1642040661, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1642040661, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2022-01-13T02:24:21Z"),
			"optimeDurableDate" : ISODate("2022-01-13T02:24:21Z"),
			"lastAppliedWallTime" : ISODate("2022-01-13T02:24:21.535Z"),
			"lastDurableWallTime" : ISODate("2022-01-13T02:24:21.535Z"),
			"lastHeartbeat" : ISODate("2022-01-13T02:24:28.077Z"),
			"lastHeartbeatRecv" : ISODate("2022-01-13T02:24:26.599Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "192.168.179.172:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1642040661, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1642040661, 1)
}

从1:

test:SECONDARY> rs.status()
{
	"set" : "test",
	"date" : ISODate("2022-01-13T02:25:36.787Z"),
	"myState" : 2,
	"term" : NumberLong(1),
	"syncSourceHost" : "192.168.179.172:27017",
	"syncSourceId" : 0,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"votingMembersCount" : 3,
	"writableVotingMembersCount" : 3,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1642040731, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2022-01-13T02:25:31.548Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1642040731, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1642040731, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1642040731, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2022-01-13T02:25:31.548Z"),
		"lastDurableWallTime" : ISODate("2022-01-13T02:25:31.548Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1642040721, 1),
	"electionParticipantMetrics" : {
		"votedForCandidate" : true,
		"electionTerm" : NumberLong(1),
		"lastVoteDate" : ISODate("2022-01-13T02:17:21.397Z"),
		"electionCandidateMemberId" : 0,
		"voteReason" : "",
		"lastAppliedOpTimeAtElection" : {
			"ts" : Timestamp(1642040230, 1),
			"t" : NumberLong(-1)
		},
		"maxAppliedOpTimeInSet" : {
			"ts" : Timestamp(1642040230, 1),
			"t" : NumberLong(-1)
		},
		"priorityAtElection" : 1,
		"newTermStartDate" : ISODate("2022-01-13T02:17:21.430Z"),
		"newTermAppliedDate" : ISODate("2022-01-13T02:17:21.527Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.179.172:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 506,
			"optime" : {
				"ts" : Timestamp(1642040731, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1642040731, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2022-01-13T02:25:31Z"),
			"optimeDurableDate" : ISODate("2022-01-13T02:25:31Z"),
			"lastAppliedWallTime" : ISODate("2022-01-13T02:25:31.548Z"),
			"lastDurableWallTime" : ISODate("2022-01-13T02:25:31.548Z"),
			"lastHeartbeat" : ISODate("2022-01-13T02:25:36.695Z"),
			"lastHeartbeatRecv" : ISODate("2022-01-13T02:25:36.138Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1642040241, 1),
			"electionDate" : ISODate("2022-01-13T02:17:21Z"),
			"configVersion" : 1,
			"configTerm" : 1
		},
		{
			"_id" : 1,
			"name" : "192.168.179.173:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1077,
			"optime" : {
				"ts" : Timestamp(1642040731, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2022-01-13T02:25:31Z"),
			"lastAppliedWallTime" : ISODate("2022-01-13T02:25:31.548Z"),
			"lastDurableWallTime" : ISODate("2022-01-13T02:25:31.548Z"),
			"syncSourceHost" : "192.168.179.172:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 2,
			"name" : "192.168.179.174:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 506,
			"optime" : {
				"ts" : Timestamp(1642040731, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1642040731, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2022-01-13T02:25:31Z"),
			"optimeDurableDate" : ISODate("2022-01-13T02:25:31Z"),
			"lastAppliedWallTime" : ISODate("2022-01-13T02:25:31.548Z"),
			"lastDurableWallTime" : ISODate("2022-01-13T02:25:31.548Z"),
			"lastHeartbeat" : ISODate("2022-01-13T02:25:36.695Z"),
			"lastHeartbeatRecv" : ISODate("2022-01-13T02:25:36.694Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "192.168.179.172:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1642040731, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1642040731, 1)
}

从2:

test:SECONDARY> rs.status()
{
	"set" : "test",
	"date" : ISODate("2022-01-13T02:26:05.092Z"),
	"myState" : 2,
	"term" : NumberLong(1),
	"syncSourceHost" : "192.168.179.172:27017",
	"syncSourceId" : 0,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"votingMembersCount" : 3,
	"writableVotingMembersCount" : 3,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1642040761, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2022-01-13T02:26:01.553Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1642040761, 1),
			"t" : NumberLong(1)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1642040761, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1642040761, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2022-01-13T02:26:01.553Z"),
		"lastDurableWallTime" : ISODate("2022-01-13T02:26:01.553Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1642040721, 1),
	"electionParticipantMetrics" : {
		"votedForCandidate" : true,
		"electionTerm" : NumberLong(1),
		"lastVoteDate" : ISODate("2022-01-13T02:17:21.397Z"),
		"electionCandidateMemberId" : 0,
		"voteReason" : "",
		"lastAppliedOpTimeAtElection" : {
			"ts" : Timestamp(1642040230, 1),
			"t" : NumberLong(-1)
		},
		"maxAppliedOpTimeInSet" : {
			"ts" : Timestamp(1642040230, 1),
			"t" : NumberLong(-1)
		},
		"priorityAtElection" : 1,
		"newTermStartDate" : ISODate("2022-01-13T02:17:21.430Z"),
		"newTermAppliedDate" : ISODate("2022-01-13T02:17:21.581Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.179.172:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 534,
			"optime" : {
				"ts" : Timestamp(1642040761, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1642040761, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2022-01-13T02:26:01Z"),
			"optimeDurableDate" : ISODate("2022-01-13T02:26:01Z"),
			"lastAppliedWallTime" : ISODate("2022-01-13T02:26:01.553Z"),
			"lastDurableWallTime" : ISODate("2022-01-13T02:26:01.553Z"),
			"lastHeartbeat" : ISODate("2022-01-13T02:26:04.743Z"),
			"lastHeartbeatRecv" : ISODate("2022-01-13T02:26:04.232Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1642040241, 1),
			"electionDate" : ISODate("2022-01-13T02:17:21Z"),
			"configVersion" : 1,
			"configTerm" : 1
		},
		{
			"_id" : 1,
			"name" : "192.168.179.173:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 534,
			"optime" : {
				"ts" : Timestamp(1642040761, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1642040761, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2022-01-13T02:26:01Z"),
			"optimeDurableDate" : ISODate("2022-01-13T02:26:01Z"),
			"lastAppliedWallTime" : ISODate("2022-01-13T02:26:01.553Z"),
			"lastDurableWallTime" : ISODate("2022-01-13T02:26:01.553Z"),
			"lastHeartbeat" : ISODate("2022-01-13T02:26:04.743Z"),
			"lastHeartbeatRecv" : ISODate("2022-01-13T02:26:04.742Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "192.168.179.172:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.179.174:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1080,
			"optime" : {
				"ts" : Timestamp(1642040761, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2022-01-13T02:26:01Z"),
			"lastAppliedWallTime" : ISODate("2022-01-13T02:26:01.553Z"),
			"lastDurableWallTime" : ISODate("2022-01-13T02:26:01.553Z"),
			"syncSourceHost" : "192.168.179.172:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1642040761, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1642040761, 1)
}

验证主从复制
在主节点上插入数据
在这里插入图片描述

从节点验证
从1:
在这里插入图片描述

从2:
在这里插入图片描述

查看延时情况
在这里插入图片描述

优先级
MongoDB 副本集的中,Secondary 会不断检测 Primary 的健康状况,一旦 Primary 故障,各 Secondary 会根据优先级来选举一个新的 Primary。在没有指定优先级的情况下,各实例的优先级为 1。在优先级一样的情况下,无法预料到到底哪个 Secondary 会成为 Primary。
获取副本集的配置信息,各实例的优先级默认为 1
设置各实例的优先级,只有在 Primary 上可以更改优先级配置。根据 rs.config().members 中的输出,各实例的索引号从 0 开始,每次递增 1,类似数组。
在这里插入图片描述

更新mongodb副本集的配置
在这里插入图片描述

查看优先级
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值