MSSQL异常:JDBC连接SqlServer2000出现"Connection refused: connect"解决方案

For this kind of database connection problem, there are always two distinct issues:

  1. The SQL server itself is not running or TCP/IP is disabled. That can be confirmed by runnetstat

    and see it is listed there. And then run

    telnet localhost 1433

    and see it can connect (or whatever port number).

  2. Even if the server is running, a client program such like jdbc may still fail to connect to it because of afirewall

    For testing, you can turn off the XP firewall. And if you have other firewall running, check the firewall log and see any activities that might be related.

You can also verify whether or not your server is running from the

 

  • SQL Server Network Utilityon the server, or from another client utility such as

     

    SQL Query Analyzer

    or a command line utility such as

    osql

     

To check the enabled protocols for a server, follow these steps:

  1. In SQL Server 2000, start the SQL Server Network Utility (svrnetcn.exe).
  2. On the General tab, select the instance of Microsoft SQL Server on which to load an installed server network library.
  3. Make sure that TCP/IP appears in the Enabled Protocols list.
  4. To find the port number, in the Enabled Protocols list, click TCP/IP, and then click Properties. The Properties dialog box displays the port number.

There is an known bug: SQL Server May Not Listen on TCP/IP Sockets When TCP/IP is the Only Protocol

In a SQL Server 2000 custom installation, if TCP/IP is the only selected protocol and all other protocols are disabled, SQL Server may not initialize and listen on TCP/IP sockets. The Server Network Utility shows that it is listening only on TCP/IP port 1433 even though it is not. To identify if SQL Server is not listening on TCP/IP, check to see if the value for TcpPort in the following registry key is incorrectly set to blank or empty:

HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SQL Server/ [Instance Name]/MSSQLServer/SuperSocketNetLib/Tcp/REG_SZ TcpPort=

To work around this problem, follow these steps:

  1. Start Registry Editor (Regedt32.exe).
  2. Locate the TcpPort value in the following key in the registry:Named instance:
    HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SQL Server/[InstanceName]/MSSQLServer/SuperSocketNetLib/Tcp/TcpPort

     

    Default instance:
    HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/MSSQLServer
    /MSSQLServer/SuperSocketNetLib/TCP/TcpPort

     

  3. . On the Edit menu, click String. Enter either of the following values:
    a. Type the port number you want. -or-
    b. Enter a value of 0 to have SQL Server dynamically detect and assign a port the next time SQL Server starts. 
  4. Click OK.
  5. Quit Registry Editor.

SQL Server 2000 Service Pack 4 (SP4):

You are running a version of Microsoft SQL Server 2000 or Microsoft SQL Server 2000 Desktop Engine (also called MSDE) that has known security vulnerabilities when used in conjunction with the Microsoft Windows Server 2003 family. To reduce your computer’s vulnerability to certain virus attacks, the TCP/IP and UDP network ports of Microsoft SQL Server 2000, MSDE, or both have been disabled. To enable these ports, you must install SQL Server 2000 Service Pack 3a, or the most recent service pack for Microsoft SQL Server 2000 or MSDE from    http://www.microsoft.com/sql/downloads/2000/sp4.mspx.

### 问题分析 在尝试通过JDBC连接HiveServer2时,出现错误信息“Could not open client transport with JDBC Uri: java.net.ConnectException: Connection refused”,表明客户端无法建立与HiveServer2的连接。此类问题通常与网络配置、HiveServer2服务状态或JDBC连接字符串配置有关。 ### 常见原因及解决方法 1. **HiveServer2服务未启动或异常** - 确保HiveServer2服务已经正确启动。可以通过执行以下命令检查服务状态: ```bash hive --service hiveserver2 ``` - 如果服务未启动,尝试启动HiveServer2服务并检查日志文件(通常位于`$HIVE_HOME/logs`目录)以排查启动失败的原因。 2. **网络连接问题** - 检查客户端与HiveServer2所在服务器之间的网络是否连通。可以使用`ping`或`telnet`命令测试: ```bash ping 192.168.1.101 telnet 192.168.1.101 10000 ``` - 如果无法通过telnet连接到10000端口,可能是防火墙阻止了连接。检查服务器防火墙设置并确保端口开放。 3. **JDBC连接字符串配置错误** - 确保JDBC连接字符串格式正确,通常为: ``` jdbc:hive2://<host>:<port>/<database> ``` - 例如: ``` jdbc:hive2://192.168.1.101:10000/default ``` - 若使用Kerberos认证,需在连接字符串中添加相应参数,例如: ``` jdbc:hive2://192.168.1.101:10000/default;principal=hive/_HOST@REALM ``` 4. **JDBC驱动版本不匹配** - 确保使用的JDBC驱动版本与HiveServer2版本兼容。如果驱动版本过旧或不匹配,可能导致连接失败。建议使用Hive发行包中自带的JDBC驱动或从官方下载对应版本的驱动。 5. **HiveServer2配置问题** - 检查HiveServer2的配置文件`hive-site.xml`,确保以下参数配置正确: - `hive.server2.thrift.bind.host`:应设置为HiveServer2监听的IP地址。 - `hive.server2.thrift.port`:默认为10000,确保该端口未被占用且与客户端连接端口一致。 - 示例配置: ```xml <property> <name>hive.server2.thrift.bind.host</name> <value>0.0.0.0</value> </property> <property> <name>hive.server2.thrift.port</name> <value>10000</value> </property> ``` 6. **Thrift服务监听状态** - 使用`netstat`命令检查HiveServer2是否正在监听指定端口: ```bash netstat -tuln | grep 10000 ``` - 如果未看到10000端口处于LISTEN状态,则HiveServer2未正常运行。 ### 示例代码 以下是一个使用JDBC连接HiveServer2的Java代码示例: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class HiveJdbcClient { public static void main(String[] args) { String jdbcUrl = "jdbc:hive2://192.168.1.101:10000/default"; String user = "hive"; String password = ""; try { Connection connection = DriverManager.getConnection(jdbcUrl, user, password); System.out.println("Connected to HiveServer2 successfully."); connection.close(); } catch (SQLException e) { System.err.println("Failed to connect to HiveServer2: " + e.getMessage()); } } } ``` 确保项目中已添加Hive JDBC依赖,例如Maven配置: ```xml <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>3.1.2</version> </dependency> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值