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.
[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.
測試說明已經把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.
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.
※ 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`.
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`.
從實施定義的固定時刻開始計算時間間隔。間隔結束時,將清除所有收集的值。 接下來的一個小時,配額計算將重新開始。對於分布式查詢處理,累積量存儲在請求者服務器上。 因此,如果用戶轉到另一台服務器,則那里的配額將重新開始。重新啟動服務器后,配額將重置。
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
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
※ 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>
<?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>
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>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>
<?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>
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>
<?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>
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>
這里有個問題:如果有很多用戶需要設置,該文件會很冗長,並且也不容易管理。這時可以為每個用戶單獨配置一個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> ...
關於用戶相關配置大致先說明到這里,后續會持續更新。
<password/> <networks> <ip>192.168.163.131</ip> <ip>192.168.163.133</ip> ---已經默認追加過來了 </networks> <profile>default</profile> <quota>default</quota> </web> </users> ...
總結
ClickHouse用戶權限管理配置可以通過修改文件來管理用權限的,也可以通過RBAC(Role-Based Access Control)的訪問控制管理,即通過SQL-driven,后續有文檔說明。需要注意的是,在修改完權限文件之后ClickHouse不需要重啟,直接會生效,所以不影響其在線服務。如果出現問題,可以直接查看其錯誤日志,定位問題解決即可。