作为参考,我尝试了以下链接的想法,但没有结果:
Docker-复合无法连接到任何指定的MySQL主机 从Web核心容器连接到.Net容器?如何获得叶问地址?
我有三个容器化应用程序: mysql@8.0“裸露”--因为没有更好的术语--隐藏端口9999;.NET Core 3.1 WebAPI;以及容器化的角应用程序。角形应用程序可以成功地调用端口5001后面的WebAPI。问题似乎是web与MySQL容器建立了连接。
所有的应用程序都是作为容器部署在我的本地开发工作站上。-- web和MySQL db --都是用一个docker-compose.yml部署的,我在下面共享了这些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: bridgeWeb代码对我在代码中使用的DbContext使用以下连接字符串:
"ConnectionStrings": {
"DefaultConnection": "server=localhost;port=9999;uid=root;pwd=*****;database=SoarDb"
}注意,连接字符串中的port与我在docker-compose中映射的内容匹配。我也尝试过使用3306和33060,但都没有效果。
我也尝试过使用127.0.0.1作为server值,但没有得到任何结果。
在web容器中记录的错误如下:
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 migration和dotnet-ef database update更新数据库到这个特定的容器数据库中。
任何洞察力都会受到极大的赞赏。我尝试了许多不同的设置排列和调整没有运气,不知道我是什么误解。
发布于 2020-08-21 19:07:59
您的错误在于,从soar-api容器的角度来看,localhost只是指容器(在容器中运行soar-api ).不是服务器停靠程序正在运行(这是下一层)。
相反,您应该能够将连接字符串设置为server=db;port=3306;...,这是因为docker提供了一个DNS代理,它允许您在同一个网络上按名称访问容器(看起来您已经用soar-network正确地设置了容器)
在实践中,容器db获得IP (例如: A),而容器soar-api获得另一个IP (B)。您来自B的连接需要指定IP地址A,除非您将您的端口-组合配置为指定(您可以也要这么做,但正如您编写的那样,对接器将为您处理它),否则您将无法知道该地址。
我猜想您是在主服务器上运行迁移,而不是在两个容器中运行迁移。
如果没有其他服务需要直接访问MySQL (这是外部计算机连接到停靠服务器并访问服务),则可能不需要在docker-compose中公开它。
注127.0.0.1 (实际上是127.0.0.0/8空间中的任何地址)是localhost的同义词。还有::1/128 (IPv6,如果启用的话)
https://stackoverflow.com/questions/63528646
复制相似问题