docker创建mongodb副本集

使用docker快速构建mongodb服务,单点mongodb服务不支持事务,单点服务也可以创建副本集
拉取镜像:

docker pull mongo:latest

创建3个mongodb服务:

docker run --name m0 -h 10.100.1.13 -p 37017:27017 -v /root/docker/mongo/m0-data/db:/data/db  -d mongo --replSet "rs"
docker run --name m1 -h 10.100.1.13 -p 47017:27017 -v /root/docker/mongo/m1-data/db:/data/db  -d mongo --replSet "rs"
docker run --name m2 -h 10.100.1.13 -p 57017:27017 -v /root/docker/mongo/m2-data/db:/data/db  -d mongo --replSet "rs"

-h 宿主机器 hostname
-p 端口映射
-v 存储目录映射到宿主机器
--replSet "rs" 副本集名称

没有指定-net参数,默认使用桥接模式,查看为容器分配ip地址

docker inspect m0 | grep IPAddress

docker exec -it m0 mongo --host 10.100.1.13 --port 37017

config={_id:"rs",members:[{_id:0,host:"10.100.1.13:37017"},{_id:1,host:"10.100.1.13:47017"},{_id:2,host:"10.100.1.13:57017",arbiterOnly:true}]};

rs.initiate(config)

查看副本集状态

rs.status()

rs:PRIMARY> rs.status()
{
    "set" : "rs",
    "date" : ISODate("2019-12-24T01:03:46.573Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "majorityVoteCount" : 2,
    "writeMajorityCount" : 2,
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1577149417, 1),
            "t" : NumberLong(1)
        },
        "lastCommittedWallTime" : ISODate("2019-12-24T01:03:37.287Z"),
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1577149417, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityWallTime" : ISODate("2019-12-24T01:03:37.287Z"),
        "appliedOpTime" : {
            "ts" : Timestamp(1577149417, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1577149417, 1),
            "t" : NumberLong(1)
        },
        "lastAppliedWallTime" : ISODate("2019-12-24T01:03:37.287Z"),
        "lastDurableWallTime" : ISODate("2019-12-24T01:03:37.287Z")
    },
    "lastStableRecoveryTimestamp" : Timestamp(1577149367, 1),
    "lastStableCheckpointTimestamp" : Timestamp(1577149367, 1),
    "electionCandidateMetrics" : {
        "lastElectionReason" : "electionTimeout",
        "lastElectionDate" : ISODate("2019-12-23T11:47:24.679Z"),
        "termAtElection" : NumberLong(1),
        "lastCommittedOpTimeAtElection" : {
            "ts" : Timestamp(0, 0),
            "t" : NumberLong(-1)
        },
        "lastSeenOpTimeAtElection" : {
            "ts" : Timestamp(1577101634, 1),
            "t" : NumberLong(-1)
        },
        "numVotesNeeded" : 2,
        "priorityAtElection" : 1,
        "electionTimeoutMillis" : NumberLong(10000),
        "numCatchUpOps" : NumberLong(57017),
        "newTermStartDate" : ISODate("2019-12-23T11:47:25.493Z"),
        "wMajorityWriteAvailabilityDate" : ISODate("2019-12-23T11:47:26.057Z")
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "10.100.1.13:37017",
            "ip" : "10.100.1.13",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 47854,
            "optime" : {
                "ts" : Timestamp(1577149417, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2019-12-24T01:03:37Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1577101644, 1),
            "electionDate" : ISODate("2019-12-23T11:47:24Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "10.100.1.13:47017",
            "ip" : "10.100.1.13",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 47792,
            "optime" : {
                "ts" : Timestamp(1577149417, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1577149417, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2019-12-24T01:03:37Z"),
            "optimeDurableDate" : ISODate("2019-12-24T01:03:37Z"),
            "lastHeartbeat" : ISODate("2019-12-24T01:03:45.864Z"),
            "lastHeartbeatRecv" : ISODate("2019-12-24T01:03:45.865Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "10.100.1.13:37017",
            "syncSourceHost" : "10.100.1.13:37017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "10.100.1.13:57017",
            "ip" : "10.100.1.13",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 47792,
            "lastHeartbeat" : ISODate("2019-12-24T01:03:45.863Z"),
            "lastHeartbeatRecv" : ISODate("2019-12-24T01:03:45.865Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : 1
        }
    ],
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1577149417, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1577149417, 1)
}

从上面可以看到:
PRIMARY主节点:10.100.1.13:37017
SECONDARY从节点:10.100.1.13:47017
ARBITER监控节点:10.100.1.13:57017
查看是否是主节点

rs.isMaster()

进入主节点管理控制台,在cbb这个数据库创建用户

rs:PRIMARY> use cbb
switched to db cbb
rs:PRIMARY> db.createUser({ user: "root", pwd: "xxxxxx", roles: [{ role: "dbOwner", db: "cbb" }] })

副本集搭建完毕,验证数据库cbb 账户/密码 root/xxxxxx

发表新评论