搭建mongo1主2备复制集
1.启动3个mongo实例
mongod --replSet rs --port 27018 --bind_ip lzl --dbpath /data/mongo27018 --logpath /data/mongo/mongo27018.log --oplogSize 10 &
mongod --replSet rs --port 27019 --bind_ip lzl --dbpath /data/mongo27019 --logpath /data/mongo/mongo27019.log --oplogSize 10 &
mongod --replSet rs --port 27020 --bind_ip lzl --dbpath /data/mongo27020 --logpath /data/mongo/mongo27020.log --oplogSize 10 &
2.初始化rs.initiate
[root@lzl mongo]# mongo lzl:27018
>rsconf = { _id: "rs0", members: [ { _id: 0, host: "localhost:27018" }, { _id: 1, host: "localhost:27019" }, { _id: 2, host: "localhost:27020" } ] }
> rs.initiate( rsconf )
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1607587467, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1607587467, 1)
}
3. 查看副本集配置
rs:SECONDARY> rs.conf()
{
"_id" : "rs",
"version" : 1,
"term" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "lzl:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "lzl:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "lzl:27020",
"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("5fd1d68b69897b49f199c666")
}
}
rs:PRIMARY> db.isMaster();
{
"topologyVersion" : {
"processId" : ObjectId("5fd1d47a69897b49f199c650"),
"counter" : NumberLong(6)
},
"hosts" : [
"lzl:27018",
"lzl:27019",
"lzl:27020"
],
"setName" : "rs",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "lzl:27018",
"me" : "lzl:27018",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1607587699, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2020-12-10T08:08:19Z"),
"majorityOpTime" : {
"ts" : Timestamp(1607587699, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2020-12-10T08:08:19Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2020-12-10T08:08:19.535Z"),
"logicalSessionTimeoutMinutes" : 30,
"connectionId" : 1,
"minWireVersion" : 0,
"maxWireVersion" : 9,
"readOnly" : false,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1607587699, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1607587699, 1)
}
文档参考