MongoDB Replication 副本集搭建

版本:针对MongoDB 5.0

官方文档:https://docs.mongodb.com/manual/tutorial/deploy-replica-set/#overview

第一步:准备三台服务器 (副本集应该总是有奇数个成员。确保选举的顺利进行)

192.168.223.133 PRIMARY
192.168.223.134 SECONDARY
192.168.223.135 SECONDARY

# 分别修改主机名
192.168.223.133 hostnamectl set-hostname mongodb01.node
192.168.223.134 hostnamectl set-hostname mongodb02.node
192.168.223.135 hostnamectl set-hostname mongodb03.node

# 分别配置/etc/hosts
vim /etc/hosts
    ## 在hosts文件中增加下面的配置
    192.168.223.133 mongodb01.node
    192.168.223.134 mongodb02.node
    192.168.223.135 mongodb03.node

第二步:安装MongoDB,增加副本集参数配置

# 安装mongodb
sudo yum install -y mongodb-org

# 设置防火墙
firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload

# 修改配置文件/etc/mongod.conf 
# mongod.conf
...
# 修改bindIp
net:
  port: 27017
  bindIp: localhost,mongodb01.node # 绑定本机的IP或者主机名

# 副本集配置
replication:
  oplogSizeMB: 102 # 对于64位系统,oplog通常是可用磁盘空间的5%。
  replSetName: rs0
   
# 启动/重新启动mongodb 
systemctl start mongod
systemctl restart mongod

第三步:副本集初始化以及配置

# 随便连接一个mongodb实例
mongo mongodb://mongodb01.node:27017/test

# 使用rs.help()可以查看副本集所有的操作方法
> rs.help()

# 初始化副本集 此处需要注意第一个"_id"表示副本集的名称,就是配置文件中配置的”replSetName“
> rs.initiate({_id:'rs0',members:[
    {_id:1,host:'mongodb01.node:27017'},
    {_id:3,host:'mongodb02.node:27017'},
    {_id:2,host:'mongodb03.node:27017'}
]})

# 当然也可以选择先配置一个节点 然后加入使用rs.add()加入其他节点, 如下:
> rs.initiate({_id:'rs0',members:[{_id:1,host:'mongodb01.node:27017'}]})
rs0:PRIMARY> rs.add('mongodb02.node:27017')
rs0:PRIMARY> rs.add('mongodb03.node:27017')

# 初始化以后会看到客户端shell变成了”rs0:PRIMARY“

# 使用rs.status()查看副本集群的状态 
# mongodb01.node:27017状态: "PRIMARY"
# mongodb02.node:27017和mongodb03.node:27017状态: "SECONDARY"
rs0:SECONDARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2021-12-15T07:33:26.513Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1639553603, 1),
                        "t" : NumberLong(2)
                },
                "lastCommittedWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1639553603, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1639553603, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1639553603, 1),
                        "t" : NumberLong(2)
                },
                "lastAppliedWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                "lastDurableWallTime" : ISODate("2021-12-15T07:33:23.687Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1639553549, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "stepUpRequestSkipDryRun",
                "lastElectionDate" : ISODate("2021-12-15T07:33:03.635Z"),
                "electionTerm" : NumberLong(2),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(1639553575, 1),
                        "t" : NumberLong(1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1639553575, 1),
                        "t" : NumberLong(1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "priorPrimaryMemberId" : 1,
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2021-12-15T07:33:03.671Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2021-12-15T07:33:03.699Z")
        },
        "electionParticipantMetrics" : {
                "votedForCandidate" : true,
                "electionTerm" : NumberLong(1),
                "lastVoteDate" : ISODate("2021-12-15T07:26:29.091Z"),
                "electionCandidateMemberId" : 1,
                "voteReason" : "",
                "lastAppliedOpTimeAtElection" : {
                        "ts" : Timestamp(1639553178, 1),
                        "t" : NumberLong(-1)
                },
                "maxAppliedOpTimeInSet" : {
                        "ts" : Timestamp(1639553178, 1),
                        "t" : NumberLong(-1)
                },
                "priorityAtElection" : 1
        },
        "members" : [
                {
                        "_id" : 1,
                        "name" : "mongodb01.node: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("2021-12-15T07:33:13.685Z"),
                        "lastDurableWallTime" : ISODate("2021-12-15T07:33:13.685Z"),
                        "lastHeartbeat" : ISODate("2021-12-15T07:33:25.726Z"),
                        "lastHeartbeatRecv" : ISODate("2021-12-15T07:33:18.704Z"),
                        "pingMs" : NumberLong(1),
                        "lastHeartbeatMessage" : "Error connecting to mongodb01.node:27017 (192.168.223.133:27017) :: caused by :: Connection refused",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 2
                },
                {
                        "_id" : 2,
                        "name" : "mongodb03.node:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 428,
                        "optime" : {
                                "ts" : Timestamp(1639553603, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1639553603, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2021-12-15T07:33:23Z"),
                        "optimeDurableDate" : ISODate("2021-12-15T07:33:23Z"),
                        "lastAppliedWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                        "lastDurableWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                        "lastHeartbeat" : ISODate("2021-12-15T07:33:25.715Z"),
                        "lastHeartbeatRecv" : ISODate("2021-12-15T07:33:25.714Z"),
                        "pingMs" : NumberLong(1),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "mongodb02.node:27017",
                        "syncSourceId" : 3,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 2
                },
                {
                        "_id" : 3,
                        "name" : "mongodb02.node:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 632,
                        "optime" : {
                                "ts" : Timestamp(1639553603, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2021-12-15T07:33:23Z"),
                        "lastAppliedWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                        "lastDurableWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1639553583, 1),
                        "electionDate" : ISODate("2021-12-15T07:33:03Z"),
                        "configVersion" : 1,
                        "configTerm" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1639553603, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1639553603, 1)
}

第四步:测试数据同步

## 主节点操作:
# 新建并切换数据库 my
rs0:PRIMARY> use my
# 新建集合并插入数据 
rs0:PRIMARY> db.col00.insert({name:"user01"})

## 从节点操作
# 从节点默认不能查询操作 需要执行rs.secondaryOk()允许操作
rs0:SECONDARY> rs.secondaryOk()
# 查询主节点创建的数据 如果可以查到说明搭建成功
rs0:SECONDARY> db.col00.find()

第四步:测试风险转移

## 将主节点mongodb01.node:27017实例关闭
systeml stop mongod

# 连接从节点mongodb02.node:27017查看集群状态 发现mongodb02.node:27017成为了主节点
rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2021-12-13T07:05:56.087Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1639379146, 1),
                        "t" : NumberLong(2)
                },
                "lastCommittedWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1639379146, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1639379146, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1639379146, 1),
                        "t" : NumberLong(2)
                },
                "lastAppliedWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                "lastDurableWallTime" : ISODate("2021-12-13T07:05:46.369Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1639379146, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "stepUpRequestSkipDryRun",
                "lastElectionDate" : ISODate("2021-12-13T07:05:26.351Z"),
                "electionTerm" : NumberLong(2),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(1639379123, 1),
                        "t" : NumberLong(1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1639379123, 1),
                        "t" : NumberLong(1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "priorPrimaryMemberId" : 1,
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2021-12-13T07:05:26.365Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2021-12-13T07:05:26.372Z")
        },
        "members" : [
                {
                        "_id" : 1,
                        "name" : "192.168.223.133: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("2021-12-13T07:05:36.368Z"),
                        "lastDurableWallTime" : ISODate("2021-12-13T07:05:36.368Z"),
                        "lastHeartbeat" : ISODate("2021-12-13T07:05:54.436Z"),
                        "lastHeartbeatRecv" : ISODate("2021-12-13T07:05:41.406Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Error connecting to 192.168.223.133:27017 :: caused by :: Connection refused",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 11,
                        "configTerm" : 2
                },
                {
                        "_id" : 2,
                        "name" : "192.168.223.129:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 4548,
                        "optime" : {
                                "ts" : Timestamp(1639379146, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2021-12-13T07:05:46Z"),
                        "lastAppliedWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                        "lastDurableWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1639379126, 1),
                        "electionDate" : ISODate("2021-12-13T07:05:26Z"),
                        "configVersion" : 11,
                        "configTerm" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 3,
                        "name" : "192.168.223.130:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1139,
                        "optime" : {
                                "ts" : Timestamp(1639379146, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1639379146, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2021-12-13T07:05:46Z"),
                        "optimeDurableDate" : ISODate("2021-12-13T07:05:46Z"),
                        "lastAppliedWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                        "lastDurableWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                        "lastHeartbeat" : ISODate("2021-12-13T07:05:54.414Z"),
                        "lastHeartbeatRecv" : ISODate("2021-12-13T07:05:54.419Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "192.168.223.129:27017",
                        "syncSourceId" : 2,
                        "infoMessage" : "",
                        "configVersion" : 11,
                        "configTerm" : 2
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1639379146, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1639379146, 1)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值