标题: 【真坑】docker-compose搭建的mongoDB副本集群无法连接,但是可以单独连接每个节点
时间: 2022-12-28发布,2022-12-28修改
来看,docker-compose搭建 mongoDB 副本集群, 一个主节点,一个副本,一个裁判。 同一台服务器上,使用docker映射三个端口
navicat 连接集群问题,单个节点可以连,集群连不上.
version: '3.1'
services:
mongo-main:
build:
context: mongo-replica-build
#restart: always
ports:
- 27017:27017
volumes:
#- $PWD/script/:/docker-entrypoint-initdb.d/
- $PWD/mongo-main:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: pass
command: --replSet rs1 --keyFile /data/keyFile --bind_ip=0.0.0.0
mongo-1:
build:
context: mongo-replica-build
ports:
- 27018:27017
#restart: always
volumes:
- $PWD/mongo-1:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: pass
command: --replSet rs1 --keyFile /data/keyFile --bind_ip=0.0.0.0
mongo-2:
build:
context: mongo-replica-build
ports:
- 27019:27017
#restart: always
volumes:
- $PWD/mongo-2:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: pass
command: --replSet rs1 --keyFile /data/keyFile --bind_ip=0.0.0.0
二次连接
的host我这个机器是 局域网的,然后docker-compose部署的,刚才启动后,设置集群我是 rs.add( mongo-1 ) ,这个mongo-1在docker容器中是有hosts的,因为是在同一个docker-compose中,
我现在副本集群
rs.initiate({
_id: "rs1",
members:[
{ _id: 0, host: "192.168.2.201:27017" },
{ _id: 1, host: "192.168.2.201:27018" },
{ _id: 2, host: "192.168.2.201:27019", arbiterOnly: true }
]
})
添加后,我的navicat就能连上了
『回复列表(0|隐藏机器人聊天)』