1 环境说明
三台主机IP 地址如下:
[dave@www.cndba.cn_1 ~]# cat /etc/hosts
127.0.0.1 localhost
172.31.185.120 mongodb1 --Master
172.31.185.165 mongodb2 --Slave
172.31.185.131 mongodb3 --Slave
[dave@www.cndba.cn_1 ~]#
操作系统:
[dave@www.cndba.cn_3 ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[dave@www.cndba.cn_3 ~]#
2 安装MongoDB 软件
在每个节点上安装MongoDB 软件,具体步骤参考之前的博客:
Redhat 7.7 平台 MongoDB 4.4.6 安装 配置 手册
https://www.cndba.cn/dave/article/4542
在副本集中启用认证后需要配置security.keyFile,否则会报如下错误:
[dave@www.cndba.cn_1 etc]# service mongodb start
BadValue: security.keyFile is required when authorization is enabled with replica sets
try '/usr/local/mongodb/bin/mongod --help' for more information
[dave@www.cndba.cn_1 etc]#
在mongodb1节点执行:
[dave@www.cndba.cn_1 etc]# openssl rand -base64 753 > /data/mongodb/etc/mongo.keyfile
[dave@www.cndba.cn_1 etc]# chmod 600 /data/mongodb/etc/mongo.keyfile
[dave@www.cndba.cn_1 etc]# scp mongo.keyfile mongodb2:`pwd`
创建复制到其他节点。这里3节点必须保持一致。
这里注意MongoDB的参数,我们多了一个replication的选项:
[dave@www.cndba.cn_1 ~]# cat /data/mongodb/etc/mongo.conf
systemLog:
destination: file
path: "/data/mongodb/logs/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/data/mongodb/data"
net:
bindIp: 0.0.0.0
port: 27017
security:
keyFile: /data/mongodb/etc/mongo.keyfile
authorization: "enabled"
processManagement:
fork: true
replication:
replSetName: rs0
[dave@www.cndba.cn_1 ~]#
安装完成后,在3个节点启动MongoDB。
3 初始化副本集
初始化有两种方式。
第一种:
> use admin
switched to db admin
> rs.initiate({_id:"rs0", members:[{_id:0, host:"172.31.185.120:27017"}, {_id:1,host:"172.31.185.165:27017"}, {_id:2, host:"172.31.185.131:27017"}]})
{ "ok" : 1 }
rs0:SECONDARY>
第二种:
>config={_id:"rs0", members:[{_id:0, host:"172.31.185.120:27017"}, {_id:1,host:"172.31.185.165:27017"}, {_id:2, host:"172.31.185.131:27017"}]}
>rs.initiate(config)
查看副本集状态:
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2022-04-30T11:28:11.666Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1651318086, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2022-04-30T11:28:06.279Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1651318086, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2022-04-30T11:28:06.279Z"),
"appliedOpTime" : {
"ts" : Timestamp(1651318086, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1651318086, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2022-04-30T11:28:06.279Z"),
"lastDurableWallTime" : ISODate("2022-04-30T11:28:06.279Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1651318036, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2022-04-30T11:26:16.197Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1651317965, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2022-04-30T11:26:16.257Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2022-04-30T11:26:17.021Z")
},
"members" : [
{
"_id" : 0,
"name" : "172.31.185.120:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 12472,
"optime" : {
"ts" : Timestamp(1651318086, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-04-30T11:28:06Z"),
"lastAppliedWallTime" : ISODate("2022-04-30T11:28:06.279Z"),
"lastDurableWallTime" : ISODate("2022-04-30T11:28:06.279Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "Could not find member to sync from",
"electionTime" : Timestamp(1651317976, 1),
"electionDate" : ISODate("2022-04-30T11:26:16Z"),
"configVersion" : 1,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.31.185.165:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 126,
"optime" : {
"ts" : Timestamp(1651318086, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1651318086, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-04-30T11:28:06Z"),
"optimeDurableDate" : ISODate("2022-04-30T11:28:06Z"),
"lastAppliedWallTime" : ISODate("2022-04-30T11:28:06.279Z"),
"lastDurableWallTime" : ISODate("2022-04-30T11:28:06.279Z"),
"lastHeartbeat" : ISODate("2022-04-30T11:28:10.210Z"),
"lastHeartbeatRecv" : ISODate("2022-04-30T11:28:11.219Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "172.31.185.120:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1
},
{
"_id" : 2,
"name" : "172.31.185.131:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 126,
"optime" : {
"ts" : Timestamp(1651318086, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1651318086, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2022-04-30T11:28:06Z"),
"optimeDurableDate" : ISODate("2022-04-30T11:28:06Z"),
"lastAppliedWallTime" : ISODate("2022-04-30T11:28:06.279Z"),
"lastDurableWallTime" : ISODate("2022-04-30T11:28:06.279Z"),
"lastHeartbeat" : ISODate("2022-04-30T11:28:10.210Z"),
"lastHeartbeatRecv" : ISODate("2022-04-30T11:28:09.726Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "172.31.185.165:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1651318086, 1),
"signature" : {
"hash" : BinData(0,"xY5b9GAQVPtuRJmtXx2vsDwOF8c="),
"keyId" : NumberLong("7092356702216912900")
}
},
"operationTime" : Timestamp(1651318086, 1)
}
rs0:PRIMARY>
4 数据验证同步
创建管理用户:
rs0:PRIMARY> use admin
switched to db admin
rs0:PRIMARY> db.createUser({ "user":"root","pwd":"root","roles":[ { role: "readWriteAnyDatabase", db: "admin" },{ role: "root", db: "admin" },"readWrite"]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "readWriteAnyDatabase",
"db" : "admin"
},
{
"role" : "root",
"db" : "admin"
},
"readWrite"
]
}
rs0:PRIMARY>
rs0:PRIMARY> show dbs
rs0:PRIMARY> use admin
switched to db admin
rs0:PRIMARY> db.auth('root','root')
1
rs0:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.001GB
创建测试库和测试用户:
rs0:PRIMARY> use cndba
switched to db cndba
rs0:PRIMARY> db.createUser({ "user":"cndba","pwd":"cndba","roles":[{role: "readWrite", db: "cndba" }]})
Successfully added user: {
"user" : "cndba",
"roles" : [
{
"role" : "readWrite",
"db" : "cndba"
}
]
}
rs0:PRIMARY>
主库操作:
rs0:PRIMARY> use cndba
switched to db cndba
rs0:PRIMARY> db.ustc.insert({name:'cndba',url:'https://www.cndba.cn'})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.ustc.find()
{ "_id" : ObjectId("626decb45f444096b30513c5"), "name" : "cndba", "url" : "https://www.cndba.cn" }
rs0:PRIMARY>
查看从库复制情况:
rs0:PRIMARY> rs.printSecondaryReplicationInfo()
source: 172.31.185.165:27017
syncedTo: Sun May 01 2022 10:19:37 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 172.31.185.131:27017
syncedTo: Sun May 01 2022 10:19:37 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
rs0:PRIMARY>
备库1查询:
rs0:SECONDARY> use cndba
switched to db cndba
rs0:SECONDARY> db.auth('cndba','cndba')
1
rs0:SECONDARY> db.ustc.find()
Error: error: {
"topologyVersion" : {
"processId" : ObjectId("626cec859239c22d088fc5e8"),
"counter" : NumberLong(4)
},
"operationTime" : Timestamp(1651371707, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotPrimaryNoSecondaryOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1651371707, 1),
"signature" : {
"hash" : BinData(0,"xDXZVcB4P7gdyKEJZe1ypE4n/Eg="),
"keyId" : NumberLong("7092356702216912900")
}
}
}
注意这里报了一个错误: “codeName” : “NotPrimaryNoSecondaryOk”,
实际上,从mongo shell登陆从节点,必须先执行rs.secondaryOk() 【低版本是rs.slaveOk()】后才能执行相关查询操作,对客户端应用则没有这个限制,默认可以读从,可以实现读写分离。
rs0:SECONDARY> rs.slaveOk()
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
rs0:SECONDARY> rs.secondaryOk()
rs0:SECONDARY> db.ustc.find()
{ "_id" : ObjectId("626decb45f444096b30513c5"), "name" : "cndba", "url" : "https://www.cndba.cn" }
rs0:SECONDARY>
备库2查询:
rs0:SECONDARY> use cndba
switched to db cndba
rs0:SECONDARY> db.auth('cndba','cndba')
1
rs0:SECONDARY> rs.secondaryOk();
rs0:SECONDARY> db.ustc.find()
{ "_id" : ObjectId("626decb45f444096b30513c5"), "name" : "cndba", "url" : "https://www.cndba.cn" }
rs0:SECONDARY>
mongo --port=27017 -uroot -p'zsjnhfj,hlsjyfj' admin
# db.shutdownServer()
5 副本集切换测试
- 关闭主库
rs0:PRIMARY> db.shutdownServer()
server should be down...
这里也可以使用如下命令主动降级:
rs.stepDown()
- 在从库查询
可以看到原主库状态已经变成:
"stateStr" : "(not reachable/healthy)"。
rs0:PRIMARY> use admin
switched to db admin
rs0:PRIMARY> db.auth('root','root')
1
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2022-05-01T03:21:15.702Z"),
"myState" : 1,
"term" : NumberLong(2),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1651375272, 1),
"t" : NumberLong(2)
},
"lastCommittedWallTime" : ISODate("2022-05-01T03:21:12.517Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1651375272, 1),
"t" : NumberLong(2)
},
"readConcernMajorityWallTime" : ISODate("2022-05-01T03:21:12.517Z"),
"appliedOpTime" : {
"ts" : Timestamp(1651375272, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1651375272, 1),
"t" : NumberLong(2)
},
"lastAppliedWallTime" : ISODate("2022-05-01T03:21:12.517Z"),
"lastDurableWallTime" : ISODate("2022-05-01T03:21:12.517Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1651375252, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "stepUpRequestSkipDryRun",
"lastElectionDate" : ISODate("2022-05-01T02:44:02.449Z"),
"electionTerm" : NumberLong(2),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(1651373037, 1),
"t" : NumberLong(1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1651373037, 1),
"t" : NumberLong(1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"priorPrimaryMemberId" : 0,
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2022-05-01T02:44:02.462Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2022-05-01T02:44:02.563Z")
},
"electionParticipantMetrics" : {
"votedForCandidate" : true,
"electionTerm" : NumberLong(1),
"lastVoteDate" : ISODate("2022-04-30T11:26:16.200Z"),
"electionCandidateMemberId" : 0,
"voteReason" : "",
"lastAppliedOpTimeAtElection" : {
"ts" : Timestamp(1651317965, 1),
"t" : NumberLong(-1)
},
"maxAppliedOpTimeInSet" : {
"ts" : Timestamp(1651317965, 1),
"t" : NumberLong(-1)
},
"priorityAtElection" : 1
},
"members" : [
{
"_id" : 0,
"name" : "172.31.185.120:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastAppliedWallTime" : ISODate("2022-05-01T02:43:57.685Z"),
"lastDurableWallTime" : ISODate("2022-05-01T02:43:57.685Z"),
"lastHeartbeat" : ISODate("2022-05-01T03:21:15.561Z"),
"lastHeartbeatRecv" : ISODate("2022-05-01T02:44:02.963Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Error connecting to 172.31.185.120:27017 :: caused by :: Connection refused",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1
},
{
"_id" : 1,
"name" : "172.31.185.165:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 69670,
"optime" : {
"ts" : Timestamp(1651375272, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2022-05-01T03:21:12Z"),
"lastAppliedWallTime" : ISODate("2022-05-01T03:21:12.517Z"),
"lastDurableWallTime" : ISODate("2022-05-01T03:21:12.517Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1651373042, 1),
"electionDate" : ISODate("2022-05-01T02:44:02Z"),
"configVersion" : 1,
"configTerm" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "172.31.185.131:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 57309,
"optime" : {
"ts" : Timestamp(1651375272, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1651375272, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2022-05-01T03:21:12Z"),
"optimeDurableDate" : ISODate("2022-05-01T03:21:12Z"),
"lastAppliedWallTime" : ISODate("2022-05-01T03:21:12.517Z"),
"lastDurableWallTime" : ISODate("2022-05-01T03:21:12.517Z"),
"lastHeartbeat" : ISODate("2022-05-01T03:21:14.516Z"),
"lastHeartbeatRecv" : ISODate("2022-05-01T03:21:15.598Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "172.31.185.165:27017",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 2
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1651375272, 1),
"signature" : {
"hash" : BinData(0,"g+0lD1JFvtD8ON/XbiMCnV8Zvtw="),
"keyId" : NumberLong("7092356702216912900")
}
},
"operationTime" : Timestamp(1651375272, 1)
}
rs0:PRIMARY>
在新主库插入文档:
rs0:PRIMARY> use cndba
switched to db cndba
rs0:PRIMARY> db.auth('cndba','cndba')
1
rs0:PRIMARY> db.ustc.insert({name:'cndba',url:'https://www.cndba.cn'})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.ustc.find()
{ "_id" : ObjectId("626decb45f444096b30513c5"), "name" : "cndba", "url" : "https://www.cndba.cn" }
{ "_id" : ObjectId("626dfd8c244fe1322d79fc76"), "name" : "cndba", "url" : "https://www.cndba.cn" }
rs0:PRIMARY>
查看从库同步
rs0:SECONDARY> rs.secondaryOk()
rs0:SECONDARY> db.ustc.find()
{ "_id" : ObjectId("626decb45f444096b30513c5"), "name" : "cndba", "url" : "https://www.cndba.cn" }
{ "_id" : ObjectId("626dfd8c244fe1322d79fc76"), "name" : "cndba", "url" : "https://www.cndba.cn" }
rs0:SECONDARY>
启动原主库并查看状态:
[dave@www.cndba.cn_1 ~]# service mongodb start
about to fork child process, waiting until server is ready for connections.
forked process: 32149
child process started successfully, parent exiting
[dave@www.cndba.cn_1 ~]#
rs0:PRIMARY> rs.printSecondaryReplicationInfo()
source: 172.31.185.120:27017
syncedTo: Sun May 01 2022 11:28:52 GMT+0800 (CST)
10 secs (0 hrs) behind the primary
source: 172.31.185.131:27017
syncedTo: Sun May 01 2022 11:28:52 GMT+0800 (CST)
10 secs (0 hrs) behind the primary
rs0:PRIMARY> rs.printSecondaryReplicationInfo()
source: 172.31.185.120:27017
syncedTo: Sun May 01 2022 11:29:12 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 172.31.185.131:27017
syncedTo: Sun May 01 2022 11:29:02 GMT+0800 (CST)
10 secs (0 hrs) behind the primary
rs0:PRIMARY> rs.printSecondaryReplicationInfo()
source: 172.31.185.120:27017
syncedTo: Sun May 01 2022 11:29:12 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 172.31.185.131:27017
syncedTo: Sun May 01 2022 11:29:12 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
rs0:PRIMARY>
刷新几次后同步正常。
rs0:SECONDARY> rs.secondaryOk()
rs0:SECONDARY> db.ustc.find()
{ "_id" : ObjectId("626decb45f444096b30513c5"), "name" : "cndba", "url" : "https://www.cndba.cn" }
{ "_id" : ObjectId("626dfd8c244fe1322d79fc76"), "name" : "cndba", "url" : "https://www.cndba.cn" }
rs0:SECONDARY>
6 添加副本节点
添加节点之前需要在新的节点上安装好MongoDB 软件,具体步骤参考之前的博客:
Redhat 7.7 平台 MongoDB 4.4.6 安装 配置 手册
https://www.cndba.cn/dave/article/4542
然后将原环境的/data/mongodb/etc 目录下的2个文件复制过来:
[dave@www.cndba.cn_1 local]# cd /data/mongodb/etc
[dave@www.cndba.cn_1 etc]# ll
total 8
-rw-r--r-- 1 root root 340 Apr 30 15:59 mongo.conf
-rw------- 1 root root 1020 Apr 30 13:50 mongo.keyfile
[dave@www.cndba.cn_1 etc]# scp * mongodb4:`pwd`
dave@www.cndba.cn_4's password:
mongo.conf 100% 340 174.7KB/s 00:00
mongo.keyfile 100% 1020 714.6KB/s 00:00
[dave@www.cndba.cn_1 etc]#
最后启动实例。
[dave@www.cndba.cn_4 etc]# service mongodb start
about to fork child process, waiting until server is ready for connections.
forked process: 2344
child process started successfully, parent exiting
[dave@www.cndba.cn_4 etc]#
将实例添加到现有副本集中,在PRIMARY 节点操作:
[dave@www.cndba.cn_2 ~]# mongo
MongoDB shell version v4.4.13
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("68e67f1f-0755-4f50-b525-694c07133e43") }
MongoDB server version: 4.4.13
rs0:PRIMARY> use admin
switched to db admin
rs0:PRIMARY> db.auth('root','root')
1
rs0:PRIMARY> rs.add("172.30.34.0:27017")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1651377199, 1),
"signature" : {
"hash" : BinData(0,"XPpPLivCuasHFl1aur5MwKpQcik="),
"keyId" : NumberLong("7092356702216912900")
}
},
"operationTime" : Timestamp(1651377199, 1)
}
rs0:PRIMARY> rs.status()
rs0:PRIMARY> rs.printReplicationInfo()
configured oplog size: 2349.000781059265MB
log length start to end: 59497secs (16.53hrs)
oplog first event time: Sat Apr 30 2022 19:26:05 GMT+0800 (CST)
oplog last event time: Sun May 01 2022 11:57:42 GMT+0800 (CST)
now: Sun May 01 2022 11:57:51 GMT+0800 (CST)
rs0:PRIMARY> rs.printSecondaryReplicationInfo()
source: 172.31.185.120:27017
syncedTo: Sun May 01 2022 11:58:02 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 172.31.185.131:27017
syncedTo: Sun May 01 2022 11:58:02 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 172.30.34.0:27017
syncedTo: Sun May 01 2022 11:58:02 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
rs0:PRIMARY>
7 删除副本节点
rs0:PRIMARY> rs.remove("172.30.34.0:27017")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1651377518, 1),
"signature" : {
"hash" : BinData(0,"6ac0CW6fsCbbg8iW83ZEHgbMW1U="),
"keyId" : NumberLong("7092356702216912900")
}
},
"operationTime" : Timestamp(1651377518, 1)
}
rs0:PRIMARY> rs.printSecondaryReplicationInfo()
source: 172.31.185.120:27017
syncedTo: Sun May 01 2022 11:58:38 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 172.31.185.131:27017
syncedTo: Sun May 01 2022 11:58:38 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
rs0:PRIMARY>
8 副本集的相关函数
直接使用help命令查看:
rs0:PRIMARY> rs.help()
rs.status() { replSetGetStatus : 1 } checks repl set status
rs.initiate() { replSetInitiate : null } initiates set with default settings
rs.initiate(cfg) { replSetInitiate : cfg } initiates set with configuration cfg
rs.conf() get the current configuration object from local.system.replset
rs.reconfig(cfg, opts) updates the configuration of a running replica set with cfg, using the given opts (disconnects)
rs.reconfigForPSASet(memberIndex, cfg, opts) updates the configuration of a Primary-Secondary-Arbiter (PSA) replica set while preserving majority writes
memberIndex: index of the node being updated; cfg: the desired new config; opts: options passed in with the reconfig
Not to be used with every configuration
For more information, visit: https://docs.mongodb.com/manual/reference/method/rs.reconfigForPSASet/
rs.add(hostportstr) add a new member to the set with default attributes (disconnects)
rs.add(membercfgobj) add a new member to the set with extra attributes (disconnects)
rs.addArb(hostportstr) add a new member which is arbiterOnly:true (disconnects)
rs.stepDown([stepdownSecs, catchUpSecs]) step down as primary (disconnects)
rs.syncFrom(hostportstr) make a secondary sync from the given member
rs.freeze(secs) make a node ineligible to become primary for the time specified
rs.remove(hostportstr) remove a host from the replica set (disconnects)
rs.secondaryOk() allow queries on secondary nodes
rs.printReplicationInfo() check oplog size and time range
rs.printSecondaryReplicationInfo() check replica set members and replication lag
db.isMaster() check who is primary
db.hello() check who is primary
reconfiguration helpers disconnect from the database so the shell will display
an error, even if the command succeeds.
rs0:PRIMARY>
版权声明:本文为博主原创文章,未经博主允许不得转载。



