SQL Server 解决不能连接到SQL Server服务器的问题

时间:2024-03-08 08:19:53

    有时会遇到以下错误提示

ERROR: (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error:) An error has occurred while establishing a connection to the server. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 5)


An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326)


A network-related error 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.) (Microsoft SQL Server, Error: 11001)

     这些错误可能是Named Pipes 连接导致的,也可能是TCP/IP 连接导致的。有很多原因会导致这样的错误的提示,循序渐进来解决这个问题。

1)在客户端ping安装了SQL Server的物理服务器。如果可以ping通,你可以尝试通过IP Address(对于默认实例的)或IP Address\Instance Name 来连接SQL Server。如果可以通过IP Address来访问,你可以把SQL Server 机器名称加进/etc/host文件中。在运行窗口输入%SystemRoot%\system32\drivers\etc\,然后用文本格式打开hosts文件。将IP Address和对应的服务器名称加入到文件中,这样就可以用机器名称代替IP Address来连接SQL Server。

2)远程到服务器,打开SQL Server Configuration Manager 检查SQL Server的服务都是运行的。

或利用SC Command

3)检查SQL Server Browser服务是否运行。如果你安装了一个SQL Server实例,但是没有配置一个指定TCP/IP端口,传入请求将会在动态的端口上监听。为了解决这个问题,你需要启动SQL Server Browser服务并且让其运行。

4)确保使用正确的实例名。当你连接默认的实例时,机器名称是实例名最好的代表。而当连接一个如sqlexpress的实例名时,你最好指定机器名称\实例名称。

5)检查SQL Server服务器网络正常。可以利用 sqlcmd -L命令来获取在安装在网络上SQL Server清单。注意只有在SQL Server Browser 服务运行时才会返回SQL Server清单。

6)检查TCP/IP协议和Named Pipes协议和端口。打开SQL Server Configuration Manager,检查SQL Server Network Configuration protocols。启动Named Pipes和TCP/IP协议。对于TCP/IP协议,右击并选择属性检查TCP/IP 通信端口。默认端口是1433,如果需要的话,这个可以更改为更安全的端口。

7)检查SQL Server的远程连接是否启用。在SSMS的服务器名称右击并选择属性。选择连接项,确保Allow remote connection to this server 被勾选。如果有做改变,你需要重启SQL Server实例来应用该变化。

也可以用SQL命令来配置远程服务器连接。

exec sp_configure "remote access", 1          -- 0 on, 1 off
exec sp_configure "remote query timeout", 600 -- seconds
exec sp_configure "remote proc trans", 0      -- 0 on, 1 off

8)检查错误日志来找问题。本地连接SQL Server和检查错误日志。你可以执行XP_READERRORLOG 存储过程或利用SSMS来读取错误提示。

9)为SQL Server端口和SQL Browser服务配置Windows防火墙。进入控制面板,点击防火墙,选中Exception项,设置允许远程连接。点击新增端口,输入端口号和名称。

10)如果你可以登录到服务器来连接SQL Server,但是不可以通过客户端电脑连接SQL Server,执行下面的命令。

-- run this command to see if SPN is not found
EXEC xp_readerrorlog 0,1,"could not register the Service Principal Name",Null