MongoDB复制集技术

MongoDB复制集简介

一组MongoDB复制集,就是一组mongod进程,这些进程维护同一个数据集合。复制集提供了数据冗余和高等级的可靠性,这是生产部署的基础。
保证数据在生产不属实的冗余和可靠性,通过在不同的机器上保存副本来保证数据不会因为单点损坏而丢失。能够随时应对数据丢失、机器损坏带来的风险。还能提高读取能力,用户的读取服务器和写入服务器在不同的地方,而且,有不同的服务器为不同的用户提供服务,提高整个系统的负载。
一组复制集就是一组mongod实例掌管统一个数据集,实例可以再不同的机器上。实例中包含一个主导,接受客户端所有的写入操作,其他都是副本实例,丛主服务器上获得数据并保存同步。
主服务器很重要,包含了所有的改变操作的日志。但是副本服务器集群包含所有的主服务器数据,因此当主服务已挂掉了,就会在副本服务器上重新选取一个成为主服务器。
每个复制集还有一个仲裁者,仲裁者不存储数据,只是负责通过心跳包来确认及群众集合的数量,并在主服务器选举的时候作为仲裁决定结果。

MongoDB集群搭建

  • 集群规划

    1. 三个以上的MongoDB节点或多实例
    2. 目录规划
    3. 多个配置文件
  • 创建数据目录

for i in $(seq 18 21);do mkdir /mongod/data-$i
mkdir /mongod/log
  • 创建日志文件
for i in $(seq 18 21);do touch /mongod/log/mongod-$i.log
chown -R mongod:mongod /mongod
  • 修改配置文件
systemLog:
    destination: file
    path: "/mongod/log/mongod-18.log"
    logAppend: true
processManagement:
    fork: true
storage:
    journal:
       enabled: true
    dbPath: "/mongod/data-18"
    directoryPerDB: true
    wiredTiger:
     engineConfig:
       cacheSizeGB: 1
       directoryForIndexes: true
     collectionConfig:
       blockCompressor: zlib
     indexConfig:
       prefixCompression: true
net:
    port: 27018
    bindIp: 0.0.0.0
replication:
  oplogSizeMB: 2048
  replSetName: my_repl

复制到其他配置文件中并修改相关数据

  • 启动多实例
mongod -f conf/mongod-27018.conf
mongod -f conf/mongod-27019.conf
mongod -f conf/mongod-27020.conf
mongod -f conf/mongod-27021.conf
  • 配置复制集
    一主两从
mongo --port 27018
>use admin
>config={_id:'my_repl',members:[{_id:0,host:'192.168.10.81:27018'},{_id:1,host:'192.168.10.81:27019'},{_id:2,host:'192.168.10.81:27020'}]}
{
	"_id" : "my_repl",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.10.81:27018"
		},
		{
			"_id" : 1,
			"host" : "192.168.10.81:27019"
		},
		{
			"_id" : 2,
			"host" : "192.168.10.81:27020"
		}
	]
}
> rs.initiate(config)
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594695272, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594695272, 1)
}
my_repl:OTHER> 
my_repl:PRIMARY> 
  • 插入数据测试
mongo --port 27018
my_repl:PRIMARY> use test
switched to db test
my_repl:PRIMARY> db.movies.insert([{title:"jaws",year:1975}])
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 1,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
my_repl:PRIMARY> show tables
movies
my_repl:PRIMARY> db
test
my_repl:PRIMARY> db.movies.find()
{ "_id" : ObjectId("5f0d20665168fa37518205c5"), "title" : "jaws", "year" : 1975 }

登录从库查看

mongo --port 27019
my_repl:SECONDARY> db
test
my_repl:SECONDARY> show tables
2020-07-14T11:15:21.226+0800 E  QUERY    [js] uncaught exception: Error: listCollections failed: {
	"operationTime" : Timestamp(1594696514, 1),
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594696514, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:834:15
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:882:16
shellHelper.show@src/mongo/shell/utils.js:893:9
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1

在复制集中从库默认不允许进行读操作,需要修改slaveOK参数

my_repl:SECONDARY> rs.slaveOk();
my_repl:SECONDARY> show tables
movies
my_repl:SECONDARY> db.movies.find()
{ "_id" : ObjectId("5f0d20665168fa37518205c5"), "title" : "jaws", "year" : 1975 }
  • 查看复制集节点状态
my_repl:SECONDARY> rs.status()
{
	"set" : "my_repl",
	"date" : ISODate("2020-07-14T03:31:19.974Z"),
	"myState" : 2,
	"term" : NumberLong(1),
	"syncingTo" : "192.168.10.81:27018",
	"syncSourceHost" : "192.168.10.81:27018",
	"syncSourceId" : 0,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1594697474, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2020-07-14T03:31:14.860Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1594697474, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2020-07-14T03:31:14.860Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1594697474, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1594697474, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2020-07-14T03:31:14.860Z"),
		"lastDurableWallTime" : ISODate("2020-07-14T03:31:14.860Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1594697444, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1594697444, 1),
	"electionParticipantMetrics" : {
		"votedForCandidate" : true,
		"electionTerm" : NumberLong(1),
		"lastVoteDate" : ISODate("2020-07-14T02:54:44.031Z"),
		"electionCandidateMemberId" : 0,
		"voteReason" : "",
		"lastAppliedOpTimeAtElection" : {
			"ts" : Timestamp(1594695272, 1),
			"t" : NumberLong(-1)
		},
		"maxAppliedOpTimeInSet" : {
			"ts" : Timestamp(1594695272, 1),
			"t" : NumberLong(-1)
		},
		"priorityAtElection" : 1,
		"newTermStartDate" : ISODate("2020-07-14T02:54:44.051Z"),
		"newTermAppliedDate" : ISODate("2020-07-14T02:54:44.648Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.10.81:27018",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 2206,
			"optime" : {
				"ts" : Timestamp(1594697474, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1594697474, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-07-14T03:31:14Z"),
			"optimeDurableDate" : ISODate("2020-07-14T03:31:14Z"),
			"lastHeartbeat" : ISODate("2020-07-14T03:31:19.445Z"),
			"lastHeartbeatRecv" : ISODate("2020-07-14T03:31:19.260Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1594695284, 1),
			"electionDate" : ISODate("2020-07-14T02:54:44Z"),
			"configVersion" : 1
		},
		{
			"_id" : 1,
			"name" : "192.168.10.81:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 2266,
			"optime" : {
				"ts" : Timestamp(1594697474, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-07-14T03:31:14Z"),
			"syncingTo" : "192.168.10.81:27018",
			"syncSourceHost" : "192.168.10.81:27018",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 2,
			"name" : "192.168.10.81:27020",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 2206,
			"optime" : {
				"ts" : Timestamp(1594697474, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1594697474, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-07-14T03:31:14Z"),
			"optimeDurableDate" : ISODate("2020-07-14T03:31:14Z"),
			"lastHeartbeat" : ISODate("2020-07-14T03:31:19.700Z"),
			"lastHeartbeatRecv" : ISODate("2020-07-14T03:31:19.849Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.10.81:27018",
			"syncSourceHost" : "192.168.10.81:27018",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594697474, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594697474, 1)
}
  • 查看当前节点是否是master
my_repl:SECONDARY> rs.isMaster()
{
	"hosts" : [
		"192.168.10.81:27018",
		"192.168.10.81:27019",
		"192.168.10.81:27020"
	],
	"setName" : "my_repl",
	"setVersion" : 1,
	"ismaster" : false,
	"secondary" : true,
	"primary" : "192.168.10.81:27018",
	"me" : "192.168.10.81:27019",
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1594697574, 1),
			"t" : NumberLong(1)
		},
		"lastWriteDate" : ISODate("2020-07-14T03:32:54Z"),
		"majorityOpTime" : {
			"ts" : Timestamp(1594697574, 1),
			"t" : NumberLong(1)
		},
		"majorityWriteDate" : ISODate("2020-07-14T03:32:54Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2020-07-14T03:32:55.427Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"connectionId" : 25,
	"minWireVersion" : 0,
	"maxWireVersion" : 8,
	"readOnly" : false,
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594697574, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594697574, 1)
}
my_repl:PRIMARY> rs.isMaster()
{
	"hosts" : [
		"192.168.10.81:27018",
		"192.168.10.81:27019",
		"192.168.10.81:27020"
	],
	"setName" : "my_repl",
	"setVersion" : 1,
	"ismaster" : true,
	"secondary" : false,
	"primary" : "192.168.10.81:27018",
	"me" : "192.168.10.81:27018",
	"electionId" : ObjectId("7fffffff0000000000000001"),
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1594697604, 1),
			"t" : NumberLong(1)
		},
		"lastWriteDate" : ISODate("2020-07-14T03:33:24Z"),
		"majorityOpTime" : {
			"ts" : Timestamp(1594697604, 1),
			"t" : NumberLong(1)
		},
		"majorityWriteDate" : ISODate("2020-07-14T03:33:24Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2020-07-14T03:33:34.340Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"connectionId" : 27,
	"minWireVersion" : 0,
	"maxWireVersion" : 8,
	"readOnly" : false,
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594697604, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594697604, 1)
}
  • 新增一个节点
    在主节点执行
my_repl:PRIMARY> rs.add("192.168.10.81:27021")
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594697683, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594697683, 1)
}

查看

my_repl:PRIMARY> rs.status()
{
	"set" : "my_repl",
	"date" : ISODate("2020-07-14T03:35:17.753Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 3,
	"writeMajorityCount" : 3,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1594697714, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2020-07-14T03:35:14.921Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1594697714, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2020-07-14T03:35:14.921Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1594697714, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1594697714, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2020-07-14T03:35:14.921Z"),
		"lastDurableWallTime" : ISODate("2020-07-14T03:35:14.921Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1594697683, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1594697683, 1),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2020-07-14T02:54:44.027Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1594695272, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2020-07-14T02:54:44.051Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2020-07-14T02:54:44.619Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.10.81:27018",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 2551,
			"optime" : {
				"ts" : Timestamp(1594697714, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-07-14T03:35:14Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1594695284, 1),
			"electionDate" : ISODate("2020-07-14T02:54:44Z"),
			"configVersion" : 2,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.10.81:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 2445,
			"optime" : {
				"ts" : Timestamp(1594697714, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1594697714, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-07-14T03:35:14Z"),
			"optimeDurableDate" : ISODate("2020-07-14T03:35:14Z"),
			"lastHeartbeat" : ISODate("2020-07-14T03:35:17.497Z"),
			"lastHeartbeatRecv" : ISODate("2020-07-14T03:35:17.604Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.10.81:27018",
			"syncSourceHost" : "192.168.10.81:27018",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 2
		},
		{
			"_id" : 2,
			"name" : "192.168.10.81:27020",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 2445,
			"optime" : {
				"ts" : Timestamp(1594697714, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1594697714, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-07-14T03:35:14Z"),
			"optimeDurableDate" : ISODate("2020-07-14T03:35:14Z"),
			"lastHeartbeat" : ISODate("2020-07-14T03:35:17.498Z"),
			"lastHeartbeatRecv" : ISODate("2020-07-14T03:35:17.604Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.10.81:27018",
			"syncSourceHost" : "192.168.10.81:27018",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 2
		},
		{
			"_id" : 3,
			"name" : "192.168.10.81:27021",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 34,
			"optime" : {
				"ts" : Timestamp(1594697714, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1594697714, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-07-14T03:35:14Z"),
			"optimeDurableDate" : ISODate("2020-07-14T03:35:14Z"),
			"lastHeartbeat" : ISODate("2020-07-14T03:35:17.519Z"),
			"lastHeartbeatRecv" : ISODate("2020-07-14T03:35:15.790Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "192.168.10.81:27018",
			"syncSourceHost" : "192.168.10.81:27018",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 2
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594697714, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594697714, 1)
}

登录新增节点验证

mongo --port 27021
my_repl:SECONDARY> rs.slaveOk()
my_repl:SECONDARY> show tables
movies
my_repl:SECONDARY> db.movies.find()
{ "_id" : ObjectId("5f0d20665168fa37518205c5"), "title" : "jaws", "year" : 1975 }
  • 删除一个节点
my_repl:PRIMARY> rs.remove("192.168.10.81:27020")
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594697909, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594697909, 1)
}
my_repl:PRIMARY> rs.status()
{
	"set" : "my_repl",
	"date" : ISODate("2020-07-14T03:38:39.298Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncingTo" : "",
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1594697909, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2020-07-14T03:38:29.049Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1594697909, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2020-07-14T03:38:29.049Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1594697909, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1594697909, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2020-07-14T03:38:29.049Z"),
		"lastDurableWallTime" : ISODate("2020-07-14T03:38:29.049Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1594697864, 1),
	"lastStableCheckpointTimestamp" : Timestamp(1594697864, 1),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2020-07-14T02:54:44.027Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1594695272, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2020-07-14T02:54:44.051Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2020-07-14T02:54:44.619Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.10.81:27018",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 2753,
			"optime" : {
				"ts" : Timestamp(1594697909, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-07-14T03:38:29Z"),
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1594695284, 1),
			"electionDate" : ISODate("2020-07-14T02:54:44Z"),
			"configVersion" : 3,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "192.168.10.81:27019",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 2646,
			"optime" : {
				"ts" : Timestamp(1594697909, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1594697909, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-07-14T03:38:29Z"),
			"optimeDurableDate" : ISODate("2020-07-14T03:38:29Z"),
			"lastHeartbeat" : ISODate("2020-07-14T03:38:39.063Z"),
			"lastHeartbeatRecv" : ISODate("2020-07-14T03:38:39.116Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 3
		},
		{
			"_id" : 3,
			"name" : "192.168.10.81:27021",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 235,
			"optime" : {
				"ts" : Timestamp(1594697909, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1594697909, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2020-07-14T03:38:29Z"),
			"optimeDurableDate" : ISODate("2020-07-14T03:38:29Z"),
			"lastHeartbeat" : ISODate("2020-07-14T03:38:39.064Z"),
			"lastHeartbeatRecv" : ISODate("2020-07-14T03:38:39.117Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 3
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594697909, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594697909, 1)
}

添加仲裁节点

arbiter节点在复制集中不进行数据同步,只负责新master节点的仲裁工作

my_repl:PRIMARY> rs.addArb("192.168.10.81:27020")
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594706596, 2),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594706596, 2)
}
my_repl:PRIMARY> 
my_repl:PRIMARY> rs.isMaster()
{
	"hosts" : [
		"192.168.10.81:27018",
		"192.168.10.81:27019",
		"192.168.10.81:27021"
	],
	"arbiters" : [
		"192.168.10.81:27020"
	],
	"setName" : "my_repl",
	"setVersion" : 6,
	"ismaster" : true,
	"secondary" : false,
	"primary" : "192.168.10.81:27018",
	"me" : "192.168.10.81:27018",
	"electionId" : ObjectId("7fffffff0000000000000001"),
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1594706596, 2),
			"t" : NumberLong(1)
		},
		"lastWriteDate" : ISODate("2020-07-14T06:03:16Z"),
		"majorityOpTime" : {
			"ts" : Timestamp(1594706596, 2),
			"t" : NumberLong(1)
		},
		"majorityWriteDate" : ISODate("2020-07-14T06:03:16Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2020-07-14T06:03:30.120Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"connectionId" : 39,
	"minWireVersion" : 0,
	"maxWireVersion" : 8,
	"readOnly" : false,
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594706596, 2),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594706596, 2)
}
  • 登录到arbiter节点
 mongo --port 27020
 my_repl:ARBITER> show tables
2020-07-14T14:10:34.380+0800 E  QUERY    [js] uncaught exception: Error: listCollections failed: {
	"ok" : 0,
	"errmsg" : "not master and slaveOk=false",
	"code" : 13435,
	"codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:834:15
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:882:16
shellHelper.show@src/mongo/shell/utils.js:893:9
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1

修改复制集节点

此方法是将已存在的复制集的节点修改为仲裁或hidden节点,hidden节点不参与选主,但是参与投票

  • 修改节点为hidden和延时节点
my_repl:PRIMARY> cfg=rs.conf()
{
	"_id" : "my_repl",
	"version" : 9,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.10.81:27018",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "192.168.10.81:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 3,
			"host" : "192.168.10.81:27021",
			"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("5f0d1e689245f59d61f2f555")
	}
}
cfg.members[1].priority=0
cfg.members[1].slaveDelay=120
cfg.members[1].hidden=true
rs.reconfig(cfg)
my_repl:PRIMARY> cfg.members[1].priority=0
0
my_repl:PRIMARY> cfg.members[1].slaveDelay=120
120
my_repl:PRIMARY> cfg.members[1].hidden=true
true
my_repl:PRIMARY> rs.reconfig(cfg)
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594709636, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594709636, 1)
}
my_repl:PRIMARY> rs.isMaster()
{
	"hosts" : [
		"192.168.10.81:27018",
		"192.168.10.81:27021"
	],
	"setName" : "my_repl",
	"setVersion" : 10,
	"ismaster" : true,
	"secondary" : false,
	"primary" : "192.168.10.81:27018",
	"me" : "192.168.10.81:27018",
	"electionId" : ObjectId("7fffffff0000000000000001"),
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1594709696, 1),
			"t" : NumberLong(1)
		},
		"lastWriteDate" : ISODate("2020-07-14T06:54:56Z"),
		"majorityOpTime" : {
			"ts" : Timestamp(1594709696, 1),
			"t" : NumberLong(1)
		},
		"majorityWriteDate" : ISODate("2020-07-14T06:54:56Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2020-07-14T06:55:03.022Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"connectionId" : 98,
	"minWireVersion" : 0,
	"maxWireVersion" : 8,
	"readOnly" : false,
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1594709696, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1594709696, 1)
}

可以看出hosts中有一个节点已经不见了,通过rs.conf()查看

my_repl:PRIMARY> rs.conf()
{
	"_id" : "my_repl",
	"version" : 10,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.10.81:27018",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "192.168.10.81:27019",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : true,
			"priority" : 0,
			"tags" : {
				
			},
			"slaveDelay" : NumberLong(120),
			"votes" : 1
		},
		{
			"_id" : 3,
			"host" : "192.168.10.81:27021",
			"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("5f0d1e689245f59d61f2f555")
	}
}

可以看出_id为1的节点hidden属性变为true,priority值变为0,slaveDelay变为120

其他操作

>rs.stepDown()
>rs.freeze(300)
>rs.slaveOK()
命令 详解
stepDown() 副本集角色切换(谨慎使用)
freeze() 锁定从库,使其不会转变为主库
slaveOK() 允许从库读

猜你喜欢

转载自blog.csdn.net/qq_33235529/article/details/107313979