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


当前回答

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

其他回答

您所需要做的就是打开服务器防火墙上的相关端口。

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连接[host]\[db实例名]中添加端口号,1433

注意instancename和port之间的,(逗号)

还要检查的一件事是,已命名实例的拼写是否正确!

本文在排除连接问题时非常有用:如何排除连接到SQL Server数据库引擎的故障

在我的例子中,数据库运行在非标准端口上。检查您正在连接的端口是否与运行数据库的端口相同。如果存在多个SQL server实例,请检查正确的实例。