【问题标题】:Can't connect to MongoDB running from docker-compose无法连接到从 docker-compose 运行的 MongoDB
【发布时间】:2021-02-27 19:49:59
【问题描述】:

我无法连接到容器化的 mongodb 实例,来自应用程序或客户端的身份验证失败 (Robo 3T)

这是我的 docker compose 文件:

    version: '3.4'

services:
  db:
    container_name : mongo
    image: mongo
    volumes:
        - c:/data/db:/data/db
        - c:/data/configdb:/data/configdb
    ports:
          - '27017:27017'
    restart: always
    environment:
      AUTH: "yes"
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: dBPassword01
    command: mongod
    networks:
        clusternetwork:
            ipv4_address: 172.16.0.2

  calculatorservice:
    image: ${DOCKER_REGISTRY-}calculatorservice
    build:
      context: .
      dockerfile: CalculatorService/Dockerfile
    depends_on:
            - db
    networks:
        clusternetwork:
            ipv4_address: 172.16.0.3

networks:
  clusternetwork:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.16.0.0/24

在我的应用程序设置中,我有:

"MongoDbSettings": {
    "ConnectionString": "mongodb://dbroot:dBPassword01@172.16.0.2:27017",
    "DatabaseName": "calculator-service"
  }

但它失败并出现以下错误:

MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.
 ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed..
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Authentication.SaslAuthenticator.AuthenticateAsync(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)

使用客户端 (Robo 3T) 我遇到了同样的身份验证问题:

我在这里错过了什么?

【问题讨论】:

  • 在您的配置中: MONGO_INITDB_ROOT_USERNAME: root,那么为什么要尝试连接 dbroot?

标签: mongodb docker docker-compose


【解决方案1】:

在您的配置中:

MONGO_INITDB_ROOT_USERNAME: root

那么为什么你尝试使用用户名dbroot 连接?我会尝试使用相同的用户名root...

【讨论】:

    【解决方案2】:

    我的 Docker 也有类似的问题。事实证明,我正在连接到 localhost 实例,而不是 docker 实例,因为端口是相同的。 我收到Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1 因为用户不匹配,但连接成功。 (现在听起来很有趣)

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,但我的 root 用户名配置得很好。问题是我在 windowsmongo 容器上运行了 robo 3t,在 wsl 内运行。

      解决方案是获取我的 wsl inet ip(在 ubuntu 中为 ifconfig 或在 Windows 中为 wsl hostname -I),然后连接到该 IP 而不是 localhost

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-28
        • 2020-04-23
        • 1970-01-01
        • 2015-05-19
        • 2023-03-15
        • 2023-01-04
        • 2020-03-06
        相关资源
        最近更新 更多