1. 程式人生 > >ClickHouse學習系列之二【使用者許可權管理】

ClickHouse學習系列之二【使用者許可權管理】

背景

      最近花了些時間看了下ClickHouse文件,發現它在OLAP方面表現很優異,而且相對也比較輕量和簡單,所以準備入門瞭解下該資料庫系統。在安裝完之後首先做的應該如何設定使用者密碼以及許可權控制。因為和MySQL、MongoDB等資料庫的使用者管理完全不一樣,為方便自己以後直接查閱,本文對其使用者許可權管理方面進行梳理說明。

說明

      ClickHouse作為一個分析型別(OLAP)的資料庫系統,相對於MySQL資料庫在使用者管理方面有很大不同,它是通過修改配置檔案來實現使用者許可權管理的。在安裝好ClickHouse之後,其預設的配置檔案在/etc/clickhouse-server目錄下,對應的配置檔案為users.xml,ClickHouse使用它來定義使用者相關的配置項。現在開始對其進行說明,對應手冊裡的說明包含以下幾個方面:

  • Settings profiles 
  • User settings
  • Constraints on Settings
  • Quotas
  • Permissions for queries
  • Access Rights

注意一點,修改了user.xml的引數之後是即時生效的,如有問題可以檢視其錯誤日誌。好了,現在開始對這些進行說明,先熟悉使用者許可權管理這一方面的相關操作。

※ Settings profiles :設定使用者配置檔案

profile的作用類似於使用者角色,可以在user.xml中定義多組profile,並可以為每組profile定義不同的配置項,類限制資源的使用。多個profile的配置可以複用。咋眼一看有點和MySQL的Proxy許可權類似。

模板:

    <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選項組裡進行指定(後面會說明)。

[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)繼承過來了。 

※ 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_ddl:DDL許可權由此標籤控制,它有兩種取值:

  • 當取值為0時,不允許DDL查詢;

  • 當取值為1時,允許DDL查詢(預設值)

如果當前會話的allow_ddl = 0,則無法執行SET allow_ddl = 1

注意:KILL QUERY可以在任何設定上執行,readonly和allow_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.132、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>

總結

      ClickHouse使用者許可權管理配置完全不同於MySQL資料庫,其是通過修改檔案來管理用許可權的,本文只對一些常用的場景進行說明,後續遇到複雜情況,也會定期更新。特別需要注意的是,在修改完許可權檔案之後ClickHouse不需要重啟,直接會生效,所以不影響其線上服務。如果出現問題,可以直接檢視其錯誤日誌,定位問題解決即可。

&n