简体   繁体   English

如何通过 inte.net 连接到远程 SQL Server Express?

[英]how to connect to remote SQL Server Express over internet?

I have a Windows 10 PC with SQL Server Express, I have setup remote connections, allocated a fixed port (express, 22172, removed TCP Dynamic Ports under IPALL), set an inbound rule for the port etc.我有一台 Windows 10 PC 和 SQL Server Express,我设置了远程连接,分配了一个固定端口(express,22172,删除了 IPALL 下的 TCP 动态端口),为端口设置入站规则等。

I can connect and manipulate the database from a Linux PC on the same.network (on the same LAN router), using:我可以从同一网络(同一 LAN 路由器)上的 Linux PC 连接和操作数据库,使用:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = @"192.168.1.22\SQLEXPRESS, 22172";
builder.InitialCatalog = "<database>";
builder.ItegratedSecurity=  "sa";
builder.Password = "<password>"

string connectionString = builder.ConnectionString;

So, the normal firewall inbound rules, remote connection settings etc work, correct?那么,正常的防火墙入站规则、远程连接设置等是否有效,对吗? I now want to be able to make that same connection, from a Windows PC that is on a different inte.net (from a different location/town..)我现在希望能够从位于不同 inte.net(来自不同位置/城镇)的 Windows PC 建立相同的连接。

I have:我有:

  • Acquired a fixed IP for the router that the host PC is on为主机 PC 所在的路由器获取固定的 IP
  • Fixed the host PC's local IP - 192.168.1.22 according to this link根据链接修复主机 PC 的本地 IP - 192.168.1.22
  • Set the IP under IP2 for "Protocols for SQLEXPRESS - TCP/IP - IP Addresses" to 192.168.1.22将 IP2 下的“SQLEXPRESS 协议 - TCP/IP - IP 地址”的 IP 设置为 192.168.1.22
  • Added SQL Server Windows NT - 64 Bit as allowed app in firewall在防火墙中添加了 SQL 服务器 Windows NT - 64 位作为允许的应用程序
  • Enabled TCP/IP and Named Pipes in SQL Server Client Network Utility with Start - Run - CLICONFG.exe通过开始 - 运行 - CLICONFG.exe 在 SQL 服务器客户端网络实用程序中启用 TCP/IP 和命名管道
  • Set port forwarding on the router: Service Port 22172, IP Address: 192.168.1.22, Internal Port 22172, Protocol - TCP, Enabled在路由器上设置端口转发:服务端口 22172、IP 地址:192.168.1.22,内部端口 22172,协议 - TCP,已启用

I have switched off the firewall, and still get the error: "(provider: TCP Provider, error 40 - Could not open a connection to SQL Server)我已关闭防火墙,但仍然收到错误消息:“(提供商:TCP 提供商,错误 40 - 无法打开与 SQL 服务器的连接)

I try these connection string options:我尝试这些连接字符串选项:

        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = @"tcp:xxx.xxx.x.xx\SQLEXPRESS, 22172";
        builder.InitialCatalog = "<database>";
        builder.UserID = "sa";
        builder.IntegratedSecurity = false;
        builder.Password = "<password>";

        sqlConnect = builder.ConnectionString;

I have also tried these variations:我也尝试过这些变化:

        builder.DataSource = @"x.xxx.x.xx\SQLEXPRESS, 22172";
        builder.DataSource = @"tcp:xxx.xxx.x.xx, 22172";
        builder.DataSource = @"xxx.xxx.x.xx, 22172";

Also these connection strings:还有这些连接字符串:

        sqlConnect = @"Data Source = tcp:xxx.xxx.x.xx\SQLEXPRESS,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";
        sqlConnect = @"Data Source = xxx.xxx.x.xx\SQLEXPRESS,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";
        sqlConnect = @"Data Source = tcp:xxx.xxx.x.xx,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";
        sqlConnect = @"Data Source = xxx.xxx.x.xx,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";

And for good measure, this one:为了更好的衡量,这个:

sqlConnect = @"Data Source=xxx.xxx.x.xx\SQLEXPRESS,22172;Initial Catalog=<database>;Persist Security Info=True;User ID=sa;Password=<password>;";

I have scanned all relevant questions on SO etc - somewhere I am doing something small wrong?我已经扫描了所有关于 SO 等的相关问题 - 我在某个地方做错了什么? Please help请帮忙

Output when running select * from sys.tcp_endpoints on host PC SQL: Thus, ip_address is NULL for both, port is 0 for both, and is_dynamic_port is 1 for both - is that the relevant information? Output 当select * from sys.tcp_endpoints时:因此,两者的 ip_address 均为 NULL,两者的端口均为 0,两者的 is_dynamic_port 均为 1 - 这是相关信息吗?

Dedicated Admin Connection  1   1   2   TCP 2   TSQL    0   STARTED 1   0   1   NULL
TSQL Default TCP            4   1   2   TCP 2   TSQL    0   STARTED 0   0   1   NULL

I have tried several times to change the dynamic port to static, I have removed all 0 or port numbers in configuration manager, but still get a 1 for _is_dynamic_port.我曾多次尝试将动态端口更改为 static,我已删除配置管理器中的所有 0 或端口号,但 _is_dynamic_port 仍然为 1。 Event Viewer shows:事件查看器显示:

Server is listening on [ 'any' <ipv4> 22172].

Same message if I run the following in SQL:如果我在 SQL 中运行以下命令,则会出现相同的消息:

EXEC xp_ReadErrorLog 0, 1, N'Server is listening on', N'any', NULL, NULL, 'DESC'

GO GO

Why does SQL settings remain on dynamic ports?为什么 SQL 设置保留在动态端口上?

From your work, I suppose you set it all on the Win PC.根据您的工作,我想您将其全部设置在 Win PC 上。 So,...所以,...

  1. What about the Linux machine? Linux机器呢? Does it allow to do outbound connections to 192.168.1.22?是否允许对 192.168.1.22 进行出站连接?
  2. Tak a look at the SqlConnectionStringBuilder.IntegratedSecurity .看看SqlConnectionStringBuilder.IntegratedSecurity It should have a bool value.它应该有一个布尔值。 false if user and password should be used for the authentication ( https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder.integratedsecurity?view=dotnet-plat-ext-6.0 ).如果应使用用户名和密码进行身份验证,则为false ( https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder.integratedsecurity?view=dotnet-plat-ext-6.0 ) .
  3. Tak a look at the SqlConnectionStringBuilder.UserID .看看SqlConnectionStringBuilder.UserID It is missing in both of your code snippets.您的两个代码片段中都缺少它。 But mentioned at created connection strings Persist Security Info=True;User ID=sa;Password=<password>;但在创建的连接字符串中提到Persist Security Info=True;User ID=sa;Password=<password>; as «User ID», «user» и «uid».作为«用户ID»,«用户»和«uid»。 Please, see here ( https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder.userid?view=dotnet-plat-ext-6.0 ).请参阅此处( https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnectionstringbuilder.userid?view=dotnet-plat-ext-6.0 )。

Check that your ISP does not block ports / all ports.检查您的 ISP 是否阻止端口/所有端口。 That is what stopped me.这就是阻止我的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM