使用TCP/IP连接到SQL Server 2008

12

当我尝试使用SQL Server Management Studio通过TCP/IP连接时,出现以下错误。我需要一步步的描述来解决我的问题。这里错了什么?

Cannot connect to 
===================================

A network related or instance specific error when a connection to SQL Server...
(provider: Named pipe-provider, error: 40 -  SQL Server)
(.Net SqlClient Data Provider)

------------------------------
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=53&LinkId=20476

------------------------------
Error Number: 53
Severity: 20
State: 0

------------------------------
Program Location:

   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer.ObjectExplorer.ValidateConnection(UIConnectionInfo ci, IServerType server)
   at Microsoft.SqlServer.Management.UI.ConnectionDlg.Connector.ConnectionThreadUser()

9
友情提醒:不要提出要求,比如当你说“我想要一步步的解决方案描述…”时。这会给人留下错误的印象,因为我们在这里都是志愿者。 - Alex Beardsley
好的,抱歉...我不是有意这么说的... - MCOL
很可能,你的客户端或服务器(或两者)都没有设置允许使用tcp/ip作为Sql Server传输的选项。 - RBarryYoung
1
遇到了相同的问题.. 尝试了这里所有的答案。防火墙没有阻止它,并在配置管理器中启用了TCP。 - The Muffin Man
9个回答

18
我通常会忘记的是,即使在SQLServerNetwork-> MSSQLServer的协议中启用了tcp,您仍然需要去启用服务器的潜在不同IP地址。即使是只有一个 NIC 的服务器也有多个 IP 地址。

  1. 点击如上所示的MSSQLServer的协议。
  2. 然后在右侧窗格中启用TCP/IP。
  3. 现在双击TCP/IP以获取对话框。
  4. 如果要在服务器上监听所有IP地址,请在第一个协议选项卡上的“Listen All”框中选择“Yes”;
  5. 否则,选择“IP地址”选项卡并将所需的IP地址启用,方法是将“Enabled”设置为“Yes”。

enter image description here


1
如果有人对如何从脚本中执行此操作感兴趣,请访问:http://promikro.com.pl/2013/09/18/mssql2012expr-en-ip-addrs-problem/。 - SOReader

13

当您检查SQL Server Network Configuration (开始菜单 > Microsoft SQL Server > 配置工具 > SQL Server Configuration Manager > SQL Server Network Configuration)时,您会看到什么?

alt text

您的服务器真的启用了TCP/IP协议吗?它默认是关闭的,在大多数情况下也保持关闭状态(除非您专门将其打开)。

仅使用基于IP的服务器地址并不意味着您正在使用TCP/IP协议进行连接...请查看http://www.connectionstrings.com/sql-server-2008以获取一个将使用TCP/IP的示例连接字符串:

Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

关键是 Network Library=DBMSSOCN;,这告诉你的代码使用 TCP/IP 进行连接。


在服务器端和客户端方面都有什么协议序列?除非您明确指定要使用哪一个,否则SQL Server客户端将选择列表中启用的第一个。这是在“SQL Native Client Configuration”下的内容。 - marc_s

8

值得注意的是,确保 SQL Server Browser 服务正在运行,因为 TCP/IP 连接将使用此服务。


1
这就是解决我的问题的方法。我同时也打开了“SQL Server代理”。非常感谢这个建议。 - DustinA

4

请查看www.connectionstrings.com。错误信息似乎是您正在使用命名管道而不是TCP/IP。不要忘记必须在服务器上明确启用SQL Server的TCP/IP。


谢谢,但我正在使用TCP/IP编号。 - MCOL
如果您设置了服务器不使用默认端口,您可能需要添加别名或明确说明端口。这应该作为示例放在发布的URL上。除此之外,防火墙和连接字符串中给出的注释应该能够解决问题。 - Sascha

3

0

要使用"TCP/IP"连接到MSSQL(SQL Server),您需要使用SQL Server Configuration Manager(SSCM)启用"TCP/IP"。这是如何在Windows机器中找到SQL Server Configuration Manager(SSCM)

因此,在SSCM上,从下面所示的"SQL Server Network Configuration/Protocols for SQLEXPRESS"中启用"TCP/IP"。*MSSQL"TCP/IP"默认情况下处于禁用状态:

enter image description here

在上面所示的“启用”后,您将收到以下消息:

enter image description here

因此,如上所述的消息,从下面显示的"SQL Server Services"重新启动SQL Server(SQLEXPRESS)

enter image description here

最后,您可以使用"TCP/IP"连接到MSSQL


0

请按照以下步骤操作:

开始 -> 程序 -> Microsoft SQL Server 2008 -> 配置工具 -> SQL Server 配置管理器

SQL Server 网络配置 -> [SQLInstanceName] 的协议

右键单击 TCP/IP -> 启用


0
如果TCP/IP已经启用,则可能是防火墙阻止了连接。打开相关端口,它应该可以工作。
您应该检查连接两端的防火墙。

-1

而且 - 你可以在 SQL Server 配置管理器的协议屏幕中更改它,但它不会生效,直到 SQL Server 服务重新启动。我只是重启了 - 这是唯一确定的方法(这里有个《异形》的参考)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接