【问题标题】:Cannot connect Docker MsSql from AspNetCore app无法从 AspNetCore 应用程序连接 Docker MsSql
【发布时间】:2019-08-22 23:02:46
【问题描述】:

我正在尝试从我的 Aspnet Core 应用程序访问 Docker MsSql 实例。

当我从 Management Studio 尝试时,我可以连接到 MsSql 服务器。但是,当我尝试运行该应用程序时,我收到以下错误消息。

SqlException:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:TCP 提供者,错误:40 - 无法打开与 SQL Server 的连接)

这是我使用的来自config.json的连接字符串

{ "ConnectionStrings": { "MyConnectionString": "Data Source=127.0.0.1;Initial Catalog=Test;Integrated Security=False;Persist Security Info=True;User ID=SA;Password=##someStrongPass~~;" } }

【问题讨论】:

  • 您是否缺少端口?还请提供您的 docker 文件/撰写文件。
  • 我也尝试了端口 1433,结果相同。
  • 您是否在 docker 容器中托管 .net 核心?如果你这样做,请检查github.com/docker-library/mysql/issues/95

标签: sql-server docker asp.net-core


【解决方案1】:

我找到了解决办法。

如果有人从容器运行他们的 AspNetCore 应用程序并为数据库实例使用第二个容器。确保在连接字符串中使用主机 IP 并从数据库容器公开数据库端口。与 Linux 上的容器不同,Windows 上的 Docker 容器无法直接连接到 DB 容器的 IP 地址。希望这可以在未来的版本中得到解决。

所以在这种情况下,连接字符串应该是

{ "ConnectionStrings": { "MyConnectionString": "Data Source=YOURMACHINEsIP;Initial Catalog=Test;Integrated Security=False;Persist Security Info=True;User ID=SA;Password=SomeStrongPass;" } }

【讨论】:

    【解决方案2】:

    您也可以使用容器名称代替 127.0.0.1,port 或 localhost,port

    {
      "ConnectionStrings": {
        "MyConnectionString": "Data Source=SQL_CONTAINER_NAME;Initial Catalog=Test;Integrated Security=False;Persist Security Info=True;User ID=SA;Password=SomeStrongPass;"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 2021-04-22
      • 2021-05-03
      • 1970-01-01
      相关资源
      最近更新 更多