mongodb4.0 replica set(副本集) 切换主节点 手动配置主从

解决思路

  1. 为了保证数据的一致性,需要先停止应用对数据库的读写
  2. 修改要切换为主节点Primary的Secondary节点的优先级
  3. 重新配置config

解决方式

数据库通过shell登录Primary节点,这是因为修改必须要Primary节点才可以。通过rs.status()查看当前副本集状态,找到members需要切换为Primary节点的数组下标,将其优先级priority设置成members中最大的值,rs.conf()获取到当前配置,重新配置rs.reconfig(config)就修改成功了。
执行命令:

mongos:PRIMARY> config = rs.conf()
mongos:PRIMARY> config.members[1].priority=2
mongos:PRIMARY> rs.reconfig(config)

以下是详细的过程输出:

mongos:PRIMARY> config = rs.conf()
{
	"_id" : "mongos",
	"version" : 1,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "172.21.0.1:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "172.21.0.1:27018",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "172.21.0.1:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {
			
		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("5cf69637809c46464bcd13cc")
	}
}
mongos:PRIMARY> config.members[0].priority=2
2
mongos:PRIMARY> rs.reconfig(config)
{
	"ok" : 1,
	"operationTime" : Timestamp(1562597866, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1562597866, 1),
		"signature" : {
			"hash" : BinData(0,"pS59GbmLjln3v8nK+1Gc4ToQDSE="),
			"keyId" : NumberLong("6698706710267166722")
		}
	}
}
mongos:PRIMARY> 
mongos:PRIMARY> rs.status()
2019-07-08T14:58:17.111+0000 E QUERY    [js] Error: error doing query: failed: network error while attempting to run command 'replSetGetStatus' on host '172.21.0.1:27018'  :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.status@src/mongo/shell/utils.js:1360:12
@(shell):1:1
2019-07-08T14:58:17.113+0000 I NETWORK  [js] trying reconnect to 172.21.0.1:27018 failed
2019-07-08T14:58:17.116+0000 I NETWORK  [js] reconnect 172.21.0.1:27018 ok
mongos:SECONDARY> 
mongos:SECONDARY> rs.status()
{
	"set" : "mongos",
	"date" : ISODate("2019-07-08T14:58:20.646Z"),
	"myState" : 2,
	"term" : NumberLong(6),
	"syncingTo" : "172.21.0.1:27017",
	"syncSourceHost" : "172.21.0.1:27017",
	"syncSourceId" : 0,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1562597898, 1),
			"t" : NumberLong(6)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1562597898, 1),
			"t" : NumberLong(6)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1562597898, 1),
			"t" : NumberLong(6)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1562597898, 1),
			"t" : NumberLong(6)
		}
	},
	"lastStableCheckpointTimestamp" : Timestamp(1562597878, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "172.21.0.1:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 9562,
			"optime" : {
				"ts" : Timestamp(1562597898, 1),
				"t" : NumberLong(6)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1562597898, 1),
				"t" : NumberLong(6)
			},
			"optimeDate" : ISODate("2019-07-08T14:58:18Z"),
			"optimeDurableDate" : ISODate("2019-07-08T14:58:18Z"),
			"lastHeartbeat" : ISODate("2019-07-08T14:58:19.244Z"),
			"lastHeartbeatRecv" : ISODate("2019-07-08T14:58:18.820Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1562597876, 1),
			"electionDate" : ISODate("2019-07-08T14:57:56Z"),
			"configVersion" : 2
		},
		{
			"_id" : 1,
			"name" : "172.21.0.1:27018",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 9567,
			"optime" : {
				"ts" : Timestamp(1562597898, 1),
				"t" : NumberLong(6)
			},
			"optimeDate" : ISODate("2019-07-08T14:58:18Z"),
			"syncingTo" : "172.21.0.1:27017",
			"syncSourceHost" : "172.21.0.1:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 2,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 2,
			"name" : "172.21.0.1:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 9562,
			"optime" : {
				"ts" : Timestamp(1562597898, 1),
				"t" : NumberLong(6)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1562597898, 1),
				"t" : NumberLong(6)
			},
			"optimeDate" : ISODate("2019-07-08T14:58:18Z"),
			"optimeDurableDate" : ISODate("2019-07-08T14:58:18Z"),
			"lastHeartbeat" : ISODate("2019-07-08T14:58:19.244Z"),
			"lastHeartbeatRecv" : ISODate("2019-07-08T14:58:19.289Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "172.21.0.1:27017",
			"syncSourceHost" : "172.21.0.1:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 2
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1562597898, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1562597898, 1),
		"signature" : {
			"hash" : BinData(0,"g+x+Tlz22VNUuyNvHQztuxkE6J0="),
			"keyId" : NumberLong("6698706710267166722")
		}
	}
}
mongos:SECONDARY> 

执行查看rs.status()过程中出现这个报错,是因为修改配置后正在重启,稍等一会就切换成功,再次回车会发现当前节点已经变成了Secondary节点

mongos:PRIMARY> rs.status()
2019-07-08T14:58:17.111+0000 E QUERY    [js] Error: error doing query: failed: network error while attempting to run command 'replSetGetStatus' on host '172.21.0.1:27018'  :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.status@src/mongo/shell/utils.js:1360:12
@(shell):1:1
2019-07-08T14:58:17.113+0000 I NETWORK  [js] trying reconnect to 172.21.0.1:27018 failed
2019-07-08T14:58:17.116+0000 I NETWORK  [js] reconnect 172.21.0.1:27018 ok
mongos:SECONDARY> 

至此,手动切换Primary节点就完成了。

你可能感兴趣的:(Database)