docker-compose启动clickhouse数据库

1. 准备

创建 clickhouse目录,进入该目录

1.1 yml文件

创建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

1.2 获取配置文件

  • 临时启动一个clickhouse容器
[root@liubei clickhouse]# docker run -d --name chtmp yandex/clickhouse-server
[root@liubei clickhouse]# docker ps |grep chtmp
db593c4ad94b        yandex/clickhouse-server                                                 "/entrypoint.sh"         22 seconds ago      Up 21 seconds       8123/tcp, 9000/tcp, 9009/tcp                                                               chtmp
  • 为配置文件创建一个conf目录,保证至少755权限
[root@liubei clickhouse]# mkdir conf
[root@liubei clickhouse]# ll
total 5
drwxr-xr-x 2 root root 4096 Jun 19 13:24 conf
  • 从容器中拷贝出配置文件
[root@liubei clickhouse]# cd conf/
[root@liubei conf]# docker cp chtmp:/etc/clickhouse-server/config.d/docker_related_config.xml ./
[root@liubei conf]# docker cp chtmp:/etc/clickhouse-server/config.xml ./
[root@liubei conf]# docker cp chtmp:/etc/clickhouse-server/users.xml ./
-rw-rw-rw- 1 root root 60072 Dec 26  2021 config.xml
-rw-rw-r-- 1 root root   314 Dec 26  2021 docker_related_config.xml
-rw-rw-rw- 1 root root  6248 Dec 26  2021 users.xml
  • 关闭临时容器
[root@liubei clickhouse]# docker stop chtmp
chtmp
[root@liubei clickhouse]# docker rm chtmp
chtmp

2. 启动

clickhouse目录下执行:

[root@liubei clickhouse]# docker-compose up -d
Creating network "clickhouse_default" with the default driver
......
Creating clickhouse_ch_server_1 ... done
Creating clickhouse_ch_client_1 ... done
[root@liubei clickhouse]# docker-compose ps
         Name                  Command         State                                 Ports
-------------------------------------------------------------------------------------------------------------------------
clickhouse_ch_client_1   /bin/sleep infinity   Up
clickhouse_ch_server_1   /entrypoint.sh        Up      0.0.0.0:8123->8123/tcp, 9000/tcp, 9009/tcp, 0.0.0.0:9100->9100/tcp

3. 测试

此时我们没有启动密码,进入容器直接登录即可。

  • 进入容器
[root@liubei clickhouse]# docker ps |grep click
5c7d20e24a1a        yandex/clickhouse-server                                                 "/entrypoint.sh"         6 minutes ago       Up 6 minutes        9000/tcp, 0.0.0.0:8123->8123/tcp, 0.0.0.0:9100->9100/tcp, 9009/tcp                         clickhouse_ch_server_1
8d56441b8e1c        yandex/clickhouse-client                                                 "/bin/sleep infinity"    6 minutes ago       Up 6 minutes                                                                                                   clickhouse_ch_client_1
[root@liubei clickhouse]# docker exec -it 5c7d bash
root@5c7d20e24a1a:/#
  • 登录clickhouse
root@5c7d20e24a1a:/# clickhouse-client
ClickHouse client version 21.12.3.32 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 21.12.3 revision 54452.

5c7d20e24a1a :) show databases;

SHOW DATABASES

Query id: 07305aa0-1c7e-47c9-bf16-de78b763c01d

┌─name───────────────┐
│ INFORMATION_SCHEMA │
│ default            │
│ information_schema │
│ system             │
└────────────────────┘

4 rows in set. Elapsed: 0.001 sec.

5c7d20e24a1a :)

4. 用户名密码

4.1 默认用户

  • 修改刚才拷贝的配置文件中的users.xml文件

修改<users><default>部分,<password></password>一行如下

    <users>
        <!-- If user name was not specified, 'default' user is used. -->
        <default>
            ......
            <password>liubei@161</password>
            <networks>
                <ip>::/0</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
        </default>
    </users>
  • 测试
root@5c7d20e24a1a:/# clickhouse-client --password liubei@161
ClickHouse client version 21.12.3.32 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 21.12.3 revision 54452.

5c7d20e24a1a :) show databases

SHOW DATABASES

Query id: 281c3ebe-73f0-45b5-a2bc-fc9757dfffe7

┌─name───────────────┐
│ INFORMATION_SCHEMA │
│ default            │
│ information_schema │
│ system             │
└────────────────────┘
4 rows in set. Elapsed: 0.001 sec.

4.2 创建其他用户

<users>中,为liubei用户 添加<liubei>部分,并配置密码。(建议在 <default>后边)

    <users>
        <!-- If user name was not specified, 'default' user is used. -->
        <default>
            ......
        </default>
        <liubei>
            <password>liubei@161</password>
            <networks incl="networks" replace="replace">
                <ip>::/0</ip>
            </networks>
            <profile>default</profile>
            <quota>default</quota>
            <allow_databases>
                <database>sdcs</database>
            </allow_databases>
        </liubei>
    </users>
  • 测试
root@5c7d20e24a1a:/# clickhouse-client -u liubei --password liubei@161
ClickHouse client version 21.12.3.32 (official build).
Connecting to localhost:9000 as user liubei.
Connected to ClickHouse server version 21.12.3 revision 54452.
5c7d20e24a1a :) 

在这里插入图片描述

posted on 2023-06-20 07:30  运维开发玄德公  阅读(243)  评论(0编辑  收藏  举报  来源

导航