You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ClickHouse JDBC驱动连接报错但Python驱动可正常连接问题咨询

问题根因与解决方案

1 首要故障根因:未配置SSL对应参数

当前报错首先和Python代码中开启的secure=true、verify=false直接相关,服务端要求走SSL加密连接,默认JDBC驱动未开启SSL配置,所以无法得到服务端响应。

2 ru.yandex.clickhouse.ClickHouseDriver SSL配置方法

有两种配置方式,二选一即可:

方式1:直接在JDBC URL中追加参数

将连接URL修改为:
jdbc:clickhouse://xxxxxx:9001/db_name?ssl=true&sslCertificateVerification=false
其中:

  • ssl=true 对应Python的secure=true,开启SSL加密连接
  • sslCertificateVerification=false 对应Python的verify=false,跳过服务端证书校验

方式2:通过Properties对象统一配置(更规范)

修改后的代码示例:

import java.util.Properties
import java.sql.Connection
import java.sql.DriverManager

Class.forName("ru.yandex.clickhouse.ClickHouseDriver")
var conn: Connection = null
try {
  val props = new Properties()
  props.setProperty("user", "usrname")
  props.setProperty("password", "pass")
  // SSL相关配置
  props.setProperty("ssl", "true")
  props.setProperty("sslCertificateVerification", "false")
  conn = DriverManager.getConnection("jdbc:clickhouse://xxxxxx:9001/db_name", props)
  val stmt = conn.createStatement()
  val sql = s"""select 1""".stripMargin
  stmt.executeQuery(sql)
} finally {
  if (conn != null)
    conn.close()
}

3 clickhouse-native-jdbc SSL配置方法

如果你使用原生TCP协议的clickhouse-native-jdbc驱动,配置参数略有差异:

  • URL追加参数:jdbc:ch://xxxxxx:9001/db_name?useSSL=true&trustServerCertificate=true
  • 或者Properties中添加对应参数:
    props.setProperty("useSSL", "true")
    props.setProperty("trustServerCertificate", "true")
    

4 其他可能的故障根因

如果配置SSL后仍然报错,排查以下问题:

  • 驱动协议与端口不匹配:ru.yandex.clickhouse驱动默认走HTTP/HTTPS协议,默认端口为8123;而clickhouse-native-jdbc走原生TCP协议,对应你Python使用的9001端口。如果你的9001是原生TCP端口,使用ru.yandex驱动访问会直接报错,需要更换为clickhouse-native-jdbc驱动,或者确认服务端HTTPS端口使用正确。
  • 运行JDBC的机器IP未加入ClickHouse服务端白名单。
  • 服务器防火墙未开放9001端口对JDBC运行机器的访问权限。
  • JDBC驱动版本与ClickHouse服务端版本兼容性问题,可尝试更换与服务端大版本匹配的驱动。

内容的提问来源于stack exchange,提问作者justlikethat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 16:36:06