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

Docker部署ClickHouse连接Java时密码验证失败求助

Docker部署ClickHouse后Java连接认证失败问题排查与解决

问题描述

我在Docker中部署了ClickHouse服务器,尝试通过Java语言连接该服务器时遇到问题:明明密码正确,却始终被拒绝。

代码示例

public static void main(String[] args) throws Exception {
    Properties prop = new Properties();
    prop.setProperty("ssl", "false");
    prop.setProperty("sslmode", "NONE"); // NONE to trust all servers; STRICT for trusted only
    prop.setProperty("username", "default");
    prop.setProperty("password", "AufsdfFMdfsdfPffdsdBIgxasdzqqfdoıfksdfz");
    Connection conn = DriverManager.getConnection("jdbc:ch:https://localhost:18123/", prop);

    Statement stmt = conn.createStatement();
    stmt.execute("CREATE TABLE IF NOT EXISTS jdbc_test(idx Int8, str String) ENGINE = MergeTree ORDER BY idx");

    try (PreparedStatement ps = conn.prepareStatement(
            "insert into jdbc_test select col1, col2 from input('col1 Int8, col2 String')")) {
        for (int i = 0; i < 10; i++) {
            ps.setInt(1, i);
            ps.setString(2, "test:" + i); // col1
            // parameters will be write into buffered stream immediately in binary format
            ps.addBatch();
        }
        // stream everything on-hand into ClickHouse
        ps.executeBatch();
    }


    ResultSet rs = stmt.executeQuery("select * from jdbc_test");
    while (rs.next()) {
        System.out.println(String.format("idx: %s str: %s", rs.getString(1), rs.getString(2)));
    }
}

报错信息

Exception in thread "main" java.sql.SQLException: Code: 516. DB::Exception: default: Authentication failed: password is incorrect, or there is no user with such name.

排查与解决方法

  • 修正连接协议与端口:你的JDBC URL使用了https协议,但已设置ssl=false,应改为http;同时ClickHouse默认HTTP端口是8123,不是18123,修正后URL为jdbc:ch:http://localhost:8123/。另外要确认Docker启动命令是否包含端口映射-p 8123:8123,否则本地无法访问容器内的ClickHouse端口。
  • 排查密码字符编码问题:密码中包含特殊字符ı,可能存在编码不一致问题。先替换为纯ASCII简单密码测试连接,若成功再调整字符编码,确保Java代码中的密码与ClickHouse配置的密码编码一致。
  • 检查ClickHouse用户配置:进入Docker容器,查看/etc/clickhouse-server/users.xml中的default用户配置,确认密码是否匹配。注意ClickHouse支持明文或哈希密码,若配置的是哈希值,需确保代码中的明文密码对应的哈希与配置一致。可在容器内用clickhouse-client --user default --password "你的密码"测试本地连接,若容器内也失败,说明用户配置存在问题。
  • 确认JDBC驱动版本兼容性:确保使用的ClickHouse JDBC驱动版本与Docker中ClickHouse服务器版本匹配,版本不兼容也可能引发认证失败,尽量选用与服务器版本一致或相近的驱动版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 07:55:29