MongoDB入门到进阶笔记:07-Mongo 副本集的搭建 添加 删除 延时 仲裁 rs.status replSetName
mongodb单台服务器
数据会有丢失的风险
单台服务器无法做高可用性
mongodb副本集能够预防数据丢失,多台mongodb数据一致
mongodb副本集能够在有问题的时候自动切换
实战说明
使用三台服务器实战mongodb副本集,生产环境中建议至少三台服务器,本次使用三个容器
docker run –name mongo1 –privileged –ip 172.18.0.10 -p 30122:22 -p 27117:27117 -v /data/docker_data/Mongo1:/data –network=n-mongo -itd centos7 /usr/sbin/init
docker run –name mongo2 –privileged –ip 172.18.0.11 -p 30222:22 -p 27217:27217 -v /data/docker_data/Mongo2:/data –network=n-mongo -itd centos7 /usr/sbin/init
docker run –name mongo3 –privileged –ip 172.18.0.12 -p 30322:22 -p 27317:27317 -v /data/docker_data/Mongo3:/data –network=n-mongo -itd centos7 /usr/sbin/init
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3dccec9ff0da centos7 “/usr/sbin/init” 4 hours ago Up 6 seconds 0.0.0.0:27317->27317/tcp, 0.0.0.0:30322->22/tcp mongo3
b48bee1209ef centos7 “/usr/sbin/init” 4 hours ago Up 3 seconds 0.0.0.0:27217->27217/tcp, 0.0.0.0:30222->22/tcp mongo2
5e7b4039fa6f centos7 “/usr/sbin/init” 4 hours ago Up 4 seconds 0.0.0.0:27117->27117/tcp, 0.0.0.0:30122->22/tcp mongo1
三台服务器的ip为:
172.18.0.10
172.18.0.11
172.18.0.12
集群搭建实战
172.18.0.10:27117
172.18.0.11:27217
172.18.0.12:27317
主节点模拟数据
for(i=1; i<=10000;i++){
db.myuser.insert( {name:’mytest’+i, age:i} )
}
解压 mongodb
【mongo1、mongo2、mongo3】
创建 data logs 目录
mongodb副本集配置文件
mongo1
[root@685e6baa229d mongodb]# more mongod.cnf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongodb.log
storage:
dbPath: /data/mongodb/data
journal:
enabled: true
processManagement:
fork: true
net:
port: 27117
bindIp: 0.0.0.0
replication:
replSetName: replSet
mongo2
[root@685e6baa229d mongodb]# more mongod.cnf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongodb.log
storage:
dbPath: /data/mongodb/data
journal:
enabled: true
processManagement:
fork: true
net:
port: 27217
bindIp: 0.0.0.0
replication:
replSetName: replSet
mongo3
[root@685e6baa229d mongodb]# more mongod.cnf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongodb.log
storage:
dbPath: /data/mongodb/data
journal:
enabled: true
processManagement:
fork: true
net:
port: 27317
bindIp: 0.0.0.0
replication:
replSetName: replSet
启动三个mongodb服务器
需要对应更改端口、数据目录、日志路径
【mongo1、mongo2、mongo3】
mongod -f mongod.cnf
mongodb副本集的初始化及其状态查看
config = { _id:”replSet”, members:[
{_id:0,host:”172.18.0.10:27117″},
{_id:1,host:”172.18.0.11:27217″},
{_id:2,host:”172.18.0.12:27317″}]
}
config = { _id:”replSet”, members:[
… {_id:0,host:”172.18.0.10:27117″},
… {_id:1,host:”172.18.0.11:27217″},
… {_id:2,host:”172.18.0.12:27317″}]
… }
{
“_id” : “replSet”,
“members” : [
{
“_id” : 0,
“host” : “172.18.0.10:27117”
},
{
“_id” : 1,
“host” : “172.18.0.11:27217”
},
{
“_id” : 2,
“host” : “172.18.0.12:27317”
}
]
}
use admin
rs.initiate( config )#副本集初始化,需要一定时间
rs.initiate(config)
{ “ok” : 1 }
rs.status()#副本集状态,一个primary,其它SECONDARY。primary是主,只有primary能写入
replSet:SECONDARY> rs.status
function() {
return db._adminCommand(“replSetGetStatus”);
}
replSet:SECONDARY> rs.status()
{
“set” : “replSet”,
“date” : ISODate(“2022-11-02T12:22:51.726Z”),
“myState” : 1,
“term” : NumberLong(1),
“syncSourceHost” : “”,
“syncSourceId” : -1,
“heartbeatIntervalMillis” : NumberLong(2000),
“majorityVoteCount” : 2,
“writeMajorityCount” : 2,
“votingMembersCount” : 3,
“writableVotingMembersCount” : 3,
“optimes” : {
“lastCommittedOpTime” : {
“ts” : Timestamp(1667391760, 1),
“t” : NumberLong(-1)
},
“lastCommittedWallTime” : ISODate(“2022-11-02T12:22:40.450Z”),
“readConcernMajorityOpTime” : {
“ts” : Timestamp(1667391760, 1),
“t” : NumberLong(-1)
},
“appliedOpTime” : {
“ts” : Timestamp(1667391771, 8),
“t” : NumberLong(1)
},
“durableOpTime” : {
“ts” : Timestamp(1667391771, 8),
“t” : NumberLong(1)
},
“lastAppliedWallTime” : ISODate(“2022-11-02T12:22:51.602Z”),
“lastDurableWallTime” : ISODate(“2022-11-02T12:22:51.602Z”)
},
“lastStableRecoveryTimestamp” : Timestamp(1667391760, 1),
“electionCandidateMetrics” : {
“lastElectionReason” : “electionTimeout”,
“lastElectionDate” : ISODate(“2022-11-02T12:22:51.544Z”),
“electionTerm” : NumberLong(1),
“lastCommittedOpTimeAtElection” : {
“ts” : Timestamp(1667391760, 1),
“t” : NumberLong(-1)
},
“lastSeenOpTimeAtElection” : {
“ts” : Timestamp(1667391760, 1),
“t” : NumberLong(-1)
},
“numVotesNeeded” : 2,
“priorityAtElection” : 1,
“electionTimeoutMillis” : NumberLong(10000),
“numCatchUpOps” : NumberLong(0),
“newTermStartDate” : ISODate(“2022-11-02T12:22:51.580Z”)
},
“members” : [
{
“_id” : 0,
“name” : “172.18.0.10:27117”,
“health” : 1,
“state” : 1,
“stateStr” : “PRIMARY”,
“uptime” : 302,
“optime” : {
“ts” : Timestamp(1667391771, 8),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2022-11-02T12:22:51Z”),
“lastAppliedWallTime” : ISODate(“2022-11-02T12:22:51.602Z”),
“lastDurableWallTime” : ISODate(“2022-11-02T12:22:51.602Z”),
“syncSourceHost” : “”,
“syncSourceId” : -1,
“infoMessage” : “Could not find member to sync from”,
“electionTime” : Timestamp(1667391771, 1),
“electionDate” : ISODate(“2022-11-02T12:22:51Z”),
“configVersion” : 1,
“configTerm” : 1,
“self” : true,
“lastHeartbeatMessage” : “”
},
{
“_id” : 1,
“name” : “172.18.0.11:27217”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 11,
“optime” : {
“ts” : Timestamp(1667391760, 1),
“t” : NumberLong(-1)
},
“optimeDurable” : {
“ts” : Timestamp(1667391760, 1),
“t” : NumberLong(-1)
},
“optimeDate” : ISODate(“2022-11-02T12:22:40Z”),
“optimeDurableDate” : ISODate(“2022-11-02T12:22:40Z”),
“lastAppliedWallTime” : ISODate(“2022-11-02T12:22:40.450Z”),
“lastDurableWallTime” : ISODate(“2022-11-02T12:22:40.450Z”),
“lastHeartbeat” : ISODate(“2022-11-02T12:22:51.556Z”),
“lastHeartbeatRecv” : ISODate(“2022-11-02T12:22:51.570Z”),
“pingMs” : NumberLong(0),
“lastHeartbeatMessage” : “”,
“syncSourceHost” : “”,
“syncSourceId” : -1,
“infoMessage” : “”,
“configVersion” : 1,
“configTerm” : 0
},
{
“_id” : 2,
“name” : “172.18.0.12:27317”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 11,
“optime” : {
“ts” : Timestamp(1667391760, 1),
“t” : NumberLong(-1)
},
“optimeDurable” : {
“ts” : Timestamp(1667391760, 1),
“t” : NumberLong(-1)
},
“optimeDate” : ISODate(“2022-11-02T12:22:40Z”),
“optimeDurableDate” : ISODate(“2022-11-02T12:22:40Z”),
“lastAppliedWallTime” : ISODate(“2022-11-02T12:22:40.450Z”),
“lastDurableWallTime” : ISODate(“2022-11-02T12:22:40.450Z”),
“lastHeartbeat” : ISODate(“2022-11-02T12:22:51.556Z”),
“lastHeartbeatRecv” : ISODate(“2022-11-02T12:22:51.558Z”),
“pingMs” : NumberLong(0),
“lastHeartbeatMessage” : “”,
“syncSourceHost” : “”,
“syncSourceId” : -1,
“infoMessage” : “”,
“configVersion” : 1,
“configTerm” : 0
}
],
“ok” : 1,
“$clusterTime” : {
“clusterTime” : Timestamp(1667391771, 8),
“signature” : {
“hash” : BinData(0,”AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
},
“operationTime” : Timestamp(1667391771, 8)
}
测试副本集的数据同步
mongo mongo1
use wyzgs
db.myuser.remove({age:{$lt:1000}})
主库执行删除:
replSet:PRIMARY> use wygzs
switched to db wygzs
replSet:PRIMARY> db.myuser.count()
10000
replSet:PRIMARY>
replSet:PRIMARY> db.myuser.remove({age:{$lt:1000}})
WriteResult({ “nRemoved” : 999 })
备库检查数据同步
rs.slaveOk()#SECONDARY需要声明是slave才能查看数据
从库无法插入数据, 会提示
“errmsg” : “not master”
“code” : 10107,
“codeName” : “NotWritablePrimary”,
replSet:SECONDARY> use wygzs
switched to db wygzs
replSet:SECONDARY>
replSet:SECONDARY>
replSet:SECONDARY> rs.slaveOk()
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
replSet:SECONDARY>
replSet:SECONDARY> db.myuser.count()
9001
replSet:SECONDARY> db.myuser.insert({name: “name1”})
WriteCommandError({
“topologyVersion” : {
“processId” : ObjectId(“63625f9ba54de6ea1ed4018c”),
“counter” : NumberLong(4)
},
“ok” : 0,
“errmsg” : “not master”,
“code” : 10107,
“codeName” : “NotWritablePrimary”,
“$clusterTime” : {
“clusterTime” : Timestamp(1667393091, 1),
“signature” : {
“hash” : BinData(0,”AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
},
“operationTime” : Timestamp(1667393091, 1)
})
查看slave的延时情况
rs.printSlaveReplicationInfo()
rs.printSecondaryReplicationInfo()
replSet:PRIMARY> rs.printSecondaryReplicationInfo()
source: 172.18.0.11:27217
syncedTo: Wed Nov 02 2022 12:46:29 GMT+0000 (UTC)
0 secs (0 hrs) behind the primary
source: 172.18.0.12:27317
syncedTo: Wed Nov 02 2022 12:46:29 GMT+0000 (UTC)
0 secs (0 hrs) behind the primary
主库模拟批量插入数据,备库会出现演示
replSet:PRIMARY> for(i=500001; i<=1000000;i++){
… db.myuser.insert({name:’mytest’+i, age:i} )
… }
replSet:PRIMARY> rs.printSecondaryReplicationInfo()
source: 172.18.0.11:27217
syncedTo: Wed Nov 02 2022 12:49:45 GMT+0000 (UTC)
2 secs (0 hrs) behind the primary
source: 172.18.0.12:27317
syncedTo: Wed Nov 02 2022 12:49:45 GMT+0000 (UTC)
2 secs (0 hrs) behind the primary