【问题标题】:docker-compose MongoDB Error: network error while attempting to run command 'isMaster' on hostdocker-compose MongoDB 错误:尝试在主机上运行命令“isMaster”时出现网络错误
【发布时间】:2018-05-30 13:09:26
【问题描述】:

我正在尝试使用 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

当我猛冲到容器中时,一切都很好。 它可能在半年前工作,但现在不会了。 我认为这是 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

【问题讨论】:

  • 没有人遇到同样的问题?

标签: mongodb docker docker-compose


【解决方案1】:

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

所以 改变:

mongos --port 27017 --configdb configserver/config01:27017,config02:27017,config03:27017 

到:

 mongos --bind_ip_all --port 27017 --configdb configserver/config01:27017,config02:27017,config03:27017

【讨论】:

    【解决方案2】:

    我也遇到过类似的问题。在我的例子中,一个脚本首先启动 docker,然后使用 mongo.exe 连接到它。

    解决方案是在 mongo.exe 连接并开始执行查询之前引入 3 秒睡眠。另一种解决方法是不将 docker ip 绑定到特定的本地 ip 并允许从所有 ip 访问。然而,这对我来说不是一个选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 2020-06-24
      相关资源
      最近更新 更多