跳转到主要内容
本页不适用于 ClickHouse Cloud。本文档介绍的功能不适用于 ClickHouse Cloud 服务。 更多信息,请参阅 ClickHouse 的 Cloud Compatibility 指南。
ClickHouse 可以配置为使用 LDAP 对 ClickHouse 数据库用户进行身份验证。本指南提供了一个简单示例,说明如何将 ClickHouse 与使用公开可访问目录服务进行身份验证的 LDAP 系统集成。
1

在 ClickHouse 中配置 LDAP 连接设置

  1. 测试与此公共 LDAP 服务器的连接:
    $ ldapsearch -x -b dc=example,dc=com -H ldap://ldap.forumsys.com
    
    返回结果大致如下:
    # 扩展 LDIF
    #
    # LDAPv3
    # 基础 <dc=example,dc=com>,范围为 subtree
    # 过滤器: (objectclass=*)
    # 请求: ALL
    #
    
    # example.com
    dn: dc=example,dc=com
    objectClass: top
    objectClass: dcObject
    objectClass: organization
    o: example.com
    dc: example
    ...
    
  2. 编辑 config.xml 文件,并添加以下内容来配置 LDAP:
    <ldap_servers>
        <test_ldap_server>
        <host>ldap.forumsys.com</host>
        <port>389</port>
        <bind_dn>uid={user_name},dc=example,dc=com</bind_dn>
        <enable_tls>no</enable_tls>
        <tls_require_cert>never</tls_require_cert>
        </test_ldap_server>
    </ldap_servers>
    
<test_ldap_server> 标签是一个任意标识,用于标识特定的 LDAP 服务器。
上面使用的是以下基础设置:
ParameterDescriptionExample
hostLDAP 服务器的主机名或 IPldap.forumsys.com
portLDAP 服务器的目录服务端口389
bind_dn用户 DN 模板路径uid={user_name},dc=example,dc=com
enable_tls是否使用安全 LDAPno
tls_require_cert连接时是否要求证书never
在此示例中,由于该公共服务器使用 389 端口且不使用安全端口,我们出于演示目的禁用了 TLS。
有关 LDAP 设置的更多详细信息,请参阅 LDAP 文档页面
  1. <user_directories> 部分中添加 <ldap> 部分,以配置用户角色映射。此部分定义了用户通过身份验证后将被授予的角色。在这个基础示例中,任何通过 LDAP 进行身份验证的用户都将获得 scientists_role,该角色会在 ClickHouse 的后续步骤中定义。该部分应类似如下:
    <user_directories>
        <users_xml>
            <path>users.xml</path>
        </users_xml>
        <local_directory>
            <path>/var/lib/clickhouse/access/</path>
        </local_directory>
        <ldap>
              <server>test_ldap_server</server>
              <roles>
                 <scientists_role />
              </roles>
              <role_mapping>
                 <base_dn>dc=example,dc=com</base_dn>
                 <search_filter>(&amp;(objectClass=groupOfUniqueNames)(uniqueMember={bind_dn}))</search_filter>
                 <attribute>cn</attribute>
              </role_mapping>
        </ldap>
    </user_directories>
    
    上面使用的是以下基础设置:
    ParameterDescriptionExample
    server前面 ldap_servers 部分中定义的标签test_ldap_server
    roles用户将映射到的、在 ClickHouse 中定义的角色名称scientists_role
    base_dn开始搜索包含该用户的组时使用的基础路径dc=example,dc=com
    search_filter用于识别并选择要映射给用户的组的 LDAP 搜索过滤器(&(objectClass=groupOfUniqueNames)(uniqueMember={bind_dn}))
    attribute应返回其值的属性名称cn
  2. 重启 ClickHouse server 以应用这些设置。
2

配置 ClickHouse 数据库角色和权限

本节中的操作步骤假定 ClickHouse 中已启用 SQL 访问控制与账户管理。要启用该功能,请参阅 SQL Users and Roles 指南
  1. 在 ClickHouse 中创建一个角色,其名称与 config.xml 文件中角色映射部分使用的名称相同
    CREATE ROLE scientists_role;
    
  2. 为该角色授予所需权限。以下语句会向任何能够通过 LDAP 进行身份验证的用户授予管理员权限:
    GRANT ALL ON *.* TO scientists_role;
    
3

测试 LDAP 配置

  1. 使用 ClickHouse 客户端登录
    $ clickhouse-client --user einstein --password password
    ClickHouse client version 22.2.2.1.
    Connecting to localhost:9000 as user einstein.
    Connected to ClickHouse server version 22.2.2 revision 54455.
    
    chnode1 :)
    
在步骤 1 中使用 ldapsearch 命令可查看目录中所有可用用户,并且所有用户的密码均为 password
  1. 验证该用户是否已正确映射到 scientists_role 角色,并具有管理员权限
    SHOW DATABASES
    
    Query id: 93b785ff-1482-4eda-95b0-b2d68b2c5e0f
    
    ┌─name───────────────┐
    │ INFORMATION_SCHEMA │
    │ db1_mysql          │
    │ db2                │
    │ db3                │
    │ db4_mysql          │
    │ db5_merge          │
    │ default            │
    │ information_schema │
    │ system             │
    └────────────────────┘
    
    9 rows in set. Elapsed: 0.004 sec.
    

摘要

本文介绍了将 ClickHouse 配置为通过 LDAP 服务器进行身份验证并映射到角色的基本方法。你也可以选择在 ClickHouse 中单独配置用户,同时让这些用户通过 LDAP 进行身份验证,而无需配置自动角色映射。LDAP 模块也可用于连接到 Active Directory。
最后修改于 2026年6月10日