参考:https://blog.csdn.net/qq_37142346/article/details/82824132

 

 

 

docker 版本的副本集:参考:https://www.cnblogs.com/cowboys/p/9264494.html

 

参考这个,比较详细:   https://www.cnblogs.com/phpk/p/11121412.html

 

原文:https://www.cnblogs.com/cowboys/p/9264494.html

docker中部署mongodb副本集

1.基本信息如下

服务器地址 192.168.73.129
副本集名称 rs
容器节点及端口映射
        m0 37017:27017
        m1 47017:27017
        m2 57017:27017
注:机器环境安装docker

2.部署步骤

2.1下载mongo镜像

docker pull mongo

2.2 启动三个节点

docker run --name m0 -p 37017:27017 -d mongo --replSet "rs"

docker run --name m1 -p 47017:27017 -d mongo --replSet "rs"

docker run --name m2 -p 57017:27017 -d mongo --replSet "rs"

docker ps -a   //查看启动的容器

2.3 连接任意一个节点,进行副本集配置

进入其中一个容器:docker exec -it de242cc5fa5a  /bin/bash 

连接三个节点中的任意一个,注意ip地址为宿主机ip,我当前的为192.168.73.129

mongo --host 192.168.73.129 --port 37017

此时已连接到m0节点,进行副本集配置

var config={
     _id:"rs",
     members:[
         {_id:0,host:"192.168.73.129:37017"},
         {_id:1,host:"192.168.73.129:47017"},
         {_id:2,host:"192.168.73.129:57017"}
]};
rs.initiate(config)

 

或者直接:

rs.initiate({ _id:"rs", members:[{_id:0,host:"192.168.73.129:37017"},{_id:1,host:"192.168.73.129:47017"},{_id:2,host:"192.168.73.129:57017"}]})

如果是添加:   rs.add({_id:4, host: "192.168.1.28", priority: 1})

 

rs.initiate(
  {
    "_id": "rs",
    "members": [
      {
        "_id": 0,
        "host": "192.168.25.140:27017",
        "priority": 10
      },
      {
        "_id": 1,
        "host": "192.168.25.141:27017",
        "priority": 9
      },
      {
        "_id": 2,
        "host": "192.168.18.142:27017",
        "arbiterOnly": true
      }
    ]
  }
)

 

 

 

响应应该类似下面,注意此时命令提示符已经发生变化,由原来的 > 变成了 rs:SECONDARY>

{
    "ok" : 1,
    "operationTime" : Timestamp(1522810920, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1522810920, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

2.4查看副本集配置信息

rs:SECONDARY> rs.conf()
{
    "_id" : "rs",
    "version" : 1,
    "protocolVersion" : NumberLong(1),
    "writeConcernMajorityJournalDefault" : true,
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.73.129:37017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "192.168.73.129:47017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "192.168.73.129:57017",
            "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("5b3c937896d237ac24a0648e")
    }
}
rs:SECONDARY> 

2.5 查看副本集状态

rs:SECONDARY> rs.status()
{
    "set" : "rs",
    "date" : ISODate("2018-07-04T09:45:38.110Z"),
    "myState" : 2,
    "term" : NumberLong(1),
    "syncingTo" : "192.168.73.129:37017",
    "syncSourceHost" : "192.168.73.129:37017",
    "syncSourceId" : 0,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1530697530, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1530697530, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1530697530, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1530697530, 1),
            "t" : NumberLong(1)
        }
    },
    "lastStableCheckpointTimestamp" : Timestamp(1530697480, 1),
    "members" : [
        {
            "_id" : 0,
            "name" : "192.168.73.129:37017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 967,
            "optime" : {
                "ts" : Timestamp(1530697530, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1530697530, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-07-04T09:45:30Z"),
            "optimeDurableDate" : ISODate("2018-07-04T09:45:30Z"),
            "lastHeartbeat" : ISODate("2018-07-04T09:45:36.221Z"),
            "lastHeartbeatRecv" : ISODate("2018-07-04T09:45:36.296Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1530696579, 1),
            "electionDate" : ISODate("2018-07-04T09:29:39Z"),
            "configVersion" : 1
        },
        {
            "_id" : 1,
            "name" : "192.168.73.129:47017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 967,
            "optime" : {
                "ts" : Timestamp(1530697530, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1530697530, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-07-04T09:45:30Z"),
            "optimeDurableDate" : ISODate("2018-07-04T09:45:30Z"),
            "lastHeartbeat" : ISODate("2018-07-04T09:45:36.221Z"),
            "lastHeartbeatRecv" : ISODate("2018-07-04T09:45:36.219Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "192.168.73.129:37017",
            "syncSourceHost" : "192.168.73.129:37017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "192.168.73.129:57017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 2153,
            "optime" : {
                "ts" : Timestamp(1530697530, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-07-04T09:45:30Z"),
            "syncingTo" : "192.168.73.129:37017",
            "syncSourceHost" : "192.168.73.129:37017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        }
    ],
    "ok" : 1,
    "operationTime" : Timestamp(1530697530, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1530697530, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
rs:SECONDARY> 

 

 

8、从库默认没有读写权限

复制代码
1 需要进入从库的mongoDB执行rs.slaveOk();或者db.getMongo().setSlaveOk();
2 但是但是但是!!!
3 只有当次生效!只有当次生效!只有当次生效!
4 如果需要永久生效 可以依次执如下命令
5 find / -name .mongorc.js
6 vim /home/shengyang/.mongorc.js
7 添加内容
8 rs.slaveOk();
复制代码

 

强制更换主节点办法
http://www.mongoing.com/docs/tutorial/force-member-to-be-primary.html

复制代码
1 注:
2 在搭建过程中,遇到一个问题
3 当将两个从节点停掉之后 在主使用rs.stepDown(120)将现有主节点切换为从节点 导致主节点丢失,该情况下 可以将停掉的一个从节点重启,此时主节点会自动选出,一位复制集的规则是必须有两台可用的数据库,才能够自动选举主从节点。
4 另:本篇未涉及投票节点,感兴趣的可自行搜索相关文档即可
5 多讲一句:本篇为4.0.3版本,4.0版本以下的相关命令 可能会失效! //经测试,个人操作原因,需要在执行 rs.reconfig();时添加"protocolVersion" : 1  
比如这样:rs.reconfig( {_id : "rs0", "protocolVersion" : 1, members: [{ _id: 0, host: "192.168.1.64:27017",priority:2 },{ _id: 1, host: "192.168.1.50:27017" },{ _id: 2, host: "192.168.1.51:27017" }]})
如果需要非主节点执行需要添加{ "force": true } 例如 rs.reconfig(cfg, { "force": true })
复制代码

 

 

 

 

2.6 客户端工具连接

 

posted on 2019-12-09 16:49  lshan  阅读(366)  评论(0编辑  收藏  举报