docker学习之mongo集群(复制集)搭建

1.运行三个容器

# 运行三个容器
root@iZwz9434lxf5ptexiu13giZ:~# docker run --name m3 -p 57017:27017 -d mongo --replSet "rs"  # 指定复制集的名称
8ef9fcffdbabbe62d77672528ea008e8429de57858c7115f03bc5ea5094fe570
root@iZwz9434lxf5ptexiu13giZ:~# docker run --name m2 -p 47017:27017 -d mongo --replSet "rs"
cf2c41c04a79fac5df8711ca53c650d52c76f5532320cc8c24efc6f854341887
root@iZwz9434lxf5ptexiu13giZ:~# docker run --name m1 -p 37017:27017 -d mongo --replSet "rs"
39cf4ca9766bd334181eacbe41f0ffbfad83676ece9ff78afa0bac6e74e0efc7

2.进入任意容器

复制代码
# 进入其中一个容器
root@iZwz9434lxf5ptexiu13giZ:~# docker exec -it m2 /bin/bash
root@cf2c41c04a79:/# mongo
MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9677e9f0-5070-4c1f-9d86-7e54d1538ecd") }
MongoDB server version: 4.4.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2021-05-24T15:05:16.337+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-05-24T15:05:17.094+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
复制代码

3. 配置复制集

复制代码
# 配置主从节点
> rs.initiate({"_id":"rs",members:[{_id:0,host:"172.17.0.1:37017"},{_id:1,host:"172.17.0.1:47017"},{_id:2,host:"172.17.0.1:57017"}]})  # 如果想通过robot3T进行复制集连接,这里的host应该配置成公网IP(我配置的是阿里云服务器)
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1621868882, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1621868882, 1)
}
# 从节点会自动选举到主节点

rs:SECONDARY> 
复制代码

4. 查看状态

复制代码
rs:PRIMARY> rs.status()
{
        "set" : "rs",
        "date" : ISODate("2021-05-24T15:08:27.963Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1621868894, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2021-05-24T15:08:14.569Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1621868894, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2021-05-24T15:08:14.569Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1621868894, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1621868894, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2021-05-24T15:08:14.569Z"),
                "lastDurableWallTime" : ISODate("2021-05-24T15:08:14.569Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1621868892, 3),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2021-05-24T15:08:12.941Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1621868882, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2021-05-24T15:08:12.992Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2021-05-24T15:08:14.488Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.17.0.1:37017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 25,
                        "optime" : {
                                "ts" : Timestamp(1621868894, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1621868894, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-05-24T15:08:14Z"),
                        "optimeDurableDate" : ISODate("2021-05-24T15:08:14Z"),
                        "lastHeartbeat" : ISODate("2021-05-24T15:08:26.955Z"),
                        "lastHeartbeatRecv" : ISODate("2021-05-24T15:08:26.473Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "172.17.0.1:47017",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                },
                {
                        "_id" : 1,
                        "name" : "172.17.0.1:47017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 191,
                        "optime" : {
                                "ts" : Timestamp(1621868894, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-05-24T15:08:14Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "Could not find member to sync from",
                        "electionTime" : Timestamp(1621868892, 1),
                        "electionDate" : ISODate("2021-05-24T15:08:12Z"),
                        "configVersion" : 1,
                        "configTerm" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 2,
                        "name" : "172.17.0.1:57017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 25,
                        "optime" : {
                                "ts" : Timestamp(1621868894, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1621868894, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-05-24T15:08:14Z"),
                        "optimeDurableDate" : ISODate("2021-05-24T15:08:14Z"),
                        "lastHeartbeat" : ISODate("2021-05-24T15:08:26.960Z"),
                        "lastHeartbeatRecv" : ISODate("2021-05-24T15:08:26.471Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "172.17.0.1:47017",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1621868894, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1621868894, 1)
}
复制代码

 

 

 

 

 

# TODO

posted @   张京墨  阅读(296)  评论(0编辑  收藏  举报
编辑推荐:
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
阅读排行:
· “你见过凌晨四点的洛杉矶吗?”--《我们为什么要睡觉》
· 编程神器Trae:当我用上后,才知道自己的创造力被低估了多少
· C# 从零开始使用Layui.Wpf库开发WPF客户端
· C#/.NET/.NET Core技术前沿周刊 | 第 31 期(2025年3.17-3.23)
· 接口重试的7种常用方案!
点击右上角即可分享
微信分享提示