docker中部署mongodb副本集

时间:2021-06-04 20:08:01

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   //查看启动的容器

docker中部署mongodb副本集

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: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>

2.6 客户端工具连接

docker中部署mongodb副本集