1、下载安装包
docker pull yandex/clickhouse-server
2、 创建临时容器 运行
docker run --rm -d --name=clickhouse \
--ulimit nofile=262144:262144 \
-p 8123:8123 -p 9009:9009 -p 9090:9000 \
yandex/clickhouse-server
或者创建容器运行
docker run -d --name=clickhouse --ulimit nofile=262144:262144 -p 8123:8123 -p 9009:9009 -p 9090:9000 yandex/clickhouse-server
3、复制临时容器内config.xml配置文件到宿主机
docker cp clickhouse:/etc/clickhouse-server/config.xml /home/data/clickhouse/conf/config.xml
docker cp clickhouse:/etc/clickhouse-server/users.xml /home/data/clickhouse/conf/users.xml
4、修改宿主机users.xml配置文件中default账号密码
1、修改default账号密码为明文
vi /home/data/clickhouse/conf/users.xml
<!-- Users and ACL. -->
<users>
<!-- If user name was not specified, 'default' user is used. -->
<default>
<!-- Password could be specified in plaintext or in SHA256 (in hex format).
If you want to specify password in plaintext (not recommended), place it in 'password' element.
Example: <password>qwerty</password>.
Password could be empty.
If you want to specify SHA256, place it in 'password_sha256_hex' element.
Example: <password_sha256_hex>65e84be33532fb784c48129675f9eff3a682b27168c0ea744b2cf58ee02337c5</password_sha256_hex>
Restrictions of SHA256: impossibility to connect to ClickHouse using MySQL JS client (as of July 2019).
If you want to specify double SHA1, place it in 'password_double_sha1_hex' element.
Example: <password_double_sha1_hex>e395796d6546b1b65db9d665cd43f0e858dd4303</password_double_sha1_hex>
How to generate decent password:
Execute: PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha256sum | tr -d '-'
In first line will be password and in second - corresponding SHA256.
How to generate double SHA1:
Execute: PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha1sum | tr -d '-' | xxd -r -p | sha1sum | tr -d '-'
In first line will be password and in second - corresponding double SHA1.
这里改为明文密码
-->
**<password>123456</password>**
<networks>
<ip>::/0</ip>
</networks>
<!-- Settings profile for user. -->
<profile>default</profile>
<!-- Quota for user. -->
<quota>default</quota>
<!-- User can create other users and grant rights to them. -->
<!-- <access_management>1</access_management> -->
</default>
</users>
密码修改完成后,启动clickhouse容器,登录测试验证。
创建新的容器并启动
把宿主机的目录映射到容器内
docker run -dit --name=clickhouse \
-p 8123:8123 -p 9009:9009 -p 9090:9000 \
--ulimit nofile=262144:262144 \
-v /home/data/clickhouse/data:/var/lib/clickhouse:rw \
-v /home/data/clickhouse/conf/config.xml:/etc/clickhouse-server/config.xml \
-v /home/data/clickhouse/conf/users.xml:/etc/clickhouse-server/users.xml \
-v /home/data/clickhouse/log:/var/log/clickhouse-server:rw \
yandex/clickhouse-server
配置详细说明:
-dit: 表示后台交互运行
--name=clickhouse: 指定启动容器名称
-p 8123:8123 -p 9009:9009 -p 9090:9000: 指定启动容器对外映射端口,格式为宿主机端口:容器端口。
-v /home/data/clickhouse/data:/var/lib/clickhouse:rw: 将启动容器的数据、配置文件、日志挂在到宿主机目录,方便后续修改文件及查看日志。例如修改配置,直接修改宿主机中文件会自动更新容器配置文件。
yandex/clickhouse-server: 容器镜像名
查看进程:docker ps
clickhoust 默认http端口是8123,tcp端口是9000,同步端口9009
web可视化界面:http://ip:8123/play ,ip为你当前机器的ip地址,该界面是clickhouse自带命令行,可以通过输入账号密码执行sql命令。
注意:
1、不能将该地址的ClickHouse服务器暴露给公共互联网,这种肉鸡方式,直接被攻击瘫痪,如果要对外使用,建议增加https证书方式认证。
客户端连接
命令行客户端
ClickHouse提供了一个原生命令行客户端clickhouse-client客户端支持命令行支持的更多信息详见Configuring。
安装部署后,系统默认会安装clickhouse-client(同时它属于clickhouse-client安装包中)。
不同的客户端和服务器版本彼此兼容,但是一些特性可能在旧客户机中不可用。我们建议使用与服务器应用相同版本的客户端。当你尝试使用旧版本的客户端时,服务器上的clickhouse-client会显示如下信息:
ClickHouse client version is older than ClickHouse server. It may lack support for new features.
步骤:
1.查看clickhouse进程:docker ps
2.进入容器内部: docker exec -it e1d6ef1133b9 /bin/bash
3.查询clickhouse-client 命令所在目录:ls /bin/
3.执行命令查看版本: ./bin/clickhouse-client
[root@node103 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f1b489108eea yandex/clickhouse-server "/entrypoint.sh" About a minute ago Up About a minute 0.0.0.0:8123->8123/tcp, :::8123->8123/tcp, 0.0.0.0:9009->9009/tcp, :::9009->9009/tcp, 0.0.0.0:9090->9000/tcp, :::9090->9000/tcp clickhouse
67d14ac84759 mysql:5.7 "docker-entrypoint.s…" 18 months ago Up 35 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@node103 ~]#
[root@node103 ~]#
[root@node103 ~]# docker exec -it f1b489108eea /bin/bash
root@f1b489108eea:/#
root@f1b489108eea:/# /bin/clickhouse-client --password
ClickHouse client version 21.12.3.32 (official build).
Password for user (default): 输入users.xml配置文件中明文密码
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 21.12.3 revision 54452.
f1b489108eea :)
可以看到版本如下:
ClickHouse client version 21.12.3.32 (official build).
连接地址为: localhost:9000,登录账号为default
Connecting to localhost:9000 as user default.
idea datasource客户端
以下使用idea自带插件datasource连接测试。
右侧菜单栏datasource>data source>clickhouse
填写登录ip、账号、密码,点击download下载clickhouse驱动包。
点击test connection连接测试,提示成功表示连接正常,错误需要根据具体问题分析原因。
点击apply保存,点击展开查看数据库及表信息。
测试通过命令行执行,点击sql图标打开命令行,输入命令,点击绿色小箭头执行sql,可以看到下方执行结果。
以上通过命令行连接clickhouse,远程链接两种方式验证都正常,表示clickhouse安装成功。
修改密码
1、明文密码,修改default账号密码为明文
vi /home/data/clickhouse/conf/users.xml
<!-- Users and ACL. -->
<users>
<!-- If user name was not specified, 'default' user is used. -->
<default>
<!-- Password could be specified in plaintext or in SHA256 (in hex format).
If you want to specify password in plaintext (not recommended), place it in 'password' element.
Example: <password>qwerty</password>.
Password could be empty.
If you want to specify SHA256, place it in 'password_sha256_hex' element.
Example: <password_sha256_hex>65e84be33532fb784c48129675f9eff3a682b27168c0ea744b2cf58ee02337c5</password_sha256_hex>
Restrictions of SHA256: impossibility to connect to ClickHouse using MySQL JS client (as of July 2019).
If you want to specify double SHA1, place it in 'password_double_sha1_hex' element.
Example: <password_double_sha1_hex>e395796d6546b1b65db9d665cd43f0e858dd4303</password_double_sha1_hex>
How to generate decent password:
Execute: PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha256sum | tr -d '-'
In first line will be password and in second - corresponding SHA256.
How to generate double SHA1:
Execute: PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha1sum | tr -d '-' | xxd -r -p | sha1sum | tr -d '-'
In first line will be password and in second - corresponding double SHA1.
这里改为明文密码
-->
**<password>123456</password>**
<networks>
<ip>::/0</ip>
</networks>
<!-- Settings profile for user. -->
<profile>default</profile>
<!-- Quota for user. -->
<quota>default</quota>
<!-- User can create other users and grant rights to them. -->
<!-- <access_management>1</access_management> -->
</default>
</users>
密码修改完成后,启动clickhoust容器,登录测试验证。
2、SHA256加密
例如对密码123456进行SHA256加密
[root@node103 ~]# echo -n 123456 | openssl dgst -sha256
(stdin)= 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
[root@node103 ~]#
1、修改users.xml配置密码
[root@node103 ~]# vim /home/data/clickhouse/conf/users.xml
<!-- Users and ACL. -->
<users>
<!-- If user name was not specified, 'default' user is used. -->
<default>
<!-- Password could be specified in plaintext or in SHA256 (in hex format).
If you want to specify password in plaintext (not recommended), place it in 'password' element.
Example: <password>qwerty</password>.
Password could be empty.
If you want to specify SHA256, place it in 'password_sha256_hex' element.
Example: <password_sha256_hex>65e84be33532fb784c48129675f9eff3a682b27168c0ea744b2cf58ee02337c5</password_sha256_hex>
Restrictions of SHA256: impossibility to connect to ClickHouse using MySQL JS client (as of July 2019).
If you want to specify double SHA1, place it in 'password_double_sha1_hex' element.
Example: <password_double_sha1_hex>e395796d6546b1b65db9d665cd43f0e858dd4303</password_double_sha1_hex>
How to generate decent password:
Execute: PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha256sum | tr -d '-'
In first line will be password and in second - corresponding SHA256.
How to generate double SHA1:
Execute: PASSWORD=$(base64 < /dev/urandom | head -c8); echo "$PASSWORD"; echo -n "$PASSWORD" | sha1sum | tr -d '-' | xxd -r -p | sha1sum | tr -d '-'
In first line will be password and in second - corresponding double SHA1.
-->
<!--
<password>123456</password>
-->
<password_sha256_hex>8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92</password_sha256_hex>
<networks>
<ip>::/0</ip>
</networks>
<!-- Settings profile for user. -->
<profile>default</profile>
<!-- Quota for user. -->
<quota>default</quota>
<!-- User can create other users and grant rights to them. -->
<!-- <access_management>1</access_management> -->
</default>
</users>
2、重启服务
[root@node103 ~]#
[root@node103 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f1b489108eea yandex/clickhouse-server "/entrypoint.sh" 24 minutes ago Up 31 seconds 0.0.0.0:8123->8123/tcp, :::8123->8123/tcp, 0.0.0.0:9009->9009/tcp, :::9009->9009/tcp, 0.0.0.0:9090->9000/tcp, :::9090->9000/tcp clickhouse
67d14ac84759 mysql:5.7 "docker-entrypoint.s…" 18 months ago Up 36 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@node103 ~]#docker restart f1b489108eea
[root@node103 ~]#
3、查看进程
[root@node103 ~]#
[root@node103 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f1b489108eea yandex/clickhouse-server "/entrypoint.sh" 24 minutes ago Up 31 seconds 0.0.0.0:8123->8123/tcp, :::8123->8123/tcp, 0.0.0.0:9009->9009/tcp, :::9009->9009/tcp, 0.0.0.0:9090->9000/tcp, :::9090->9000/tcp clickhouse
67d14ac84759 mysql:5.7 "docker-entrypoint.s…" 18 months ago Up 36 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@node103 ~]#
4、进入clickhouse容器
[root@node103 ~]#
[root@node103 ~]# docker exec -it f1b489108eea /bin/bash
root@f1b489108eea:/#
5、连接clickhouse,输入未加密密码
root@f1b489108eea:/#
root@f1b489108eea:/# ./bin/clickhouse-client --password
ClickHouse client version 21.12.3.32 (official build).
Password for user (default): 输入未加密密码
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 21.12.3 revision 54452.
6、查看数据库
f1b489108eea :) show databases;
SHOW DATABASES
Query id: d72ed485-1e8e-4ced-9369-1a76683ac5a9
┌─name───────────────┐
│ INFORMATION_SCHEMA │
│ default │
│ information_schema │
│ system │
└────────────────────┘
4 rows in set. Elapsed: 0.130 sec.
f1b489108eea :)
以上表示修改使用加密后的密码成功。
ClickHouse SQL 语句
参考:https://clickhouse.com/docs/zh/sql-reference/statements
ClickHouse支持以下形式的查询:
SELECT
INSERT INTO
CREATE
ALTER
其他类型的查询
ClickHouse访问权限控制
https://clickhouse.com/docs/zh/operations/access-rights
接口
https://clickhouse.com/docs/zh/interfaces