如何解决C#和Docker-无法从容器化的.NET Core 3.1 Web API连接到容器化的MySQL服务器
作为参考,我尝试了以下链接中的想法,但无济于事:
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命令行进行了部署。
这是我的docker-compose.yml
,用于API和数据库:
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
对于我在代码中使用的DbContext
,Web API代码使用以下连接字符串:
"ConnectionStrings": {
"DefaultConnection": "server=localhost;port=9999;uid=root;pwd=*****;database=SoarDb"
}
请注意,连接字符串中的port
与我在docker-compose
中映射的匹配。我曾经也尝试过使用3306
和33060
,但无济于事。
我也尝试使用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 migration
和dotnet-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配置为指定(您可以do this too,但您已经写好了,码头工人将为您处理)
我想您是在主服务器外部而不是在两个容器中运行迁移。
如果不需要其他服务直接访问MySQL,则无需在9999
的{{1}}上公开MySQL(这是供外部计算机连接到docker-server并访问该服务)
注释docker-compose
(实际上是127.0.0.1
空间中的任何地址)都是127.0.0.0/8
的同义词。另外localhost
(如果启用了IPv6,则为IPv6)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。