我刚刚在我的家庭服务器上安装了SQL Server Express 2012。我试图从桌面PC的Visual Studio 2012连接到它,并反复得到众所周知的错误:

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

为了解决这个问题,我所做的是:

在服务器上运行“SQL Server配置管理器”,启用“SQL Server浏览器” 在服务器上为本地子网的TCP端口1433和1434添加Windows防火墙例外。 验证我在桌面上登录的用户在SQL Server实例上进行了登录。 验证我在SQL Server实例上使用Windows身份验证。 反复重启SQL Server和整个服务器。 把我的头发都拔出来。

如何让SQL Server 2012 Express允许远程连接!?


当前回答

我更喜欢“Rosdi Kasim”的方式,因为它不需要在IP上进行详细配置。

当我再次尝试启动另一个服务器时,我肯定会再次忘记它。

通过简单地启用Sql Server浏览器服务,然后在连接服务器时在IP后面添加\SQLEXPRESS来保持简单(KISS)。

直接使用IP而不使用“\SQLEXPRESS”是我的失败点,因为它不使用默认端口。

谢谢。

其他回答

连接到远程SQL Server的正确方法(不打开UDP端口1434并启用SQL Server浏览器)是使用ip和端口而不是命名实例。

使用ip和port代替命名实例也更安全,因为它减少了攻击表面积。

也许2张图就能表达2000个单词…

该方法使用指定的端口(这是大多数人想要的,我相信)..

此方法需要打开UDP端口1434和SQL Server浏览器运行..

还有一件事…

Kyralessa提供了很好的信息,但我还有一件事要补充,即使在这篇文章之后,我也被难住了。

在“SQL Server Network Configuration > Protocols for Server > TCP/IP Enabled”下。右键单击TCP/IP,选择属性。在IP地址下,您需要为正在使用的每种连接类型将启用设置为Yes。

我必须添加一个防火墙入站端口规则来打开UDP端口1434。这是一个Sql Server浏览器监听。

I had the same issue with SQL Server 2014 locally installed named instance. Connecting using the FQDN\InstanceName would fail, while connecting using only my hostname\InstanceName worked. For example: connecting using mycomputername\sql2014 worked, but using mycomputername.mydomain.org\sql2014 did not. DNS resolved correctly, TCP/IP was enabled within SQL Configuration Manager, Windows Firewall rules added (and then turned the firewall off for testing to ensure it wasn't blocking anything), but none of those fixed the problem.

最后,我不得不在SQL Server上启动“SQL Server浏览器”服务,这解决了连接问题。

我从未意识到SQL Server浏览器服务实际上是在帮助SQL Server建立连接;我的印象是,当你点击“浏览更多”服务器连接时,它只是帮助填充下拉列表,但它实际上有助于将客户端请求与正确的端口#对齐,如果端口#没有显式分配(类似于网站绑定如何帮助缓解托管多个网站的IIS web服务器上的相同问题)。

这个连接项给了我关于SQLServer浏览器服务的线索:https://connect.microsoft.com/SQLServer/feedback/details/589901/unable-to-connect-on-localhost-using-fqdn-machine-name

when you use wstst05\sqlexpress as a server name, the client code separates the machine name from the instance name and the wstst05 is compared against the netbios name. I see no problem for them to match and the connection is considered local. From there, we retrieve the needed information WITHOUT contacting SQL Browser and connect to the SQL instance via Shared Memory without any problem. when you use wstst05.capatest.local\sqlexpress, the client code fails the comparison of the name (wstst05.capatest.local) to the netbios name (wstst05) and considers the connection "remote". This is by design and we will definitely consider improving this in the future. Anyway, due to considering the connection remote and the fact that it is a named instance, client decides that it needs to use SQLBrowser for name resolution. It attempts to contact SQL Browser on wstst05.capatest.local (UDP port 1434) and apparently that part fails. Hence the error you get.

来自TechNet的“SQL Server Browser”服务的原因(重点是我添加的):https://technet.microsoft.com/en-us/library/ms181087(v=sql.120).aspx

在“使用SQL Server浏览器”部分:

If the SQL Server Browser service is not running, you are still able to connect to SQL Server if you provide the correct port number or named pipe. For instance, you can connect to the default instance of SQL Server with TCP/IP if it is running on port 1433. However, if the SQL Server Browser service is not running, the following connections do not work: Any component that tries to connect to a named instance without fully specifying all the parameters (such as the TCP/IP port or named pipe). Any component that generates or passes server\instance information that could later be used by other components to reconnect. Connecting to a named instance without providing the port number or pipe. DAC to a named instance or the default instance if not using TCP/IP port 1433. The OLAP redirector service. Enumerating servers in SQL Server Management Studio, Enterprise Manager, or Query Analyzer. If you are using SQL Server in a client-server scenario (for example, when your application is accessing SQL Server across a network), if you stop or disable the SQL Server Browser service, you must assign a specific port number to each instance and write your client application code to always use that port number. This approach has the following problems: You must update and maintain client application code to ensure it is connecting to the proper port. The port you choose for each instance may be used by another service or application on the server, causing the instance of SQL Server to be unavailable.

更多信息来自同一篇文章的“SQL Server浏览器如何工作”部分:

Because only one instance of SQL Server can use a port or pipe, different port numbers and pipe names are assigned for named instances, including SQL Server Express. By default, when enabled, both named instances and SQL Server Express are configured to use dynamic ports, that is, an available port is assigned when SQL Server starts. If you want, a specific port can be assigned to an instance of SQL Server. When connecting, clients can specify a specific port; but if the port is dynamically assigned, the port number can change anytime SQL Server is restarted, so the correct port number is unknown to the client. ... When SQL Server clients request SQL Server resources, the client network library sends a UDP message to the server using port 1434. SQL Server Browser responds with the TCP/IP port or named pipe of the requested instance. The network library on the client application then completes the connection by sending a request to the server using the port or named pipe of the desired instance

很高兴我问了。我最终找到的解决办法是:

如何配置SQL Server Express以允许端口1433上的远程tcp/ip连接?

运行SQL Server配置管理器。 进入“SQL Server网络配置> SQLEXPRESS协议”。 确保TCP/IP已启用。

到目前为止,一切顺利,完全在意料之中。但之后:

右键单击TCP/IP并选择属性。 验证在IP2下,IP地址被设置为计算机在本地子网上的IP地址。 向下滚动到IPAll。 确保“TCP动态端口”为空。(我的端口设置为5位数。) 确保“TCP端口”设置为“1433”。(我的是空白。)

(此外,如果您遵循这些步骤,就不需要启用SQL Server浏览器,只需要允许端口1433,而不是1434。)

这额外的五个步骤是我在以前版本的SQL Server、Express或其他版本中从未做过的。它们似乎是必要的,因为我在服务器上使用了一个命名实例(myservername\SQLEXPRESS),而不是默认实例。在这里看到的:

配置服务器监听特定的TCP端口(SQL Server配置管理器)