docker-compose MongoDB 错误:尝试在主机上运行命令“isMaster”时出现网络错误

Man*_* V. 3 mongodb docker docker-compose

我正在尝试使用 mongo 从外部连接到 docker 容器中的 mongodb,但出现以下错误。

MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017
2018-05-30T14:54:13.950+0200 E QUERY    [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:27017'  :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed
Run Code Online (Sandbox Code Playgroud)

当我猛击容器时一切都很好。它可能在半年前工作,但现在不会了。我认为这是 docker-compose 的问题,但我不明白。

shell 和服务器的版本是一样的。为了测试这是我的 docker-compose.yaml

version: '3'
networks:
  backend:
    external:
      name: my-mongo-cluster

services:

  ## Config Servers
  config01:
    image: mongo
    command: mongod --port 27017 --configsvr --replSet configserver --noprealloc --smallfiles --oplogSize 16 
    volumes:
      - ./scripts:/scripts
    networks: 
      - backend

  config02:
    image: mongo
    command: mongod --port 27017 --configsvr --replSet configserver --noprealloc --smallfiles --oplogSize 16 
    volumes:
      - ./scripts:/scripts
    networks: 
      - backend

  config03:
    image: mongo
    command: mongod --port 27017 --configsvr --replSet configserver --noprealloc --smallfiles --oplogSize 16 
    volumes:
      - ./scripts:/scripts
    networks: 
      - backend

  ## Shards
  shard01a:
    image: mongo
    command: mongod --port 27018 --shardsvr --replSet shard01 --noprealloc --smallfiles --oplogSize 16 
    volumes:
      - ./scripts:/scripts
    networks: 
      - backend

  shard01b:
    image: mongo
    command: mongod --port 27018 --shardsvr --replSet shard01 --noprealloc --smallfiles --oplogSize 16 
    volumes:
      - ./scripts:/scripts
    networks: 
      - backend

  shard02a:
    image: mongo
    command: mongod --port 27019 --shardsvr --replSet shard02 --noprealloc --smallfiles --oplogSize 16 
    volumes:
      - ./scripts:/scripts
    networks: 
      - backend

  shard02b:
    image: mongo
    command: mongod --port 27019 --shardsvr --replSet shard02 --noprealloc --smallfiles --oplogSize 16 
    volumes:
      - ./scripts:/scripts
    networks: 
      - backend

  shard03a:
    image: mongo
    command: mongod --port 27020 --shardsvr --replSet shard03 --noprealloc --smallfiles --oplogSize 16 
    volumes:
      - ./scripts:/scripts
    networks: 
      - backend

  shard03b:
    image: mongo
    command: mongod --port 27020 --shardsvr --replSet shard03 --noprealloc --smallfiles --oplogSize 16 
    volumes:
      - ./scripts:/scripts
    networks: 
      - backend


  ## Router
  router:
    image: mongo
    command: mongos --port 27017 --configdb configserver/config01:27017,config02:27017,config03:27017 
    ports:
      - "27017:27017"
    volumes:
      - ./scripts:/scripts
    networks: 
      - backend
    depends_on:
      - config01
      - config02
      - config03
      - shard01a
      - shard01b
      - shard02a
      - shard02b
      - shard03a
      - shard03b
Run Code Online (Sandbox Code Playgroud)

小智 10

我遇到了同样的问题,我通过向 mongos 添加以下关键字来解决它:-- bind_ip_all

所以改变:

mongos --port 27017 --configdb configserver/config01:27017,config02:27017,config03:27017 
Run Code Online (Sandbox Code Playgroud)

到:

 mongos --bind_ip_all --port 27017 --configdb configserver/config01:27017,config02:27017,config03:27017
Run Code Online (Sandbox Code Playgroud)