我正在尝试从我的 Aspnet Core 应用程序访问 Docker MsSql 实例。
当我尝试从 Management Studio 时,我可以连接到 MsSql 服务器。但是,当我尝试运行该应用程序时,收到以下错误消息。
SqlException: 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: 40 - Could not open a connection to SQL Server)
这是我在 config.json
中使用的连接字符串
{
“连接字符串”:{
"MyConnectionString": "数据源=127.0.0.1;初始目录=测试;集成安全性=False;持久安全信息=True;用户ID=SA;密码=##someStrongPass~~;"
}
}
最佳答案
我找到了解决方案。
如果有人从容器运行其 AspNetCore 应用程序并使用第二个容器作为数据库实例。确保在连接字符串中使用主机 IP 并从数据库容器公开数据库端口。与Linux上的容器不同,Windows上的Docker容器无法直接连接到DB容器的IP地址。希望这个问题可以在未来的版本中得到解决。
所以在这种情况下,连接字符串应该是
{
“连接字符串”:{
"MyConnectionString": "数据源 = YOURMACHINEsIP;初始目录 = 测试;集成安全性 = False;持久安全信息 = True;用户 ID = SA;密码 = SomeStrongPass;"
}
}
关于sql-server - 无法从 AspNetCore 应用程序连接 Docker MsSql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54755400/