【问题标题】:.NET Core application in docker cannot connect to mysql serverdocker 中的 .NET Core 应用程序无法连接到 mysql 服务器
【发布时间】:2021-06-14 15:43:52
【问题描述】:

我有一个在 docker 中运行的 .NET Core 应用程序。

对于连接字符串:

 "Server=localhost;port=3306;database=xxxxx;uid=xxxxx;password=xxxxx;"

当它连接到同一个centos服务器上的非docker mysql服务器时, 它显示:

MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. ---> System.AggregateException: One or more errors occurred. (Connection refused 127.0.0.1:3306) ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Connection refused 127.0.0.1:3306
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.Socket.DoMultipleAddressConnectCallback(Object result, MultipleAddressConnectAsyncResult context)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Sockets.Socket.DoMultipleAddressConnectCallback(Object result, MultipleAddressConnectAsyncResult context)
   at System.Net.Sockets.Socket.MultipleAddressConnectCallback(IAsyncResult result)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.TcpClient.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.TcpClient.<>c.<ConnectAsync>b__28_1(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at MySql.Data.Common.StreamCreator.GetTcpStream(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.NativeDriver.Open()
---> (Inner Exception #0) System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (111): Connection refused 127.0.0.1:3306
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.Socket.DoMultipleAddressConnectCallback(Object result, MultipleAddressConnectAsyncResult context)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Sockets.Socket.DoMultipleAddressConnectCallback(Object result, MultipleAddressConnectAsyncResult context)
   at System.Net.Sockets.Socket.MultipleAddressConnectCallback(IAsyncResult result)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.TcpClient.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.TcpClient.<>c.<ConnectAsync>b__28_1(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)<---

但是我检查了端口 3306 是否处于 LISTEN 状态。 我该如何解决这个问题?

【问题讨论】:

  • 你的mysql服务器在哪里?在另一个容器中还是在应用程序容器内或在主机上?
  • @MartinOsusky mysql 服务器在主机上。 docker中的应用可以直接连接宿主应用吗?

标签: mysql docker asp.net-core connection-refused


【解决方案1】:

序幕:

好的,我明白了。你需要从 docker 容器中的应用程序直接连接到主机服务。

我曾经解决过这个问题,但最后我更喜欢切换到解决方案: 应用容器+数据库容器

第二个没有问题的选项是:应用容器 + 专用数据库机器(除 docker 主机外)。我绝对建议更改设置。

解决方案 1:

有一个选项可以从 docker 容器连接到主机。但这不是本地主机。将连接字符串更改为(host.docker.internal):

"Server=host.docker.internal;port=3306;database=xxxxx;uid=xxxxx;password=xxxxx;"

如果您使用的是 linux,请在启动容器时添加 --add-host 选项:

docker run --add-host host.docker.internal:host-gateway <your_image>

解决方案 2(仅限 Linux):

保持连接字符串不变(或将服务器从 localhost 更改为 127.0.0.1)。

将联网方式改为host

docker run --network=host <your_image>

【讨论】:

  • 谢谢!使用解决方案 2 修复。我在 dockerfile 中添加了暴露 8080,然后在运行 docker 时添加了 5001:8080。
猜你喜欢
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-26
  • 1970-01-01
  • 2019-11-24
  • 2020-07-17
  • 2017-03-14
相关资源
最近更新 更多