MongoDB单节点转换成复制集
背景
之前图省事,开发环境几台mongodb都是单机在运行,考虑到宕机等不确定的风险,给他们做一下高可用。mongodb原生的复制集方案非常简单好用。
操作步骤
- MongoDB版本: 3.6.21
- 实验环境中,3个mongodb实例运行在同一个虚拟机上。另外,从节约资源考虑,我们最终搭建完的结构是: 1主、1备、1仲裁节点
- 实验环境
- 当前正在运行的单机节点:10.0.3.115:27018
- 准备添加secondary节点:10.0.3.115:27019
- 准备添加arbiter节点:10.0.3.115:27020
1.关闭当前的 27018 节点
> use admin
> db.shutdownServer()
2.修改配置文件
修改 27018 的配置文件(YAML格式),在最后加上下面3行内容
replication:
oplogSizeMB: 2048
replSetName: maotiapp
3.再次启动 27018 进程
mongod -f /data/mongodb/27018/conf/mongod.conf
4.初始化集群配置
mongo --port 27018
>
> config = {_id:'maotiapp', members:[{_id:0,host:'10.0.3.115:27018'}]}
{
"_id" : "maotiapp",
"members" : [
{
"_id" : 0,
"host" : "10.0.3.115:27018"
}
]
}
> rs.initiate(config)
{ "ok" : 1 }
maotiapp:PRIMARY> rs.status()
{
"set" : "maotiapp",
"date" : ISODate("2021-01-18T09:52:34.567Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1610963543, 7),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1610963543, 7),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1610963543, 7),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1610963543, 7),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.0.3.115:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 171,
"optime" : {
"ts" : Timestamp(1610963543, 7),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-01-18T09:52:23Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1610963541, 2),
"electionDate" : ISODate("2021-01-18T09:52:21Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"operationTime" : Timestamp(1610963543, 7),
"$clusterTime" : {
"clusterTime" : Timestamp(1610963543, 7),
"signature" : {
"hash" : BinData(0,"NQlkxnscuDDzaRztIGdHu5yA3AY="),
"keyId" : NumberLong("6919035732233289729")
}
}
}
5.启动 27019 27020 实例
修改 27019 27020 配置文件与 27018 一致
启动服务
mongod -f /data/mongodb/27019/conf/mongod.conf
mongod -f /data/mongodb/27020/conf/mongod.conf
6.登录到 27018 实例里,将 27019 27020 加到集群中去
maotiapp:PRIMARY> rs.add('10.0.3.115:27019')
maotiapp:PRIMARY> rs.addArb('10.0.3.115:27020')
7. 查看集群状态
maotiapp:PRIMARY> rs.isMaster()
{
"hosts" : [
"10.0.3.115:27018",
"10.0.3.115:27019"
],
"arbiters" : [
"10.0.3.115:27020"
],
"setName" : "maotiapp",
"setVersion" : 3,
"ismaster" : true,
"secondary" : false,
"primary" : "10.0.3.115:27018",
"me" : "10.0.3.115:27018",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1610966883, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2021-01-18T10:48:03Z"),
"majorityOpTime" : {
"ts" : Timestamp(1610966883, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2021-01-18T10:48:03Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2021-01-18T10:48:06.939Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 6,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1610966883, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1610966883, 1),
"signature" : {
"hash" : BinData(0,"iiPbfyjfCmCW/Uai55twlPRmg+U="),
"keyId" : NumberLong("6919035732233289729")
}
}
}
maotiapp:PRIMARY> rs.status()
maotiapp:PRIMARY> rs.conf()
如果有数据正在同步过程中,这里可能看到新加的节点是startup或者其它的状态,稍等即可。
因为有悔,所以披星戴月;因为有梦,所以奋不顾身! 个人博客首发:easydb.net
微信公众号:easydb
关注我,不走丢!
因为有悔,所以披星戴月;因为有梦,所以奋不顾身!
个人博客首发:easydb.net
微信公众号:easydb
关注我,不走丢!