基于Docker搭建MongoDB复制集

内容纲要

拉去镜像

docker pull mongo:4

设置docker网络环境

docker network create <network name> 

部署mongo

部署三台服务,docker暴露端口分别为27017,27018,27019

docker run --net mongonet --name mongo1 -v E:/docker/mongo/data1:/data/db -p 27017:27017 -d mongo:4 --replSet mongoSet
docker run --net mongonet --name mongo2 -v E:/docker/mongo/data2:/data/db -p 27018:27017 -d mongo:4 --replSet mongoSet
docker run --net mongonet --name mongo3 -v E:/docker/mongo/data3:/data/db -p 27019:27017 -d mongo:4 --replSet mongoSet
  • --replSet后方参数为mongo复制集名称

设置复制集

  • 进入容器
docker exec -it mongo1 /bin/bash
  • 执行添加复制集命令
rs.initiate({
        _id : "mongoSet",
        members: [
            {_id:0,host:"mongo1:27017"},
            {_id:2,host:"mongo2:27017"},
            {_id:3,host:"mongo3:27017"}
        ]
    })
  • 验证
rs.status()
mongoSet:SECONDARY> rs.status()
{
        "set" : "mongoSet",
        "date" : ISODate("2023-07-05T12:15:41.656Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1688559332, 3),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2023-07-05T12:15:32.902Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1688559332, 3),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2023-07-05T12:15:32.902Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1688559332, 3),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1688559332, 3),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2023-07-05T12:15:32.902Z"),
                "lastDurableWallTime" : ISODate("2023-07-05T12:15:32.902Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1688559316, 4),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2023-07-05T12:15:16.080Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1688559305, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2023-07-05T12:15:16.180Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2023-07-05T12:15:17.099Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongo1:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 320,
                        "optime" : {
                                "ts" : Timestamp(1688559332, 3),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2023-07-05T12:15:32Z"),
                        "lastAppliedWallTime" : ISODate("2023-07-05T12:15:32.902Z"),
                        "lastDurableWallTime" : ISODate("2023-07-05T12:15:32.902Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1688559316, 1),
                        "electionDate" : ISODate("2023-07-05T12:15:16Z"),
                        "configVersion" : 1,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 2,
                        "name" : "mongo2:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 36,
                        "optime" : {
                                "ts" : Timestamp(1688559332, 3),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1688559332, 3),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2023-07-05T12:15:32Z"),
                        "optimeDurableDate" : ISODate("2023-07-05T12:15:32Z"),
                        "lastAppliedWallTime" : ISODate("2023-07-05T12:15:32.902Z"),
                        "lastDurableWallTime" : ISODate("2023-07-05T12:15:32.902Z"),
                        "lastHeartbeat" : ISODate("2023-07-05T12:15:40.106Z"),
                        "lastHeartbeatRecv" : ISODate("2023-07-05T12:15:41.114Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "mongo1:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 3,
                        "name" : "mongo3:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 36,
                        "optime" : {
                                "ts" : Timestamp(1688559332, 3),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1688559332, 3),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2023-07-05T12:15:32Z"),
                        "optimeDurableDate" : ISODate("2023-07-05T12:15:32Z"),
                        "lastAppliedWallTime" : ISODate("2023-07-05T12:15:32.902Z"),
                        "lastDurableWallTime" : ISODate("2023-07-05T12:15:32.902Z"),
                        "lastHeartbeat" : ISODate("2023-07-05T12:15:40.107Z"),
                        "lastHeartbeatRecv" : ISODate("2023-07-05T12:15:41.113Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "mongo1:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1688559332, 3),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1688559332, 3)
}
THE END
分享
二维码
< <上一篇
下一篇>>