点击上方“IT那活儿”公众号--专注于企业全栈运维技术分享,不管IT什么活儿,干就完了!!!
安装clickhouse
clickhouse-client-23.9.1.1854.x86_64.rpm clickhouse-common-static-23.9.1.1854.x86_64.rpm clickhouse-server-23.9.1.1854.x86_64.rpm
rpm -ivh *.rpm
/etc/clickhouse-server/*
<!--修改日志存放路径-->
<log>/data/clickhouse/logs/clickhouse-server.log</log>
<errorlog>/data/clickhouse/logs/clickhouse-server.err.log</errorlog>
<!--取消远程主机访问限制-->
<listen_host>::</listen_host>
<!--引用副配置文件-->
<include_from>/etc/clickhouse-server/metrika.xml</include_from>
<remote_servers incl="clickhouse_remote_servers" />
<zookeeper incl="zookeeper-servers" optional="true" />
<?xml version="1.0" encoding="utf-8"?>
<yandex>
<clickhouse_remote_servers>
<test_cluster>
<shard>
<replica>
<internal_replication>true</internal_replication>
<host>***.***.***.193</host>
<port>9000</port>
</replica>
<replica>
<internal_replication>true</internal_replication>
<host>***.***.***.194</host>
<port>9000</port>
</replica>
</shard>
<shard>
<replica>
<internal_replication>true</internal_replication>
<host>***.***.***.195</host>
<port>9000</port>
</replica>
<replica>
<internal_replication>true</internal_replication>
<host>***.***.***.196</host>
<port>9000</port>
</replica>
</shard>
</test_cluster>
</clickhouse_remote_servers>
<zookeeper-servers>
<node>
<host>***.***.***.193</host>
<port>2181</port>
</node>
<node>
<host>***.***.***.194</host>
<port>2181</port>
</node>
<node>
<host>***.***.***.195</host>
<port>2181</port>
</node>
</zookeeper-servers>
<!--相同分片的节点的shard一致,所有节点的replica都不一致,一般采用主机名-->
<macros>
<cluster>test_cluster</cluster>
<shard>02</shard>
<replica>clickhouse-04</replica>
</macros>
<networks>
<ip>::/0</ip>
</networks>
<clickhouse_compression>
<case>
<min_part_size>10000000000</min_part_size>
<min_part_size_ratio>0.01</min_part_size_ratio>
<method>lz4</method>
</case>
</clickhouse_compression>
</yandex>
clickhouse
systemctl start clickhouse-server

clickhouse应用
CREATE DATABASE itil on cluster test_cluster
CREATE TABLE itil.application_log_information_local on cluster test_cluster
(
`LOG_ID` UUID DEFAULT generateUUIDv4(),
.......
`END_TIME` DateTime,
`COST_TIME` UInt64
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/application_log_information','{replica}')
PARTITION BY toYYYYMMDD(START_TIME)
ORDER BY (API_LOG_TYPE,SYSTEM_ID,API_ID,START_TIME)
TTL toDateTime(START_TIME) + toIntervalDay(90)
SETTINGS index_granularity = 8192;
Distributed(cluster_name, database_name, table_name[, sharding_key])
* cluster_name:集群名称; * database_name:数据库名称; * table_name:表名称; * sharding_key:可选的,用于分片的key值,在数据写入的过程中,分布式表会依据分片key的规则,将数据分布到各个节点的本地表。
CREATE TABLE itil.application_log_information_all on cluster test_cluster as itil.application_log_information_local
ENGINE = Distributed(test_cluster, itil, application_log_information_local, rand());
* rand():按照随机数划分。
<replica>
<internal_replication>true</internal_replication>
<host>***.***.***.***</host>
<port>9000</port>
<user>*****</user>
<password>******</password>
</replica>
application_log_information_all并不是一张物理表,而是一张逻辑表,本地并不存在。
clickhouse角色及用户创建
<profiles>
<!-- Default settings. -->
<default>
<max_memory_usage>429496729600</max_memory_usage>
<load_balancing>random</load_balancing>
<background_pool_size>64</background_pool_size>
<background_fetches_pool_size>64</background_fetches_pool_size>
<background_distributed_schedule_pool_size>64</background_distributed_schedule_pool_size>
<background_schedule_pool_size>64</background_schedule_pool_size>
</default>
<itil_users>
<distributed_product_mode>allow</distributed_product_mode>
<max_memory_usage>429496729600</max_memory_usage>
<load_balancing>random</load_balancing>
<background_pool_size>64</background_pool_size>
<background_fetches_pool_size>64</background_fetches_pool_size>
<background_distributed_schedule_pool_size>64</background_distributed_schedule_pool_size>
<background_schedule_pool_size>64</background_schedule_pool_size>
</itil_users>
<!-- Profile that allows only read queries. -->
<readonly>
<readonly>1</readonly>
</readonly>
</profiles>
<users>
<!-- If user name was not specified, 'default' user is used. -->
<default>
<password></password>
<networks>
<ip>::/0</ip>
</networks>
<profile>default</profile>
<quota>default</quota>
</default>
<itil>
<!--此处密码使用sha256加密,加密代码附在最后-->
<password_sha256_hex>95752e432cfa6b486dc90e230c4889000216a5de70e982f026a30bda65bc26e8</password_sha256_hex>
<networks>
<ip>::/0</ip>
</networks>
<profile>itil_users</profile>
<quota>default</quota>
<!--限制itil用户只能访问itil数据库,对语法没有任何限制 -->
<allow_databases>
<database>itil</database>
</allow_databases>
</itil>
</users>
clickhouse权限分配
#创建数据库
CREATE DATABASE databasename ON CLUSTER clustername;
#创建用户
CREATE USER IF NOT EXISTS databaseuser ON CLUSTER clustername IDENTIFIED WITH plaintext_password BY 'password';
#分配权限
GRANT ALL PRIVILEGES ON databasename.* TO 'databaseuser'@'%' ON CLUSTER clustername;
#分配远程权限
grant REMOTE ON *.* to databaseuser on cluster clustername;
密码加密
echo -n "your_password" | openssl dgst -sha256
from hashlib import sha256
def generate_sha256_hashCode(plainText):
plainTextBytes = plainText.encode('utf-8') #字符串在哈希之前,需要编码
encryptor = sha256()
encryptor.update(plainTextBytes)
hashCode = encryptor.hexdigest()
print(hashCode)
if __name__ == "__main__":
generate_sha256_hashCode('your_password')
clickhouse使用注意
配置文件路径:/etc/clickhouse-server/config.xml 日志文件路径:/var/log/clickhouse-server/ 建表信息路径:/var/lib/clickhouse/ metadata分区数据路径:/var/lib/clickhouse/data

本文作者:胡京康(上海新炬中北团队)
本文来源:“IT那活儿”公众号





