ClickHouse學習系列之二【用戶權限管理】

背景

      最近花了些時間看了下ClickHouse文檔,發現它在OLAP方面表現很優異,並且相對也比較輕量和簡單,因此準備入門瞭解下該數據庫系統。在安裝完以後首先作的應該如何設置用戶密碼以及權限控制。由於和MySQL、MongoDB等數據庫的用戶管理徹底不同,爲方便本身之後直接查閱,本文對其用戶權限管理方面進行梳理說明。html

說明

      ClickHouse做爲一個分析類型(OLAP)的數據庫系統,相對於MySQL數據庫在用戶管理方面有很大不一樣,它是經過修改配置文件來實現用戶權限管理的。在安裝好ClickHouse以後,其默認的配置文件在/etc/clickhouse-server目錄下,對應的配置文件爲users.xml,ClickHouse使用它來定義用戶相關的配置項。如今開始對其進行說明,對應手冊裏的說明包含如下幾個方面:web

注意一點,修改了user.xml的參數以後是即時生效的,若有問題能夠查看其錯誤日誌。好了,如今開始對這些進行說明,先熟悉用戶權限管理這一方面的相關操做。shell

※ Settings profiles :設置用戶配置文件

profile的做用相似於用戶角色,能夠在user.xml中定義多組profile,並能夠爲每組profile定義不一樣的配置項,類限制資源的使用。多個profile的配置能夠複用。咋眼一看有點和MySQL的Proxy權限相似。數據庫

模板:express

 <profiles> --配置profile <default>  -- 自定義profile
            <max_memory_usage>10000000000</max_memory_usage>
            <use_uncompressed_cache>0</use_uncompressed_cache>
            <load_balancing>random</load_balancing>
        </default>
        <readonly>  -- 自定義profile
            <readonly>1</readonly>
       <max_memory_usage>100000000</max_memory_usage>
        </readonly>
    </profiles>

說明:服務器

  • <default>:自定義profile,能夠在它下面設置相關參數,如:最大內存使用、只讀等等。更多的配置參數後續會介紹,也而已看官網文檔,能夠設置多個profile。

該示例指定了兩個profile:default和readonly。 默認<default>有一個特殊用途:必須始終存在而且在啓動服務器時應用。profile文件能夠相互繼承,只須要在配置文件中列出便可,如定義一個test的profile:網絡

        <test>
            <profile>readonly</profile>
            <max_memory_usage>10000</max_memory_usage>
        </test> 

test的profile繼承了readonly的profile,包含了其全部的配置,而且使用新參數來覆蓋其原有的配置。設置了以後如何使用呢?有二種方法,第1是直接在終端命令行裏進行設置,第2個是在users.xml中的users選項組裏進行指定(後面會說明)。less

[root@dba clickhouse-server]# clickhouse-client
ClickHouse client version 20.3.5.21 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 20.3.5 revision 54433.

dba :) set profile = 'test'

SET profile = 'test'

Ok.
rows in set. Elapsed: 0.002 sec. 

dba :) set max_memory_usage = 123123

SET max_memory_usage = 123123

Received exception from server (version 20.3.5):
Code: 164. DB::Exception: Received from localhost:9000. DB::Exception: Cannot modify 'max_memory_usage' setting in readonly mode. 
rows in set. Elapsed: 0.005 sec. 

dba :) Bye.
View Code

測試說明已經把readonly的profile的參數(readonly)繼承過來了。 dom

※ Constraints on Settings:約束

user.xml配置文件的profile選項組下constraints選項組裏定義對設置的約束,並禁止用戶使用SET查詢更改某些設置。constraints標籤能夠設置一組約束條件,以限制profile內的參數值被隨意修改,約束條件有以下三種規則:分佈式

  • min:最小值約束,在設置相應參數的時候,取值不能小於該閾值;

  • max:最大值約束,在設置相應參數的時候,取值不能大於該閾值;

  • readonly:只讀約束,該參數值不容許被修改。

須要在profile選項組裏設置constraints,模板:

<profiles>
  <user_name>
    <constraints>
      <setting_name_1>
        <min>lower_boundary</min>
      </setting_name_1>
      <setting_name_2>
        <max>upper_boundary</max>
      </setting_name_2>
      <setting_name_3>
        <min>lower_boundary</min>
        <max>upper_boundary</max>
      </setting_name_3>
      <setting_name_4>
        <readonly/>
      </setting_name_4>
    </constraints>
  </user_name>
</profiles>

說明:若是違反約束,則會引起異常,而且設置實際上不會更改。支持三種約束類型:最小,最大,只讀。 最小和最大約束爲數字設置指定上限和下限,而且能夠組合使用。 只讀約束指定用戶徹底不能更改相應的設置。如: 

    <profiles>
        <default>
            <max_memory_usage>10000000000</max_memory_usage>
            <use_uncompressed_cache>0</use_uncompressed_cache>
            <force_index_by_date>0</force_index_by_date>
            <load_balancing>random</load_balancing>
            <constraints>
                <max_memory_usage>
                    <min>100000</min>
                    <max>20000</max>
                </max_memory_usage>
                <force_index_by_date>
                    <readonly/>
                </force_index_by_date>
            </constraints>
        </default>
    </profiles>

說明:在default默認profile中定義的constraints約束,將做爲默認的全局約束,自動被其餘profile繼承。例子中約束了參數max_memory_usage的最大最小值和參數force_index_by_date的只讀屬性,不能修改。關於更多的參數後續會再進行說明,也能夠看官方文檔。若是違反約束則會報錯:

Code: 452. DB::Exception: Received from localhost:9000. DB::Exception: Setting max_memory_usage shouldn't be less than 100000.

Code: 452. DB::Exception: Received from localhost:9000. DB::Exception: Setting force_index_by_date should not be changed. 
View Code

※ Quotas:配額,限制使用資源,限制有二種類型:一是在固定週期裏的執行次數(quotas),二是限制用戶或則查詢的使用資源(profiles)

user.xml配置文件的選項組quotas裏設置,限制該用戶一段時間內的資源使用,即對一段時間內運行的一組查詢施加限制,而不是限制單個查詢。還具備限制單個查詢的複雜性的功能。模板:

    <!-- Quotas. -->
    <quotas>
        <!-- Name of quota. -->
        <default> --指定quotas名
            <!-- Limits for time interval. You could specify many intervals with different limits. -->
            <interval> --時間間隔
                <!-- Length of interval. -->
                <duration>3600</duration> --週期
                <!-- No limits. Just calculate resource usage for time interval. -->
                <queries>0</queries>
                <errors>0</errors>
                <result_rows>0</result_rows>
                <read_rows>0</read_rows>
                <execution_time>0</execution_time>
            </interval>
        </default>
    </quotas>

默認狀況下,配額僅跟蹤每小時的資源消耗,而沒有限制使用狀況。在每一個請求以後,將爲每一個時間間隔計算的資源消耗輸出到服務器日誌。

說明:

  • <default>:配額規則名。
  • <interval>:配置時間間隔,每一個時間內的資源消耗限制。
  • <duration>:時間週期,單位秒。
  • <queries>:時間週期內容許的請求總數,0表示不限制。
  • <errors>:時間週期內容許的異常總數,0表示不限制。
  • <result_rows>:時間週期內容許返回的行數,0表示不限制。
  • <read_rows>:時間週期內容許在分佈式查詢中,遠端節點讀取的數據行數,0表示不限制。
  • <execution_time>:時間週期內容許執行的查詢時間,單位是秒,0表示不限制。

上面示例中的配置,屬性值均爲0,因此資源配額不作任何限制。如今繼續聲明另一組配額:

<statbox>
    <interval>
        <duration>3600</duration>
        <queries>1000</queries>
        <errors>100</errors>
        <result_rows>1000000000</result_rows>
        <read_rows>100000000000</read_rows>
        <execution_time>900</execution_time>
    </interval>

    <interval>
        <duration>86400</duration>
        <queries>10000</queries>
        <errors>1000</errors>
        <result_rows>5000000000</result_rows>
        <read_rows>500000000000</read_rows>
        <execution_time>7200</execution_time>
    </interval>
</statbox>

說明:對於「 statbox」配額,每小時和每24小時(86,400秒)設置限制, 若是超過限制則會執行失敗,並給出什麼時候才能執行的錯誤:

Code: 201. DB::Exception: Received from localhost:9000. DB::Exception: Quota for user `default` for 10s has been exceeded: queries = 4/3. Interval will end at 2020-04-02 11:29:40. Name of quota template: `default`. 
View Code

從實施定義的固定時刻開始計算時間間隔。間隔結束時,將清除全部收集的值。 接下來的一個小時,配額計算將從新開始。對於分佈式查詢處理,累積量存儲在請求者服務器上。 所以,若是用戶轉到另外一臺服務器,則那裏的配額將從新開始。從新啓動服務器後,配額將重置。

quotas 在配置的「用戶」部分分配給用戶,若是不是根據時間週期而是根據查詢的資源消耗來進行限制,則在user.xml裏的profile裏進行設置,如參數:

1:max_memory_usage:在單個ClickHouse服務進程中,運行一次查詢限制使用的最大內存用量,默認值爲10G;
2:max_memory_usage_for_user:在單個ClickHouse服務進程中,以用戶爲單位進行統計,單個用戶在運行查詢時,限制使用的最大內存用量,默認值爲0,即不作限制;
3:max_memory_usage_for_all_queries:在單個ClickHouse服務進程中,全部運行的查詢累加在一塊兒,限制使用的最大內存用量,默認爲0不作限制;
4:max_partitions_per_insert_block:在單次INSERT寫入的時候,限制建立的最大分區個數,默認值爲100個。若是超出這個閾值數目,將會獲得異常;
5:max_rows_to_group_by:在執行GROUP BY聚合查詢的時候,限制去重後的聚合KEY的最大個數,默認值爲0,即不作限制。當超過閾值數量的時候,其處理方式由group_by_overflow_mode參數決定;
6:group_by_overflow_mode:當max_rows_to_group_by熔斷規則觸發的時候,有三種處理形式: 
throw拋出異常,此乃默認值;
break當即中止查詢,並返回當前部分的數據;
any僅以當前已存在的聚合KEY,繼續完成聚合查詢;
7:max_bytes_before_external_group_by:在執行GROUP BY聚合查詢的時候,限制使用的最大內存用量,默認值爲0,即不作限制。當超過閾值數量的時候,聚合查詢將會進一步借用本地磁盤。

若是超過了限制則報錯:

Code: 241. DB::Exception: Received from localhost:9000. DB::Exception: Memory limit (for user) exceeded: would use 4.81 MiB (attempt to allocate chunk of 5045339 bytes), maximum: 1.00 B: While executing MergeTree
View Code

User settings:用戶配置

user.xml配置文件的users選項組是配置自定義用戶,定義一個新用戶,必須包含如下幾項屬性:用戶名、密碼、訪問ip、數據庫、表等等。它還能夠應用上面的profile、quota。

模板:

<users>
    <!-- If user name was not specified, 'default' user is used. -->
    <user_name> --配置的用戶
        <password></password> --明文密碼
        <!-- Or -->
        <password_sha256_hex></password_sha256_hex> --加密密碼,二選一

        <networks incl="networks" replace="replace"> --容許登陸的地址,用於限制用戶登陸的客戶端地址
        </networks>

        <profile>profile_name</profile>   --指定用戶的profile

        <quota>default</quota>    -- 指定用戶的quota,限制用戶使用資源

        <databases>               --指定數據庫
            <database_name>
                <table_name>      --指定數據表
                    <filter>expression</filter>
                </table_name>
            </database_name>
        </databases>
    </user_name>
    <!-- Other users settings -->
</users>

說明:默認配置了default用戶,在此以前的全部示例中,一直使用的是這個用戶。

  • <user_name>:自定義用戶
  • <password>:用戶密碼
    密碼能夠以純文本、SHA256(十六進制格式)、password_double_sha1_hex(和MySQL兼容)指定,設置方法以下:
    1.純文本:
    <password>password</password>
    2.sha256:
    <password_sha256_hex>password</password_sha256_hex>
    從shell生成密碼的示例:
    PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha256sum | tr -d '-'
    第一行明文,第二行sha256
    3.sha1:
    <password_double_sha1_hex
    >password</password_double_sha1_hex> 從shell生成密碼的示例: PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha1sum | tr -d '-' | xxd -r -p | sha1sum | tr -d '-' 第一行明文,第二行sha1
  • <networks>:限制用戶登陸的客戶端地址
    能夠經過IP,主機等進行限制
    <ip>:IP地址,如10.0.0.1
    <host>:主機名,如example01.host.ru
    <host_regexp>:^example\d\d-\d\d-\d\.host\.ru$
    
    來自任何IP:
    <ip> :: / 0 </ ip>
    來自本機:
    <ip>::1</ip>
    <ip>127.0.0.1</ip>
  • <profile>:指定用戶的profile
  • <quota>:指定用戶的quota,限制用戶使用資源
  • <database_name>:指定用戶訪問的數據庫
  • <table_name>:指定用戶訪問的表
  • <filter>:指定用戶訪問的過濾器,限制返回符合條件的行。如:id = 1 ,即查詢表只返回id=1的行

例子:

    <users>
        <default>
            <password>123456</password>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
        </default>

        <zhoujy>
            <password_double_sha1_hex>6bb4837eb74329105ee4568dda7dc67ed2ca2ad9</password_double_sha1_hex>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
            <allow_databases>
                <database>test</database>
            </allow_databases>
            <databases>
                <test>  
                    <xx>
                        <filter>id >= 500 </filter>  --行級限制
                    </xx>
                </test>
            </databases>
        </zhoujy>

    </users>

該示例指定了兩個用戶:

default:指定了密碼、訪問IP、profile、quota。
zhoujy :指定了密碼、訪問IP、profile、quota,以及它只能使用test庫,而且只能返回test庫xx表id大於等於500的數據。

※ Permissions for queries:查詢權限管理

查詢能夠分爲如下幾種類型:

  • 讀:SELECT,SHOW,DESCRIBE,EXISTS
  • 寫:INSERT,OPTIMIZE。
  • DDL:CREATE,ALTER,RENAME,ATTACH,DETACH,DROP TRUNCATE。
  • 設置:SET,USE。
  • KILL

以上的權限經過配置標籤來控制:

readonly讀權限、寫權限和設置權限,由此標籤控制,它有三種取值:

  • 0,不進行任何限制(默認值);

  • 1,只擁有讀權限(只能執行SELECT、EXISTS、SHOW和DESCRIBE);

  • 2,擁有讀權限和設置權限(在讀權限基礎上,增長了SET查詢)。

當設置readonly=1後,用戶將沒法在當前會話中更改readonly和allow_ddl設置;也能夠經過約束來限制更改權限。

allow_ddlDDL權限由此標籤控制,它有兩種取值:

  • 當取值爲0時,不容許DDL查詢;

  • 當取值爲1時,容許DDL查詢(默認值)

若是當前會話的allow_ddl = 0,則沒法執行SET allow_ddl = 1

注意:KILL QUERY能夠在任何設置上執行,readonlyallow_ddl須要定義在用戶profiles中。

    <profiles>   --在profiles裏設置
        ...
        <normal> --只讀,不能DDL
            <readonly>1</readonly>
            <allow_ddl>0</allow_ddl>
        </normal>

        <normal_1> --讀且能set,不能DDL
            <readonly>2</readonly>
            <allow_ddl>0</allow_ddl>
        </normal_1>

        <normal_2> --只讀,即便DDL容許
            <readonly>1</readonly>
            <allow_ddl>1</allow_ddl>
        </normal_2>

        <normal_3> --讀寫,能DDL
            <readonly>0</readonly>
            <allow_ddl>1</allow_ddl>
        </normal_3>

    </profiles>

...
    <users>
        ...
        <test>
            <password>123456</password>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>normal_3</profile> --用戶引用相關profile
            <quota>default</quota>
        </test>
    </users>
...

說明:在profiles裏設置相應的權限角色,再在users裏引用,繼承這些參數的限制。

※ Access Rights:訪問權限控制

訪問權限在users.xml中的users選項組裏設置,用於在羣集中組合的服務器之間交換信息的用戶不得有任何限制或配額-不然,分佈式查詢將失敗。不能授予對一個數據庫有徹底訪問權限,而對另外一數據庫具備只讀訪問權限。權限控制包含以下:

  • 網絡訪問控制:經過IP地址或則host主機名
  • 數據庫訪問控制:經過read_only、allow_ddl來控制讀、寫、設置、DDL、KILL等
  • 指定數據庫訪問:經過<allow_databases>指定訪問數據庫
  • 指定表的訪問:經過filter指定表達式來訪問表中的數據行

使用

在說明部分已經對ClickHouse的用戶權限管理作了大體介紹,若是須要後續會繼續更新相關知識點。好了,如今開始對各類場景進行生成相應的用戶配置文件。

1)管理帳號:由於profiles裏默認的profile是沒有限制的,因此默認就是管理帳號。由於ClickHouse的沒有相似MySQL這樣的管理權限,因此默認狀況下管理帳號也是讀寫帳號。惟一的區別就是限制各個帳號能夠設置不一樣的使用資源。 

<?xml version="1.0"?>
<yandex>
    <profiles>
        <default>
            <max_memory_usage>10000000000</max_memory_usage>
            <use_uncompressed_cache>0</use_uncompressed_cache>
            <load_balancing>random</load_balancing>
        </default>
        <readonly>
            <readonly>1</readonly>
        </readonly>
    </profiles>

    <users>
        <default>
            <password></password>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
        </default>

        <zhoujy>
            <password_double_sha1_hex>6bb4837eb74329105ee4568dda7dc67ed2ca2ad9</password_double_sha1_hex>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
                <ip>127.0.0.1</ip>
                <ip>192.168.163.132</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
        </zhoujy>

    </users>

    <quotas>
        <default>
            <interval>
                <duration>3600</duration>
                <queries>0</queries>
                <errors>0</errors>
                <result_rows>0</result_rows>
                <read_rows>0</read_rows>
                <execution_time>0</execution_time>
            </interval>
        </default>
    </quotas>

</yandex>
View Code

2)只讀帳號:在profiles裏設置readonly,在users裏給指定用戶進行引用。

<?xml version="1.0"?>
<yandex>
    <profiles>
        <default>
            <max_memory_usage>10000000000</max_memory_usage>
            <use_uncompressed_cache>0</use_uncompressed_cache>
            <load_balancing>random</load_balancing>
        </default>
        <readonly>
            <readonly>1</readonly>
        </readonly>
    </profiles>

    <users>
        <default>
            <password></password>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
        </default>

        <zhoujy>
            <password_double_sha1_hex>6bb4837eb74329105ee4568dda7dc67ed2ca2ad9</password_double_sha1_hex>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
                <ip>127.0.0.1</ip>
                <ip>192.168.163.132</ip>
            </networks>
            <profile>readonly</profile>
            <quota>default</quota>
        </zhoujy>

    </users>

    <quotas>
        <default>
            <interval>
                <duration>3600</duration>
                <queries>0</queries>
                <errors>0</errors>
                <result_rows>0</result_rows>
                <read_rows>0</read_rows>
                <execution_time>0</execution_time>
            </interval>
        </default>
    </quotas>

</yandex>
View Code

3)讀寫帳號:若是非要和管理帳號區分的話,就限制該帳號不能使用set相關的操做,使用constraints進行設置: 

<?xml version="1.0"?>
<yandex>
    <profiles>
        <default>
            <max_memory_usage>10000000000</max_memory_usage>
            <use_uncompressed_cache>0</use_uncompressed_cache>
            <load_balancing>random</load_balancing>
        </default>

        <readonly>
            <readonly>1</readonly>
        </readonly>

        <readwrite>
            <constraints>
                <max_memory_usage>
                    <readonly/>
                </max_memory_usage>
                <force_index_by_date>
                    <readonly/>
                </force_index_by_date>
            </constraints>
        </readwrite>

    </profiles>

    <users>
        <default>
            <password></password>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
        </default>

        <zhoujy>
            <password_double_sha1_hex>6bb4837eb74329105ee4568dda7dc67ed2ca2ad9</password_double_sha1_hex>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
                <ip>127.0.0.1</ip>
                <ip>192.168.163.132</ip>
            </networks>
            <profile>readwrite</profile>
            <quota>default</quota>
        </zhoujy>

    </users>

    <quotas>
        <default>
            <interval>
                <duration>3600</duration>
                <queries>0</queries>
                <errors>0</errors>
                <result_rows>0</result_rows>
                <read_rows>0</read_rows>
                <execution_time>0</execution_time>
            </interval>
        </default>
    </quotas>

</yandex>
View Code

4)限制帳號:限制方面有不少狀況,這裏逐步進行說明

4.1:限制IP訪問,在users選項組裏進行設置ip:

...
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>   --容許任何地址訪問
                <ip>127.0.0.1</ip>  --容許本地訪問
                <ip>192.168.163.132</ip> --容許該IP訪問
            </networks>
...

設置多個IP訪問,還支持host、host_regexp等選項組。

4.2:限制數據庫訪問,在users選項組裏進行設置allow_database:

...
            <allow_databases>
                <database>test</database>
            </allow_databases>
...

只容許訪問test庫,其餘庫不能使用。

4.3:限制錶行訪問,在users選項組裏進行設置database: 

...
            <databases>
                <test>
                    <xx>
                        <filter>id >= 500 </filter>
                    </xx>
                </test>
            </databases>
...

只能訪問test庫中表xx的id大於等於500的數據。

4.4:限制必定週期內的資源使用,在quotas選項組裏設置:

...
    <quotas>
        <default>   --quotas名稱
            <interval> --週期
                <duration>3600</duration>  --週期時間,單位秒
                <queries>0</queries>   --查詢限制,0不限制
                <errors>0</errors>       --錯誤限制,0不限制
                <result_rows>0</result_rows> --返回行限制,0不限制
                <read_rows>0</read_rows>  --讀取行限制,0不限制
                <execution_time>0</execution_time> --執行時間限制,0不限制
            </interval>
        </default>
    </quotas>
...

設置好quotas以後,須要在users下的每隔用戶選項組裏引用,如: 

<quota>default</quota>

4.5:限制總體資源使用,須要在profiles選項組裏設置:

...
    <profiles>
        <default>
            <max_memory_usage>10000000000</max_memory_usage>  --限制查詢最大使用內存
            <use_uncompressed_cache>0</use_uncompressed_cache>
            <load_balancing>random</load_balancing>
        </default>
    </profiles>
...

查詢只能使用最大10000000000 bytes的內存,還能夠設置其餘的參數

5:經常使用模板設置

最後來配置一個平常常用的一個users.xml模板,大體的要求以下:

①:只讀帳號:能從任何IP訪問的帳號 zhoujy_ro

②:讀寫帳號,不能set:只能從192.168.163.132鏈接的帳號 zhoujy_rw

③:管理帳號,能讀寫能set:只能從192.168.163.13二、192.168.163.133鏈接的帳號 zhoujy_admin

④:限制帳號:

只能讀寫指定庫:只能從192.168.163.132鏈接的帳號 zhoujy_db

只能讀指定庫表中的行:只能從192.168.163.132鏈接的帳號 zhoujy_tb

模板示例:

<?xml version="1.0"?>
<yandex>
    <profiles>

        <default>
            <max_memory_usage>100000000</max_memory_usage>
            <use_uncompressed_cache>0</use_uncompressed_cache>
            <load_balancing>random</load_balancing>
        </default>

        <readonly>
            <readonly>1</readonly>
        </readonly>

        <readwrite>
            <constraints>
                <max_memory_usage>
                    <readonly/>
                </max_memory_usage>
                <force_index_by_date>
                    <readonly/>
                </force_index_by_date>
            </constraints>
        </readwrite>

    </profiles>

    <users>
        <default>
            <password></password>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
        </default>

        <!--從任何IP訪問的只讀帳號zhoujy_ro -->
        <zhoujy_ro>
            <password_double_sha1_hex>6bb4837eb74329105ee4568dda7dc67ed2ca2ad9</password_double_sha1_hex>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>readonly</profile>
            <quota>default</quota>
        </zhoujy_ro>

        <!--從指定IP訪問的讀寫帳號zhoujy_rw,指定不能set的參數 -->
        <zhoujy_rw>
            <password_double_sha1_hex>6bb4837eb74329105ee4568dda7dc67ed2ca2ad9</password_double_sha1_hex>
            <networks incl="networks" replace="replace">
                <ip>192.168.163.132</ip>
            </networks>
            <profile>readwrite</profile>
            <quota>default</quota>
        </zhoujy_rw>

        <!--從指定IP訪問的管理帳號 -->
        <zhoujy_admin>
            <password_double_sha1_hex>6bb4837eb74329105ee4568dda7dc67ed2ca2ad9</password_double_sha1_hex>
            <networks incl="networks" replace="replace">
                <ip>192.168.163.132</ip>
                <ip>192.168.163.133</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
        </zhoujy_admin>

        <!--從指定IP訪問指定數據庫 -->
        <zhoujy_db>
            <password_double_sha1_hex>6bb4837eb74329105ee4568dda7dc67ed2ca2ad9</password_double_sha1_hex>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
                <ip>127.0.0.1</ip>
                <ip>192.168.163.132</ip>
            </networks>
            <profile>readwrite</profile>
            <quota>default</quota>
            <allow_databases>
                <database>test</database>
            </allow_databases>
        </zhoujy_db>

        <!--從指定IP訪問指定數據庫表的記錄 -->
        <zhoujy_tb>
            <password_double_sha1_hex>6bb4837eb74329105ee4568dda7dc67ed2ca2ad9</password_double_sha1_hex>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
                <ip>127.0.0.1</ip>
                <ip>192.168.163.132</ip>
            </networks>
            <profile>readonly</profile>
            <quota>default</quota>
            <allow_databases>
                <database>test</database>
            </allow_databases>
            <databases>
                <test>
                    <xx>
                        <filter>id >= 500 </filter>
                    </xx>
                </test>
            </databases>
        </zhoujy_tb>

    </users>

    <quotas>
        <default>
            <interval>
                <duration>3600</duration>
                <queries>0</queries>
                <errors>0</errors>
                <result_rows>0</result_rows>
                <read_rows>0</read_rows>
                <execution_time>0</execution_time>
            </interval>
        </default>
    </quotas>

</yandex>

這裏有個問題:若是有不少用戶須要設置,該文件會很冗長,而且也不容易管理。這時能夠爲每一個用戶單獨配置一個xml文件的方式,存放在/etc/clickhouse-server/users.d目錄下便可:

# cat /etc/clickhouse-server/users.d/zhoujy_rw.xml 
<yandex>
    <users>
        <zhoujy_rw>
            <password_double_sha1_hex>6bb4837eb74329105ee4568dda7dc67ed2ca2ad9</password_double_sha1_hex>
            <networks incl="networks" replace="replace">
                <ip>192.168.163.132</ip>
            </networks>
            <profile>readwrite</profile>
            <quota>default</quota>
        </zhoujy_rw>
    </users>
</yandex>

ClickHouse對於每一個配置文件,服務器在啓動時會生成file-preprocessed.xml文件。這些文件包含全部已完成的替換和替代,好比上面的用戶雖然用單獨文件存放,但會在生成file-preprocessed.xml裏會整合在一塊兒,默認路徑在:/var/lib/clickhouse/preprocessed_configs/,即實時從新加載用戶和集羣的設置。 這意味着能夠修改羣集,用戶及其設置,而無需從新啓動服務器,固然也能夠驗證配置參數是否生效。

針對配置還能夠定義「替代」,若是元素具備incl屬性,則文件中的相應的值將被替換、刪除、追加。默認狀況下,帶替換文件的路徑爲/etc/metrika.xml。能夠在配置文件(users.xml)裏添加include_from元素來進行進行更改:

<include_from>/etc/clickhouse-server/metrika.xml</include_from>

如上面的配置文件,包含了【incl="networks"】,使用incl表示該參數能夠經過metrika.xml文件進行替換(replace="replace")、追加(默認)、刪除(remove,通過測試發現remove配置不了,後期繼續關注):

# cat /etc/clickhouse-server/metrika.xml 
<yandex>
            <networks>
                <ip>192.168.163.133</ip>
            </networks>
            <networks_admin>
                <ip>192.168.163.132</ip>
            </networks_admin>
</yandex>


# cat /etc/clickhouse-server/users.xml
...
    <users>
        <default>
            <password></password>
            <networks incl="networks_admin" repace="replace" />
            <profile>default</profile>
            <quota>default</quota>
        </default>

        <web>
            <password></password>
            <networks incl="networks">
                <ip>192.168.163.131</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
        </web>
    </users>
...

最終運行起來的時候生效的結果以下:
# cat /var/lib/clickhouse/preprocessed_configs/users.xml 
...
    <users>
        <default>
            <password/>
            <networks repace="replace"> 
                <ip>192.168.163.132</ip> ---已經replace繼承過來了 </networks>
            <profile>default</profile>
            <quota>default</quota>
        </default>

        <web>
            <password/>
            <networks>
                <ip>192.168.163.131</ip>

                <ip>192.168.163.133</ip> ---已經默認追加過來了 </networks>
            <profile>default</profile>
            <quota>default</quota>
        </web>
    </users>
...

關於用戶相關配置大體先說明到這裏,後續會持續更新。

總結

      ClickHouse用戶權限管理配置徹底不一樣於MySQL數據庫,其是經過修改文件來管理用權限的,本文只對一些經常使用的場景進行說明,後續遇到複雜狀況,也會按期更新。特別須要注意的是,在修改完權限文件以後ClickHouse不須要重啓,直接會生效,因此不影響其在線服務。若是出現問題,能夠直接查看其錯誤日誌,定位問題解決便可。

相關文章
相關標籤/搜索