我正在从事一个大学小组项目,该项目之前使用 MySQL,但该团队决定迁移到 MS SQL Server Express。我是唯一使用 Mac 的团队成员,因此我试图让项目连接到 Docker 容器。我在 Visual Studio for Mac 中工作。
我使用以下命令创建并启动了 SQL Express 2017 容器:
docker run -d --name sqlexpress -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=MSSQLContainer1' -e 'MSSQL_PID=Express' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest-ubuntu
运行时看起来像这样:
CONTAINER ID: e448f6e4df4
IMAGE: mcr.microsoft.com/mssql/server:2017-latest-ubuntu
COMMAND: "/opt/mssql/bin/sqls…"
CREATED: 21 hours ago
STATUS: Up 21 hours
PORTS: 0.0.0.0:1433->1433/tcp
NAMES: sqlexpress
当我在 Docker 中运行 SQL Server 并输入 SELECT @@VERSION 时:
Microsoft SQL Server 2017 (RTM-CU16) (KB4508218) - 14.0.3223.3 (X64) Jul 12 2019 17:43:08 Copyright (C) 2017 Microsoft Corporation Express Edition (64-bit) on Linux (Ubuntu 16.04.6 LTS)
我能够从 Azure Data Studio 或 DBeaver 连接到容器,没有任何问题,并创建一个名为 HelpdeskSystem 的数据库.
但是我一生都无法让我们的 .NET Core 应用程序连接到数据库。
这是我当前的连接字符串;不过,我尝试了不同的变体:
"Server=localhost\\sqlexpress,1433;Database=HelpdeskSystem;User=sa;Password=MSSQLContainer1;"
此外,这就是我的队友在他们的 Windows 计算机上使用的连接字符串的样子(我也尝试在我的字符串中包含这些额外的属性):
"server=localhost\\sqlexpress;Database=HelpdeskSystem;Trusted_Connection=True;Integrated Security=True;MultipleActiveResultSets=true"
当我尝试运行需要数据库连接的单元测试时,出现此异常:
InnerException: {System.Net.Sockets.SocketException (0x80004005): Undefined error: 0 at System.Data.SqlClient.SNI.SSRP.GetPortByInstanceName(String browserHostName, String instanceName) at System.Data.SqlClient.SNI.SNIProxy.CreateTcpHandle(DataSource details, Int64 timerExpire, Object callbackObject, Boolean parallel)}
Message: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 25 - Connection string is not valid)"
我不确定我的字符串是否错误,或者是否存在其他一些阻止 Web 应用程序连接的潜在问题,但我绝对能够使用 Azure Data Studio 连接到容器,所以我不明白为什么网络应用程序将无法这样做。
最佳答案
使用此连接字符串。您可以在环回IP前面提及tcp。
"Server=tcp:127.0.0.1,1433;Database=HelpdeskSystem;User=sa;Password=MSSQLContainer1;"
关于sql-server - 为什么 macOS 上的 .NET Core 项目无法连接到 SQL Server Express Docker 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58183107/