【入门篇】ClickHouse 的安装与配置

文章目录

ClickHouse是一个用于联机分析(OLAP)的列式数据库管理系统(DBMS)。

0. 前言

安装ClickHouse:首先,你需要选择适合你操作系统的安装方式。ClickHouse提供了各种安装方法,包括二进制包安装、Docker容器安装和源代码编译安装。你可以根据自己的需求选择其中一种方式进行安装。

ClickHouse的安装

在 Unix-like 系统(包括 Linux 和 Mac OS)上安装 ClickHouse 的推荐方式是使用预编译的二进制包。以下是在 Ubuntu 上安装 ClickHouse 的步骤:

1. 添加 ClickHouse 的仓库

shell 复制代码
sudo apt-get install apt-transport-https ca-certificates dirmngr
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E0C56BD4

echo "deb https://repo.clickhouse.tech/deb/stable/ main/" | sudo tee \
    /etc/apt/sources.list.d/clickhouse.list
sudo apt-get update

2. 安装 ClickHouse

shell 复制代码
sudo apt-get install -y clickhouse-server clickhouse-client

3. 启动 ClickHouse 服务器

shell 复制代码
sudo service clickhouse-server start

4. 使用 ClickHouse 客户端

shell 复制代码
clickhouse-client

如果你在 Windows 上,可以使用 Docker。首先安装 Docker,然后运行以下命令获取 ClickHouse 镜像并运行:

shell 复制代码
docker run -d --name some-clickhouse-server --ulimit nofile=262144:262144 yandex/clickhouse-server

然后你可以使用 ClickHouse 客户端连接服务器:

shell 复制代码
docker run -it --rm --link some-clickhouse-server:clickhouse-server yandex/clickhouse-client --host clickhouse-server

ClickHouse的配置

ClickHouse 的配置文件通常在 /etc/clickhouse-server/ 目录下。

主要的配置文件是 config.xml,这里可以设置服务器的各种行为,比如内存限制、连接限制、认证、日志记录等。

你也可以在 users.xml 文件中设置用户级别的配置,比如访问权限、查询限制等。

使用 clickhouse-client 可以直接连接到服务器,如果需要远程连接,你可能需要在 users.xml 中添加相应的网络访问权限。

1. 详细安装教程

1.1. 系统要求

ClickHouse可以在任何具有x86_64,AArch64或PowerPC64LE CPU架构的Linux,FreeBSD或Mac OS X上运行。

官方预构建的二进制文件通常针对x86_64进行编译,并利用SSE 4.2指令集,因此,除非另有说明,支持它的CPU使用将成为额外的系统需求。下面是检查当前CPU是否支持SSE 4.2的命令:

bash 复制代码
$ grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 supported" || echo "SSE 4.2 not supported"

要在不支持SSE 4.2AArch64PowerPC64LE架构的处理器上运行ClickHouse,您应该通过适当的配置调整从源代码构建ClickHouse

1.1. 可用安装包 {#install-from-deb-packages}

1.1.1. DEB安装包

建议使用Debian或Ubuntu的官方预编译deb软件包。运行以下命令来安装包:

bash 复制代码
sudo apt-get install -y apt-transport-https ca-certificates dirmngr
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 8919F6BD2B48D754

echo "deb https://packages.clickhouse.com/deb stable main" | sudo tee \
    /etc/apt/sources.list.d/clickhouse.list
sudo apt-get update

sudo apt-get install -y clickhouse-server clickhouse-client

sudo service clickhouse-server start
clickhouse-client # or "clickhouse-client --password" if you've set up a password.

Deprecated Method for installing deb-packages

bash 复制代码
sudo apt-get install apt-transport-https ca-certificates dirmngr
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E0C56BD4

echo "deb https://repo.clickhouse.com/deb/stable/ main/" | sudo tee \
    /etc/apt/sources.list.d/clickhouse.list
sudo apt-get update

sudo apt-get install -y clickhouse-server clickhouse-client

sudo service clickhouse-server start
clickhouse-client # or "clickhouse-client --password" if you set up a password.

如果您想使用最新的版本,请用testing替代stable(我们只推荐您用于测试环境)。

你也可以从这里手动下载安装包:下载

安装包列表:

  • clickhouse-common-static --- ClickHouse编译的二进制文件。
  • clickhouse-server --- 创建clickhouse-server软连接,并安装默认配置服务
  • clickhouse-client --- 创建clickhouse-client客户端工具软连接,并安装客户端配置文件。
  • clickhouse-common-static-dbg --- 带有调试信息的ClickHouse二进制文件。

1.1.1. RPM安装包 {#from-rpm-packages}

推荐使用CentOS、RedHat和所有其他基于rpm的Linux发行版的官方预编译rpm包。

首先,您需要添加官方存储库:

bash 复制代码
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://packages.clickhouse.com/rpm/clickhouse.repo
sudo yum install -y clickhouse-server clickhouse-client

sudo /etc/init.d/clickhouse-server start
clickhouse-client # or "clickhouse-client --password" if you set up a password.

For systems with zypper package manager (openSUSE, SLES):

bash 复制代码
sudo zypper addrepo -r https://packages.clickhouse.com/rpm/clickhouse.repo -g
sudo zypper --gpg-auto-import-keys refresh clickhouse-stable
sudo zypper install -y clickhouse-server clickhouse-client

sudo /etc/init.d/clickhouse-server start
clickhouse-client # or "clickhouse-client --password" if you set up a password.

Deprecated Method for installing rpm-packages

bash 复制代码
sudo yum install yum-utils
sudo rpm --import https://repo.clickhouse.com/CLICKHOUSE-KEY.GPG
sudo yum-config-manager --add-repo https://repo.clickhouse.com/rpm/clickhouse.repo
sudo yum install clickhouse-server clickhouse-client

sudo /etc/init.d/clickhouse-server start
clickhouse-client # or "clickhouse-client --password" if you set up a password.

如果您想使用最新的版本,请用testing替代stable(我们只推荐您用于测试环境)。prestable有时也可用。

然后运行命令安装:

bash 复制代码
sudo yum install clickhouse-server clickhouse-client

你也可以从这里手动下载安装包:下载

1.1.1.Tgz安装包 {#from-tgz-archives}

如果您的操作系统不支持安装debrpm包,建议使用官方预编译的tgz软件包。

所需的版本可以通过curlwget从存储库https://packages.clickhouse.com/tgz/下载。

下载后解压缩下载资源文件并使用安装脚本进行安装。以下是一个最新稳定版本的安装示例:

bash 复制代码
LATEST_VERSION=$(curl -s https://packages.clickhouse.com/tgz/stable/ | \
    grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sort -V -r | head -n 1)
export LATEST_VERSION

case $(uname -m) in
  x86_64) ARCH=amd64 ;;
  aarch64) ARCH=arm64 ;;
  *) echo "Unknown architecture $(uname -m)"; exit 1 ;;
esac

for PKG in clickhouse-common-static clickhouse-common-static-dbg clickhouse-server clickhouse-client
do
  curl -fO "https://packages.clickhouse.com/tgz/stable/$PKG-$LATEST_VERSION-${ARCH}.tgz" \
    || curl -fO "https://packages.clickhouse.com/tgz/stable/$PKG-$LATEST_VERSION.tgz"
done

tar -xzvf "clickhouse-common-static-$LATEST_VERSION-${ARCH}.tgz" \
  || tar -xzvf "clickhouse-common-static-$LATEST_VERSION.tgz"
sudo "clickhouse-common-static-$LATEST_VERSION/install/doinst.sh"

tar -xzvf "clickhouse-common-static-dbg-$LATEST_VERSION-${ARCH}.tgz" \
  || tar -xzvf "clickhouse-common-static-dbg-$LATEST_VERSION.tgz"
sudo "clickhouse-common-static-dbg-$LATEST_VERSION/install/doinst.sh"

tar -xzvf "clickhouse-server-$LATEST_VERSION-${ARCH}.tgz" \
  || tar -xzvf "clickhouse-server-$LATEST_VERSION.tgz"
sudo "clickhouse-server-$LATEST_VERSION/install/doinst.sh" configure
sudo /etc/init.d/clickhouse-server start

tar -xzvf "clickhouse-client-$LATEST_VERSION-${ARCH}.tgz" \
  || tar -xzvf "clickhouse-client-$LATEST_VERSION.tgz"
sudo "clickhouse-client-$LATEST_VERSION/install/doinst.sh"

Deprecated Method for installing tgz archives

bash 复制代码
export LATEST_VERSION=$(curl -s https://repo.clickhouse.com/tgz/stable/ | \
    grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sort -V -r | head -n 1)
curl -O https://repo.clickhouse.com/tgz/stable/clickhouse-common-static-$LATEST_VERSION.tgz
curl -O https://repo.clickhouse.com/tgz/stable/clickhouse-common-static-dbg-$LATEST_VERSION.tgz
curl -O https://repo.clickhouse.com/tgz/stable/clickhouse-server-$LATEST_VERSION.tgz
curl -O https://repo.clickhouse.com/tgz/stable/clickhouse-client-$LATEST_VERSION.tgz

tar -xzvf clickhouse-common-static-$LATEST_VERSION.tgz
sudo clickhouse-common-static-$LATEST_VERSION/install/doinst.sh

tar -xzvf clickhouse-common-static-dbg-$LATEST_VERSION.tgz
sudo clickhouse-common-static-dbg-$LATEST_VERSION/install/doinst.sh

tar -xzvf clickhouse-server-$LATEST_VERSION.tgz
sudo clickhouse-server-$LATEST_VERSION/install/doinst.sh
sudo /etc/init.d/clickhouse-server start

tar -xzvf clickhouse-client-$LATEST_VERSION.tgz
sudo clickhouse-client-$LATEST_VERSION/install/doinst.sh

对于生产环境,建议使用最新的stable版本。你可以在GitHub页面https://github.com/ClickHouse/ClickHouse/tags找到它,它以后缀`-stable`标志。

1.1.1.Docker安装包 {#from-docker-image}

要在Docker中运行ClickHouse,请遵循Docker Hub上的指南。它是官方的deb安装包。

1.1.1. 其他环境安装包 {#from-other}

对于非linux操作系统和Arch64 CPU架构,ClickHouse将会以master分支的最新提交的进行编译提供(它将会有几小时的延迟)。

  • macOS --- curl -O 'https://builds.clickhouse.com/master/macos/clickhouse' && chmod a+x ./clickhouse
  • FreeBSD --- curl -O 'https://builds.clickhouse.com/master/freebsd/clickhouse' && chmod a+x ./clickhouse
  • AArch64 --- curl -O 'https://builds.clickhouse.com/master/aarch64/clickhouse' && chmod a+x ./clickhouse

下载后,您可以使用clickhouse client连接服务,或者使用clickhouse local模式处理数据,不过您必须要额外在GitHub下载serverusers配置文件。

不建议在生产环境中使用这些构建版本,因为它们没有经过充分的测试,但是您可以自行承担这样做的风险。它们只是ClickHouse功能的一个部分。

1.1.1. 使用源码安装 {#from-sources}

要手动编译ClickHouse, 请遵循LinuxMac OS X说明。

您可以编译并安装它们,也可以使用不安装包的程序。通过手动构建,您可以禁用SSE 4.2AArch64 cpu

复制代码
  Client: programs/clickhouse-client
  Server: programs/clickhouse-server

您需要创建一个数据和元数据文件夹,并为所需的用户chown授权。它们的路径可以在服务器配置(src/programs/server/config.xml)中改变,默认情况下它们是:

复制代码
  /opt/clickhouse/data/default/
  /opt/clickhouse/metadata/default/

在Gentoo上,你可以使用emerge clickhouse从源代码安装ClickHouse。

1.1. 启动 {#qi-dong}

如果没有service,可以运行如下命令在后台启动服务:

bash 复制代码
$ sudo /etc/init.d/clickhouse-server start

日志文件将输出在/var/log/clickhouse-server/文件夹。

如果服务器没有启动,检查/etc/clickhouse-server/config.xml中的配置。

您也可以手动从控制台启动服务器:

bash 复制代码
$ clickhouse-server --config-file=/etc/clickhouse-server/config.xml

在这种情况下,日志将被打印到控制台,这在开发过程中很方便。

如果配置文件在当前目录中,则不需要指定------config-file参数。默认情况下,它的路径为./config.xml

ClickHouse支持访问限制设置。它们位于users.xml文件(与config.xml同级目录)。

默认情况下,允许default用户从任何地方访问,不需要密码。可查看user/default/networks

更多信息,请参见Configuration Files

启动服务后,您可以使用命令行客户端连接到它:

bash 复制代码
$ clickhouse-client

默认情况下,使用default用户并不携带密码连接到localhost:9000。还可以使用--host参数连接到指定服务器。

终端必须使用UTF-8编码。

更多信息,请参阅Command-line client

示例:

复制代码
$ ./clickhouse-client
ClickHouse client version 0.0.18749.
Connecting to localhost:9000.
Connected to ClickHouse server version 0.0.18749.

:) SELECT 1

SELECT 1

┌─1─┐
│ 1 │
└───┘

1 rows in set. Elapsed: 0.003 sec.

:)

恭喜,系统已经工作了!

为了继续进行实验,你可以尝试下载测试数据集或查看教程

相关推荐
l1t1 小时前
PostgreSQL pg_clickhouse插件的安装和使用
数据库·clickhouse·postgresql·插件
honder试试1 天前
Springboot实现Clickhouse连接池的配置和接口查询
spring boot·后端·clickhouse
Mr_wilson_liu1 天前
通过DBeaver22.0.5 连接数据库ck(clickhouse)、pg(postgres)
数据库·clickhouse
波波仔863 天前
clickhouse表存储引擎
clickhouse·表存储引擎
波波仔863 天前
clickhouse存储和分区
clickhouse·排序·分区
波波仔863 天前
clickhouse insert与update区别
clickhouse·insert·update
波波仔863 天前
clickhouse简介
数据库·clickhouse
深色風信子3 天前
ClickHouse 快速入门
clickhouse·列式存储
波波仔863 天前
行存储与列存储的区别
数据库·clickhouse·行存储·列储存
吃喝不愁霸王餐APP开发者3 天前
霸王餐用户行为埋点:Kafka Connect+ClickHouse实时OLAP分析
分布式·clickhouse·kafka