作为引用,我尝试了以下链接中的想法,但均无济于事:
Docker-Compose Unable to connect to any of the specified MySQL hosts
Connect to MySQL container from Web Api .Net Core Container? How to get Ip Address?
我有三个容器化的应用程序:mysql@8.0“暴露”-缺少更好的术语-位于端口9999后面; .NET Core 3.1 WebAPI;和一个容器化的Angular应用。 Angular应用程序可以成功对端口5001后面的WebAPI进行调用。问题在于,Web API似乎与MySQL容器建立了连接。
所有应用程序均作为容器部署在我的本地开发工作站上。 Web API和MySQL数据库正在使用单个docker-compose.yml进行部署,我在下面进行了共享。我为前端应用程序构建了一个简单的镜像,并从Docker命令行进行了部署。
这是我的API和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将数据库更新到此特定的容器化db。
非常感谢任何见解。我尝试了许多不同的设置和调整排列,但是没有运气,也不知道我在误解什么。

最佳答案

您的错误是,从soar-api容器的 Angular 来看,localhost仅指向容器(在其中运行soar-api的容器)……而不是在服务器docker所在的容器(这是下一层)。
相反,您应该能够将连接字符串设置为server=db;port=3306;...,这是因为docker提供了DNS代理,该代理允许您按名称访问同一网络上的容器(看起来您已经使用soar-network正确设置了)
实际上,容器db获得一个IP(例如:A),而容器soar-api获得另一个IP(B)。您与B的连接需要指定IP地址A,除非您将docker-compose配置为指定,否则您无法知道该IP地址(您可以do this too,但是正如您所写的,docker会为您处理)
我想您是在主服务器上而不是从两个容器中运行迁移。
如果不需要其他服务直接访问MySQL(这是让外部计算机连接到docker-server并访问该服务),则无需在9999docker-compose上公开MySQL。
注意127.0.0.1(实际上127.0.0.0/8空间中的任何地址)都是localhost的同义词。也是::1/128(IPv6,如果已启用)

关于c# - C#和Docker-无法从容器化的.NET Core 3.1 Web API连接到容器化的MySQL服务器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63528646/

10-11 15:16
查看更多