我刚刚在我的家庭服务器上安装了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允许远程连接!?


当前回答

我有一个不同的问题,所有的答案到目前为止提到!

我应该首先说,我有它在Visual Studio,而不是SQL Server Express,但解决方案应该完全相同。

天啊,这其实很简单,也许还有点傻。 当我试图创建一个数据库,Visual Studio建议SQL Server的名称,它给了我我的Windows用户名,因为它实际上是服务器的名称,我去了。

实际上是我的Windows用户名+ \SQLEXPRESS。如果你没有改变任何设置,这可能也是你的。如果有效,停止阅读;这就是我的答案。如果不起作用,可能名字不同。

如果像我一样,你在Visual Studio中只遇到了这个问题,请执行以下步骤:

打开SQL Server Management studio。 如果你没有看到你的服务器(默认停靠在左边)按F8或转到视图->对象资源管理器。 右键单击服务器名称并选择Properties(最后一项) 在左下角,你可以看到你的服务器的实际名称在“服务器”下面(不是连接,而是上面)。

这是服务器的名称,这是您应该尝试连接的对象!不是Visual Studio建议的!

其他回答

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

在我的例子中,数据库运行在非标准端口上。检查您正在连接的端口是否与运行数据库的端口相同。如果存在多个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中启用远程连接

SQL Server中的一切都配置好了,我的问题是防火墙阻塞端口1433

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

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

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

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

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