簡體   English   中英

C# and Docker - Can't connect to containerized MySQL server from containerized .NET Core 3.1 Web API

[英]C# and Docker - Can't connect to containerized MySQL server from containerized .NET Core 3.1 Web API

作為參考,我嘗試了以下鏈接中的想法無濟於事:

Docker-Compose Unable to connect to any of the specified MySQL hosts Connect to MySQL container from Web Api.Net Core Container? 如何獲得 Ip 地址?

我有三個容器化應用程序:mysql@8.0“暴露”——因為沒有更好的術語——在端口 9999 后面; 一個 .NET 核心 3.1 WebAPI; 和一個容器化的 Angular 應用程序。 Angular 應用程序可以成功調用 5001 端口后面的 WebAPI 就好了。 問題是 web API 似乎與 MySQL 容器建立連接。

所有應用程序都作為容器部署在我的本地開發工作站上。 web API 和 MySQL db 與單個docker-compose.yml共享 72AE 一起部署。 我為前端應用程序構建了一個簡單的映像,並從 Docker 命令行部署它。

這是我的docker-compose.yml和 DB 的 docker-compose.yml:

version: "3.3"

services: # list of services composing your application
  db: # the service hosting your MySQL instance
    image: mysql:8.0 # the image and tag docker will pull from docker hub
    volumes: # this section allows you to configure persistence within multiple restarts
      - db_data:/var/lib/mysql
    restart: always # if the db crash somehow, restart it
    ports:
      - "9999:3306"
    environment: # env variables, you usually set this to override existing ones
      MYSQL_ROOT_PASSWORD: *****
    networks:
      - soar-network
  soar-api: # you application service
    build: ./ # this tells docker-compose to not pull from docker hub, but to build from the Dockerfile it will find in ./
    restart: always
    ports:
      - "5001:80"
    networks:
      - soar-network
    # set a dependency between your service and the database:
    # this means that your application will not run if the db service is not running,
    # but it doesn't assure you that the dabase will be ready to accept incoming connection
    # (so your application could crash untill the db initializes itself)
    depends_on:
      - db

volumes:
  db_data: # this tells docker-compose to save your data in a generic docker volume. You can see existing volumes typing 'docker volume ls'

networks:
  soar-network:
    driver: bridge

Web API 代碼對我在代碼中使用的DbContext使用以下連接字符串:

  "ConnectionStrings": {
    "DefaultConnection": "server=localhost;port=9999;uid=root;pwd=*****;database=SoarDb"
  }

請注意,連接字符串中的port與我在docker-compose中映射的端口匹配。 嘗試過使用330633060 ,但無濟於事。

我也嘗試過使用127.0.0.1作為server值,但沒有成功。

web API 容器中記錄的錯誤如下:

soar-api_1  | fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
soar-api_1  |       An error occurred using the connection to database 'SoarDb' on server 'localhost'.
soar-api_1  | fail: Microsoft.EntityFrameworkCore.Query[10100]
soar-api_1  |       An exception occurred while iterating over the results of a query for context type 'DataAccess.SoarDataContext'.
soar-api_1  |       MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
soar-api_1  |          at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in C:\projects\mysqlconnector\src\MySqlConnector\Core\ServerSession.cs:line 442

另一個奇怪的事情是:我可以使用dotnet-ef add migrationdotnet-ef database update添加遷移並更新數據庫到這個特定的容器化數據庫。

非常感謝任何見解。 我嘗試了許多不同的設置排列和調整,但沒有運氣,不知道我在誤解什么。

您的錯誤是,從soar-api容器的角度來看, localhost只是指回容器(在其中運行soar-api )......而不是運行 docker 的服務器(這是下一層) .

相反,您應該能夠將連接字符串設置為server=db;port=3306;...這是因為 docker 提供了一個 DNS 代理,它允許您在同一網絡上按名稱訪問容器(看起來您已經使用soar-network正確設置)

在實踐中,容器db得到一個 IP (比如: A ),而容器soar-api得到另一個 IP ( B )。 您來自B的連接需要指定 IP 地址A ,除非您將 docker-compose 配置為指定(您也可以這樣做,但正如您編寫的那樣,Z05B6053C41A2130AFD6FC3B158BDA4E6 將為您處理)

我想您是在主服務器外部運行遷移,而不是在任何一個容器內。

如果沒有其他服務需要直接訪問它,您可能不需要在docker-compose中的9999上公開 MySQL(這是為了讓外部計算機連接到 docker-server 並訪問該服務)。

注意127.0.0.1 (實際上是127.0.0.0/8空間中的任何地址)是localhost的同義詞。 還有::1/128 (IPv6,如果啟用)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM