将ASP.NET核心容器连接到远程SQL Server

发布于 2025-02-03 10:34:28 字数 790 浏览 4 评论 0原文

我正在尝试将我的ASP.NET Core应用程序连接到远程SQL Server。

该应用程序是使用iis Wscore 2016图像部署的。当我在主机上运行应用程序时,它可以正常工作,但是在容器中使用此连接字符串:

Data Source=xx.xxx.xx.xx,1433;Initial Catalog=somedb;User Id=xxxxxx;Password=xxxxx;

或:

Server=xx.xx.xx.xx,PORT_NB;Database=DATABASE;User Id=USER;Password=PASSWORD

但是没有运气 - 我正在使用默认的Docker网络。

错误是这样:

错误:在建立与SQL Server的连接时发生了与网络有关或特定实例的错误。该服务器未找到或无法访问。验证实例名称是正确的,并且配置了SQL Server以允许远程连接。 (提供商:TCP提供商,错误:0-没有这样的主机。)

注意:

  1. 服务器正在允许远程连接
  2. 测试所有连接字符串,并且可以从主机上运行的应用程序连接到远程SQL Server
  3. , 但是示例是在Linux容器上

阅读Docker文档,他们提到了IP

转发, 容器到外界,我可以将容器连接到其他远程服务(例如远程SQL Server)?

我应该使用IP转发使用主机网络或桥梁吗?

有帮助吗?谢谢

I am trying to connect my ASP.NET Core application to a remote SQL Server.

The application is deployed using IIS WScore 2016 image. When I run the application on my host, it's working, but in the container using this connection string :

Data Source=xx.xxx.xx.xx,1433;Initial Catalog=somedb;User Id=xxxxxx;Password=xxxxx;

or:

Server=xx.xx.xx.xx,PORT_NB;Database=DATABASE;User Id=USER;Password=PASSWORD

But no luck - I am using the default Docker network.

The error is like this:

Error: 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: 0 - No such host is known.)

Notes:

  1. The server is allowing the remote connection
  2. All the connection strings are tested and can connect to the remote SQL Server from the application that runs on the host
  3. I have read the Docker documentation and they mentioned the IP forwarding but the example was on linux containers and I did not find any help about connecting the Windows containers to a remote SQL Server

Question

My concrete question is how to expose the container to the outside world and I can connection my container to the other remote services like a remote SQL Server?

Should I use host network or bridge with the IP forwarding?

Any help? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杀お生予夺 2025-02-10 10:34:28

我更改了连接字符串:

 "ConnectionString": "Server=xx.xxx.xx.xx\\MSSQLSERVER,1433;Initial Catalog=Dbname;User Id=username;Password=xxxxxxx;"

然后我重新启动了apppoll,它起作用了

I changed the connection string to this :

 "ConnectionString": "Server=xx.xxx.xx.xx\\MSSQLSERVER,1433;Initial Catalog=Dbname;User Id=username;Password=xxxxxxx;"

then i restarted the AppPoll and it worked

花心好男孩 2025-02-10 10:34:28

您在此容器中使用的当前网络是什么?如果使用默认网络,则使用网络地址转换(NAT)。这意味着您正在使用主机IP地址连接到外部网络。因此,您的问题很可能是SQL Server拒绝了容器主机IP的连接。

Windows上的替代方法是使用其他网络驱动器。有很多选择,我建议您查看更适合您需求的选项: https://cda.ms/ 4NP

What is the current network you're using for this container? If you used the default network, then you're using Network Address Translation (NAT). What that means is that you're using the host IP address to connect to the external network. So, your problem most likely is that the SQL Server is rejecting the connection from the IP of the container host.

The alternative on Windows is to use a different network drive. There are many options and I'd recommend you look at the option that better suits your needs: https://cda.ms/4nP

又爬满兰若 2025-02-10 10:34:28

我遇到了相同的错误,请尝试从连接字符串中删除端口,如下所示:

 "ConnectionString": "Server=ContainerName;Initial Catalog=Dbname;User Id=username;Password=xxxxxxx;"

I had the same error, try removing the port from the connection string as follows:

 "ConnectionString": "Server=ContainerName;Initial Catalog=Dbname;User Id=username;Password=xxxxxxx;"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文