docker中的.NET核心应用程序无法连接到mysql服务器



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

对于连接字符串:

"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状态。如何解决此问题?

序言:

好的,我明白了。。您需要从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>

最新更新