# clickHouse-数据库部署 *平台部署知识* 本文将介绍如何下载和安装ClickHouse,以及如何配置ClickHouse服务器和客户端,以实现数据库的初始设置和运行 ## 目录 [TOC]  ## 单节点部署 ### APT 安装 #### 准备环境 ##### 检查 grep 我们需要通过下面的命令,检查是否可以进行 clickhouse 的安装,在运行下面的命令之后,如果返回`SSE 4.2 supported`则代表可以安装。 ``` grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 supported" || echo "SSE 4.2 not supported" ``` ##### 检查 apt 命令是否存在 可以直接通过调用 apt 来判断是否存在,如果没有报找不到的错误就代表此命令存在,下面是一个代表命令存在的日志示例。 ``` root@liming-virtual-machine:~# apt apt 2.4.8 (amd64) Usage: apt [options] command apt is a commandline package manager and provides commands for searching and managing as well as querying information about packages. It provides the same functionality as the specialized APT tools, like apt-get and apt-cache, but enables options more suitable for interactive use by default. Most used commands: list - list packages based on package names search - search in package descriptions show - show package details install - install packages reinstall - reinstall packages remove - remove packages autoremove - Remove automatically all unused packages update - update list of available packages upgrade - upgrade the system by installing/upgrading packages full-upgrade - upgrade the system by removing/installing/upgrading packages edit-sources - edit the source information file satisfy - satisfy dependency strings See apt(8) for more information about the available commands. Configuration options and syntax is detailed in apt.conf(5). Information about how to configure sources can be found in sources.list(5). Package and version choices can be expressed via apt_preferences(5). Security details are available in apt-secure(8). This APT has Super Cow Powers. ``` ##### 添加 apt 源 由于默认的乌班图 apt 源下载太慢,所以我们要使用阿里云的镜像来进行下载,所以要配置阿里云镜像,首先我们执行`vim /etc/apt/sources.list`命令进入到 apt 的镜像源文件,然后在第一行添加`deb http://repo.yandex.ru/clickhouse/deb/stable/ main/` 这样就配置好了apt的源,下面是一个示例。 ``` root@liming-virtual-machine:~# cat /etc/apt/sources.list #deb cdrom:[Ubuntu 22.04.2 LTS _Jammy Jellyfish_ - Release amd64 (20230223)]/ jammy main restricted deb http://repo.yandex.ru/clickhouse/deb/stable/ main/ ``` ##### 更新 apt 数据 以及 dirmngr 的安装 执行下面的代码就可以实现更新 以及 dirmngr 的安装。 ``` apt install dirmngr apt-key adv --keyserver keyserver.ubuntu.com --recv E0C56BD4 apt update ``` #### 开始部署 ##### apt 安装 我们只需要在这里执行一个 apt 的命令就可以自动的联网下载安装包! ``` apt-get install clickhouse-client clickhouse-server ``` ###### 安装的过程中可能需要配置密码 Click House是支持用户管理的,因此需要创建一个初始用户,在新版本的Click House安装的时候会自动的弹出录入提示,由用户指定初始用户的密码,初始用户名为default,在老版本中是不允许用户指定的,直接是无密码,新版本中可以手动指定。 ``` Creating pid directory /var/run/clickhouse-server. chown -R clickhouse:clickhouse '/var/log/clickhouse-server/' chown -R clickhouse:clickhouse '/var/run/clickhouse-server' chown clickhouse:clickhouse '/var/lib/clickhouse/' groupadd -r clickhouse-bridge useradd -r --shell /bin/false --home-dir /nonexistent -g clickhouse-bridge clickhouse-bridge chown -R clickhouse-bridge:clickhouse-bridge '/usr/bin/clickhouse-odbc-bridge' chown -R clickhouse-bridge:clickhouse-bridge '/usr/bin/clickhouse-library-bridge' Enter password for default user: ``` ###### 进入到 clickhouse 的目录 安装好的 click house 目录位于 `/etc` 中 这里有两个目录 分别是客户端 以及 服务端,到这里,click house 的安装完成了,接下来开始进行部署! ``` root@liming-virtual-machine:~# cd /etc/clickhouse- clickhouse-client/ clickhouse-server/ ``` ### 部署 click house #### 目录结构查看 在服务端配置文件目录中有两个配置文件,两个目录,目录中存储的都是一些默认的配置,默认配置目录中是空的。 Config是服务端的配置项,user是有关使用的配置。稍后将进入到服务端配置文件中将开始配置数据。 ``` root@liming-virtual-machine:/etc/clickhouse-server# ll ../clickhouse-client/ total 20 drwxr-xr-x 2 root root 4096 12月 5 18:45 ./ drwxr-xr-x 131 root root 12288 12月 5 18:44 ../ -rw-r--r-- 1 root root 2273 2月 17 2022 config.xml root@liming-virtual-machine:/etc/clickhouse-server# ll ../clickhouse-server/ total 92 drwx------ 4 clickhouse clickhouse 4096 12月 5 18:44 ./ drwxr-xr-x 131 root root 12288 12月 5 18:44 ../ dr-x------ 2 clickhouse clickhouse 4096 12月 5 18:44 config.d/ -r-------- 1 clickhouse clickhouse 61417 2月 17 2022 config.xml dr-x------ 2 clickhouse clickhouse 4096 12月 5 18:45 users.d/ -r-------- 1 clickhouse clickhouse 6248 2月 17 2022 users.xml ``` #### 配置 ##### 配置允许访问IP 首先进入到配置文件中。 ``` vim ./config.xml ``` 然后我们配置文件中的 listen_host 节点,这里是代表能够访问clickHouse服务的主机IP,冒号代表通配的意思,当我们将第一行的 :: 注释解开之后,则代表任何主机都可以访问服务。 ``` <!-- <listen_host>::</listen_host> --> <!-- Same for hosts without support for IPv6: --> <!-- <listen_host>0.0.0.0</listen_host> --> <!-- Default values - try listen localhost on IPv4 and IPv6. --> <!-- <listen_host>::1</listen_host> <listen_host>127.0.0.1</listen_host> --> ``` 下面就是解开之后的样子 ``` <!-- Listen specified address. Use :: (wildcard IPv6 address), if you want to accept connections both with IPv4 and IPv6 from everywhere. Notes: If you open connections from wildcard address, make sure that at least one of the following measures applied: - server is protected by firewall and not accessible from untrusted networks; - all users are restricted to subset of network addresses (see users.xml); - all users have strong passwords, only secure (TLS) interfaces are accessible, or connections are only made via TLS interfaces. - users without password have readonly access. See also: https://www.shodan.io/search?query=clickhouse --> <listen_host>::</listen_host> ``` #### 配置数据目录以及日志目录 还是在 config.xml 中配置 ``` <!-- Path to data directory, with trailing slash. --> <path>存储 ck 数据的地址</path> ``` ``` <log>日志目录</log> ``` #### 为 click house 的配置文件,数据目录以及日志目录配置 777 权限 ``` chmod -R 777 /var/run/clickhouse-server chmod -R 777 /etc/clickhouse-server/ chmod -R 777 /etc/clickhouse-client/ chmod -R 777 /opt/software/clickHouse/ ``` ### 启动 click house ``` **root@liming-virtual-machine:/etc/clickhouse-server# clickhouse status # 查看ck状态 Now there is no clickhouse-server process. root@liming-virtual-machine:/etc/clickhouse-server# clickhouse start # 启动 ck chown -R clickhouse: '/var/run/clickhouse-server/' Will run su -s /bin/sh 'clickhouse' -c '/usr/bin/clickhouse-server --config-file /etc/clickhouse-server/config.xml --pid-file /var/run/clickhouse-server/clickhouse-server.pid --daemon' Waiting for server to start Waiting for server to start Server started root@liming-virtual-machine:/etc/clickhouse-server# clickhouse status # 再次查看状态 /var/run/clickhouse-server/clickhouse-server.pid file exists and contains pid = 23785. The process with pid = 23785 is running. ``` ### 进入 ck 客户端 ```shell clickhouse-client -m --host ck所在的主机IP --password 密码 # m 代表在交互式中可以使用换行。 # host 代表需要访问的click House 所在主机 # user 代表访问click House 时候使用的用户 # password 代表访问click House 时候使用的密码 ``` 下面是启动之后的测试结果 ``` **root@liming-virtual-machine:/etc/clickhouse-server# clickhouse-client -m --host 127.0.0.1 --password ClickHouse client version 22.2.2.1. Password for user (default): Connecting to 127.0.0.1:9000 as user default. Connected to ClickHouse server version 22.2.2 revision 54455. Warnings: * Effective user of the process (clickhouse) does not match the owner of the data (root). liming-virtual-machine :) show databases; SHOW DATABASES Query id: 7d5b89a9-e75c-4d28-89be-2bc2a7fb37c4 ┌─name───────────────┐ │ INFORMATION_SCHEMA │ │ default │ │ information_schema │ │ system │ └────────────────────┘ 4 rows in set. Elapsed: 0.003 sec. liming-virtual-machine :) ``` ------ ***操作记录*** 作者:[root](http://www.lingyuzhao.top/index.html?search=1 "root") 操作时间:2023-12-08 18:48:28 星期五 事件描述备注:保存/发布 [](如果不需要此记录可以手动删除,每次保存都会自动的追加记录)