【转载:https://www.cnblogs.com/crow1840/p/17506884.html】
1、创建clickhouse目录
[root@localhost docker]# ls
gitlab_docker jenkins_docker mysql_docker
[root@localhost docker]# mkdir clickhouse/conf -p
2.创建docker-compose.yml
[root@localhost clickhouse]# cat docker-compose.yml
version: '3'
services:
ch_server:
image: yandex/clickhouse-server
ports:
- "8123:8123"
- "9100:9100"
volumes:
- ./conf/docker_related_config.xml:/etc/clickhouse-server/config.d/docker_related_config.xml:rw
- ./conf/config.xml:/etc/clickhouse-server/config.xml:rw
- ./conf/users.xml:/etc/clickhouse-server/users.xml:rw
- /etc/localtime:/etc/localtime:ro
- ./log:/var/log/clickhouse-server
- ./db:/var/lib/clickhouse:rw
ch_client:
image: yandex/clickhouse-client
entrypoint:
- /bin/sleep
command:
- infinity
3、配置文件
1)临时启动一个clickhouse容器
[root@localhost clickhouse]# docker run -d --name chtmp yandex/clickhouse-server
Unable to find image 'yandex/clickhouse-server:latest' locally
latest: Pulling from yandex/clickhouse-server
ea362f368469: Pull complete
38ba82a23e2b: Pull complete
9b17d04b6c62: Pull complete
5658714e4e8b: Pull complete
6bde977a0bf8: Pull complete
39053b27290b: Pull complete
762d3d237065: Pull complete
Digest: sha256:1cbf75aabe1e2cc9f62d1d9929c318a59ae552e2700e201db985b92a9bcabc6e
Status: Downloaded newer image for yandex/clickhouse-server:latest
8d0d3076bef1ba56cb4adf45cf1584e6874e8c4753dc2ea0657e7a5efcbde9a0
[root@localhost clickhouse]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d0d3076bef1 yandex/clickhouse-server "/entrypoint.sh" 28 seconds ago Up 26 seconds 8123/tcp, 9000/tcp, 9009/tcp chtmp
2) 从容器中拷贝出配置文件
[root@localhost conf]# docker cp chtmp:/etc/clickhouse-server/config.d/docker_related_config.xml ./
Successfully copied 2.05kB to /usr/local/docker/clickhouse/conf/./
[root@localhost conf]# docker cp chtmp:/etc/clickhouse-server/config.xml ./
Successfully copied 63.5kB to /usr/local/docker/clickhouse/conf/./
[root@localhost conf]# docker cp chtmp:/etc/clickhouse-server/users.xml ./
Successfully copied 8.19kB to /usr/local/docker/clickhouse/conf/./
[root@localhost conf]# ll
总用量 76
-rw-rw-rw- 1 root root 61476 1月 23 2022 config.xml
-rw-rw-r-- 1 root root 314 1月 23 2022 docker_related_config.xml
-rw-rw-rw- 1 root root 6248 1月 23 2022 users.xml
3) 关闭临时容器
[root@localhost conf]# docker stop chtmp
chtmp
[root@localhost conf]# docker rm chtmp
chtmp
4、启动
[root@localhost clickhouse]# docker-compose up -d
[+] Running 5/5
✔ ch_client 4 layers [⣿⣿⣿⣿] 0B/0B Pulled 719.5s
✔ 2f94e549220a Pull complete 24.6s
✔ a72d8599d7c2 Pull complete 1.7s
✔ e9232762ed9d Pull complete 702.2s
✔ 29c8f4b1e77e Pull complete 6.0s
[+] Running 2/3
⠧ Network clickhouse_default Created 1.7s
✔ Container clickhouse-ch_server-1 Started 1.5s
✔ Container clickhouse-ch_client-1 Started 1.5s
[root@localhost clickhouse]# docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
clickhouse-ch_client-1 yandex/clickhouse-client "/bin/sleep infinity" ch_client 14 seconds ago Up 12 seconds
clickhouse-ch_server-1 yandex/clickhouse-server "/entrypoint.sh" ch_server 14 seconds ago Up 12 seconds 9000/tcp, 0.0.0.0:8123->8123/tcp, :::8123->8123/tcp, 0.0.0.0:9100->9100/tcp, :::9100->9100/tcp, 9009/tcp
[root@localhost clickhouse]# docker-compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
clickhouse-ch_client-1 yandex/clickhouse-client "/bin/sleep infinity" ch_client 17 seconds ago Up 15 seconds
clickhouse-ch_server-1 yandex/clickhouse-server "/entrypoint.sh" ch_server 17 seconds ago Up 15 seconds 9000/tcp, 0.0.0.0:8123->8123/tcp, :::8123->8123/tcp, 0.0.0.0:9100->9100/tcp, :::9100->9100/tcp, 9009/tcp
5、测试
[root@localhost conf]# docker exec -it -u root 73b2e11644b3 bash
root@73b2e11644b3:/# clickhouse-client
ClickHouse client version 22.1.3.7 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 22.1.3 revision 54455.
73b2e11644b3 :) show databases
SHOW DATABASES
Query id: bd604a15-5f23-4b21-86cb-7560e62744e1
┌─name───────────────┐
│ INFORMATION_SCHEMA │
│ default │
│ information_schema │
│ system │
└────────────────────┘
4 rows in set. Elapsed: 0.003 sec.
73b2e11644b3 :) exit
Bye.
root@73b2e11644b3:/# exit
exit
6、用户名密码
1) 默认用户
修改刚才拷贝的配置文件中的users.xml文件
修改<users>中<default>部分,<password></password>一行如下
<users>
<!-- If user name was not specified, 'default' user is used. -->
<default>
......
<password>wjht#%$347</password>
<networks>
<ip>::/0</ip>
</networks>
<profile>default</profile>
<quota>default</quota>
</default>
</users>
2) 测试登录
root@73b2e11644b3:/# clickhouse-client --password 'wjht#%$347'
ClickHouse client version 22.1.3.7 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 22.1.3 revision 54455.
73b2e11644b3 :) show databases
SHOW DATABASES
Query id: 959365a2-db8b-44e9-ad5e-5a22c711fb63
┌─name───────────────┐
│ INFORMATION_SCHEMA │
│ default │
│ information_schema │
│ system │
└────────────────────┘
5 rows in set. Elapsed: 0.004 sec.
7、创建其他用户
<users>
<!-- If user name was not specified, 'default' user is used. -->
<default>
......
</default>
<wjhtwxx>
<password>root123</password>
<networks incl="networks" replace="replace">
<ip>::/0</ip>
</networks>
<profile>default</profile>
<quota>default</quota>
<allow_databases>
<database>aaa</database>
</allow_databases>
</wjhtwxx>
</users>
标签:compose,tcp,server,clickhouse,docker,root,localhost
From: https://www.cnblogs.com/sddll/p/18058882