简体   繁体   English

如何使MS SQL Server可用于连接?

[英]How can I make MS SQL Server available for connections?

I'm trying to connect to MS SQL Server (running on my machine) from a Java program. 我正在尝试从Java程序连接到MS SQL Server(在我的机器上运行)。 I'm getting the following long winded exception: 我得到了以下冗长的例外情况:

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

When I check "Properties" and click "View Connection Properties" in the Object Explorer of MS SQL, I find that the "Server is Unavailable." 当我检查“属性”并在MS SQL的对象资源管理器中单击“查看连接属性”时,我发现“服务器不可用”。 This seems possibly related to the exception message. 这似乎与异常消息有关。

How can I make the server available? 如何使服务器可用?

Edit: 编辑:

I am using SQL Server 2008, and I have now enabled TCP/IP, and restarted my instance. 我正在使用SQL Server 2008,现在我已经启用了TCP / IP,并重新启动了我的实例。 I am still told that "Server is unavailable." 我仍然被告知“服务器不可用”。

Any other ideas? 还有其他想法吗?

I ran into this problem as well. 我也遇到了这个问题。 The MSKB article applies to SQL server 2005. MSKB文章适用于SQL Server 2005。

As the "SQL Server Surface Area Configuration" tool has been dropped in lieu of "Facets" - this wasn't obvious to me. 由于“SQL Server表面区域配置”工具已被取代而不是“Facets” - 这对我来说并不明显。

I resolved this by setting the TCPAll port and enabling the relevant IP. 我通过设置TCPAll端口并启用相关IP解决了这个问题。


Steps 脚步

  1. Open the Sql Server Configuration Manager (Start -> Programs -> Microsoft SQL Server 2008 -> Configuration Tools) 打开Sql Server配置管理器(开始 - >程序 - > Microsoft SQL Server 2008 - >配置工具)

  2. Expand SQL Server Network Configuration -> [Your Server Instance] 展开SQL Server网络配置 - > [您的服务器实例]

  3. Double click TCP/IP 双击TCP / IP

    • Under Protocol : 根据议定书

      Ensure Enabled is Yes 确保已启用

    • Under IP Addresses : IP地址下

      Scroll to the bottom and set the TCP Port under IPAll, (1433 by default) 滚动到底部并在IPAll下设置TCP端口(默认为1433)

      Find the IP address you want to connect to and set Enabled and Active to Yes 找到要连接的IP地址,并将“已启用”和“ 活动 设置为“ 是”

Before messing about with connections, first check that the SQL Server Service is actually running. 在搞乱连接之前,首先检查SQL Server服务是否实际运行。

You can do this by either using the SQL Server Configuration Manager (located in the configuration tools folder) or in the standard services console in the Windows control panel. 您可以使用SQL Server配置管理器(位于配置工具文件夹中)或Windows控制面板中的标准服务控制台执行此操作。

Once you have checked the service is up and running, you need to ensure that SQL Server has been configured to allow remote connections. 检查服务是否已启动并运行后,您需要确保已将SQL Server配置为允许远程连接。

See below for an explanation on how to do this: 有关如何执行此操作的说明,请参阅下文:

http://support.microsoft.com/kb/914277 http://support.microsoft.com/kb/914277

I am using SQL Server 2012 and have had same issues. 我使用SQL Server 2012并遇到了同样的问题。

Please note when you activate TCP/IP there are 2 options: 请注意,当您激活TCP / IP时,有2个选项:

  • Active - YES - If you just click yes on this it wont work Active - YES - 如果您只是单击是,它将无法正常工作
  • Enabled - click yes on this too. Enabled - 也请单击“是”。 I did not pay attention to this and was wondering why its was not working, wasting a lot of time in the process 我没有注意到这一点,并且想知道为什么它不起作用,在这个过程中浪费了很多时间

You can test listening ports with this: 您可以使用以下方法测试侦听端口:

netstat -an

Depending on the version of SQL Server you're using, it may well be set up only to use named pipes by default. 根据您使用的SQL Server版本,默认情况下可能只设置为使用命名管道。

You need to enable TCP/IP connections to the server. 您需要启用到服务器的TCP / IP连接。 If you can tell us which version of SQL server you're using, we can give more precise instructions for how to enable TCP/IP. 如果您可以告诉我们您正在使用哪个版本的SQL服务器,我们可以提供有关如何启用TCP / IP的更精确说明。

As usual, check the obvious: 1. See if your router has ports 1433 and 1434 enabled (these are the defaults for IP/UDP communication) 2. Ensure that if you have a firewall in place on either the client or the server, that said ports are not blocked (this is the one that was my "gotcha"). 像往常一样,检查一下:1。查看您的路由器是否启用了端口1433和1434(这些是IP / UDP通信的默认设置)2。确保如果客户端或服务器上有防火墙,说端口没有阻塞(这是我的“陷阱”)。 3. Go into Sql Configuration Manager and under Sql Server Network Configuration, ensure that TCP/IP is enabled. 3.进入Sql Configuration Manager并在Sql Server网络配置下,确保已启用TCP / IP。 Also, in the same program under Sql Native Client 1.0 (if installed) that it, too has TCP/IP enabled. 此外,在Sql Native Client 1.0(如果已安装)下的同一程序中,它也启用了TCP / IP。

A cheap and sleazy way to ensure that all of your connection information is working is to create a .udl file (anywhere on your system), fill in the appropriate information and hit "Test Connection". 确保所有连接信息正常工作的廉价而简单的方法是创建.udl文件(系统中的任何位置),填写相应的信息并点击“测试连接”。 If there is something wrong there, you'll get a (fairly) informative message as to the problem. 如果那里有什么问题,你会得到关于问题的(公平)信息。 If you don't know about .udl files, right click in Windows explorer (right side pane) to create a "New Text File". 如果您不了解.udl文件,请右键单击Windows资源管理器(右侧窗格)以创建“新文本文件”。 Type any acceptable name but change the extension from .txt to .udl. 键入任何可接受的名称,但将扩展名从.txt更改为.udl。 You will be asked to confirm the extension change, answer yes. 系统会要求您确认扩展更改,回答是。 Once created, click the file (or double click if you don't use single click) to open it. 创建后,单击该文件(如果不使用单击,则单击双击)以打开它。 There are 4 tabs. 有4个标签。 The first allows you to select the provider. 第一个允许您选择提供者。 For Visual studio use the Sql Native client, for any other program try the standard Sql Server Provider. 对于Visual Studio使用Sql Native客户端,对于任何其他程序,请尝试使用标准的Sql Server Provider。 On the second tab you can fill in the server name, sql server user name and password (if used), etc. Then test the connection. 在第二个选项卡上,您可以填写服务器名称,sql server用户名和密码(如果使用)等。然后测试连接。 One other item to note - if you are using Windows Authentication, ensure that those users have been added to the Server Logins. 另外一项需要注意的事项 - 如果您使用的是Windows身份验证,请确保已将这些用户添加到服务器登录中。

Open up Sql Server Configuration Manager. 打开Sql Server配置管理器。 ( Start | Programs | Whatever version of sql server | Configuration Tools) (开始|程序|无论什么版本的sql server |配置工具)

Browse down to 'SQL Server Services' and restart your instance. 浏览到“SQL Server服务”并重新启动您的实例。 However you could do this in Management Studio by right clicking on the instance and selecting restart. 但是,您可以通过右键单击实例并选择重新启动来在Management Studio中执行此操作。

If your restart fails then, check out Computer Management | 如果重新启动失败,请查看计算机管理| Event Viewer | 事件查看器| Application and look for sql server events. 应用程序并查找sql​​ server事件。 it will record successful and error messages here. 它会在这里记录成功和错误消息。

Here are my screenshots. 这是我的截图。 If you can't read the words, then download the image or copy and paste the image into Paint. 如果您无法阅读单词,请下载图像或将图像复制并粘贴到Paint中。

Inside Glassfish: 里面的Glassfish:

"General" Tab: “常规选项卡:

在此输入图像描述

"Additional Properties" Tab “其他属性”选项卡

URL= jdbc:sqlserver://localhost;databaseName=NETEAV URL = jdbc:sqlserver:// localhost; databaseName = NETEAV

在此输入图像描述

Inside Microsoft SQL Server Management Studio: 在Microsoft SQL Server Management Studio中:

在此输入图像描述

Check is the connection is blocked by Firewall or not. 检查连接是否被防火墙阻止。 If third-party Firewall is active then it may block the TCP connection. 如果第三方防火墙处于活动状态,则可能会阻止TCP连接。 After this step- 在此步骤之后 -
1. Check TCP enabled on or not 1.检查TCP是否已启用
a. 一个。 Open SQL Server Configuration Manager 打开SQL Server配置管理器
b. SQL Server network configuration SQL Server网络配置
c. C。 Protocols for MSSQLSERVER MSSQLSERVER的协议
d. d。 TCP – Enable it TCP - 启用它
e. Also check listen all – Yes 还要检查全部听 - 是的
f. F。 In IP Addresses tab, IPAll – port number should be 1433 在IP地址选项卡中,IPAll - 端口号应为1433

First off, check that the sql server service is running. 首先,检查sql server服务是否正在运行。 If you're using SQL 2005 or 2008, check Configuration manager (2008) or Surface are configuration tool (2005) to make sure the TCP/IP protocol is enabled and TCP/IP connections are allowed. 如果您使用的是SQL 2005或2008,请检查Configuration Manager(2008)或Surface是配置工具(2005)以确保启用TCP / IP协议并允许TCP / IP连接。 With SSE(express) these are off by default, which would cause your problem. 使用SSE(快速),默认情况下这些都会关闭,这会导致您的问题。 Also just in case you're running multiple instances, you may need SQL browser service running. 此外,如果您正在运行多个实例,则可能需要运行SQL浏览器服务。 If this is the case, you should be able to connect object explorer by using (local) as the server address, since this will use a local/shared memory connection. 如果是这种情况,您应该能够使用(本地)作为服务器地址来连接对象资源管理器,因为这将使用本地/共享内存连接。

From 2005 and up the SQL server browser service has te be running. 从2005年起, SQL服务器浏览器服务已经开始运行。
That one fooled me many times. 那个骗了我很多次。

Same error observed while connecting to SQL Server 2014. 连接到SQL Server 2014时观察到相同的错误。

I logged in the sql server to ensure that the correct port number was set. 我登录了sql server以确保设置了正确的端口号。
For my case the Port number was not set for the sql instance. 对于我的情况,没有为sql实例设置端口号。

Hence I removed the Port number from the connection string url, then I was able to connect . 因此,我从连接字符串url中删除了端口号,然后我就可以连接了。

Anybody facing this issue, can try this way 任何人都面临这个问题,可以尝试这种方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM