本文基于 CentOS7 (3.10.0-693.el7.x86_64 GNU/Linux)

本文基于 ClickHouse-v23.3.2.37-lts

准备工作

  • 需要 cmake 3.20 or newer
  • 需要 clang 15.0 or newer
  • 需要 lld 15 or newer
  • 需要 Ninja
  • 需要 Yasm
  • 需要 Gawk

安装必要工具

# add extra repos
yum -y install epel-release centos-release-scl centos-release-scl-rh

# installing needed libs
yum -y install readline-devel unixODBC-devel openssl-devel libicu-devel libtool-ltdl-devel openssl-devel scl-utils

# installing build tools
yum -y install git devtoolset-9 tar wget rh-python36
yum -y install http://repo.okay.com.mx/centos/7/x86_64/release/okay-release-1-5.el7.noarch.rpm
yum -y install ninja-build yasm gawk
yum -y update binutils

安装 cmake

安装步骤如下:

cd /opt
wget https://github.com/Kitware/CMake/releases/download/v3.26.3/cmake-3.26.3-linux-x86_64.tar.gz
tar xzf cmake-3.26.3-linux-x86_64.tar.gz
ln -svf /opt/cmake-3.26.3-linux-x86_64/bin/* /usr/bin/
# 查看版本,验证是否安装成功
cmake --version

安装 clang 与 lld

先下载好安装包

# 你想要安装的目录
export basedir=/opt

cd "$basedir"
# 此部分最好科学上网,否则速度感人
# git config --global http.proxy socks5://127.0.0.1:7890
# git config --global https.proxy socks5://127.0.0.1:7890
git clone https://github.com/llvm/llvm-project.git
git tag
git checkout -b remotes/origin/release/16.x
git checkout llvmorg-16.0.3

执行编译安装 clang 与 lld 步骤

export basedir=/opt
cd "$basedir"

export THREADS=$(grep -c ^processor /proc/cpuinfo)
mkdir $basedir/llvm-16.0.3 && cd $basedir/llvm-16.0.3
scl enable devtoolset-9 rh-python36 'cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;lld" -G "Unix Makefiles" ../llvm-project/llvm'
scl enable devtoolset-9 rh-python36 'make -j $THREADS && make install'

注意:lld 并不是必须编译的,如果本地系统的 ld 版本过低,可能需要使用 llvm 的 lld 来代替系统的 ld 以免编译时出现找不到相关库的问题

也可以使用 ninja 来进行编译,速度会比 make 要快。

export basedir=/opt
cd "$basedir"

export THREADS=$(grep -c ^processor /proc/cpuinfo)
mkdir $basedir/llvm-16.0.3 && cd $basedir/llvm-16.0.3
scl enable devtoolset-9 rh-python36 'cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;lld" -G Ninja ../llvm-project/llvm'
scl enable devtoolset-9 rh-python36 'ninja -j $THREADS && ninja install'

此过程会耗费大量时间,请耐心等待。

编译 Clickhouse

下载 ClickHouse 源码:

cd /opt
git clone https://github.com/ClickHouse/ClickHouse.git

开始编译操作

export codedir=/opt/ClickHouse
git tag
# 好像切换了分支没生效,编译的版本为23.3.2.1
git checkout -b v23.3.2.37-lts
git submodule sync
# 此部分最好科学上网,否则速度感人
# git config --global http.proxy socks5://127.0.0.1:7890
# git config --global https.proxy socks5://127.0.0.1:7890
git submodule update --init --recursive

mkdir -p ${codedir}/build
cd ${codedir}/build

scl enable devtoolset-9 rh-python36 'CC=/usr/local/bin/clang CXX=/usr/local/bin/clang++ cmake -DCMAKE_BUILD_TYPE:STRING=Release -DCOMPILER_CACHE=disabled -DCMAKE_MAKE_PROGRAM:FILEPATH=/usr/bin/ninja ..'

# THREADS根据CPU个数自行进行设置
export THREADS=$(grep -c ^processor /proc/cpuinfo)
# 编译 clickhouse
ninja -j $THREADS clickhouse
# 编译 odbc 桥接
ninja -j $THREADS clickhouse-odbc-bridge
# 编译 lib 桥接
ninja -j $THREADS clickhouse-library-bridge

其中 CC 为 clang 的安装目录,CXX 为 clang++ 的安装目录,需要根据自己的实际情况进行替换。

编译完毕之后,可执行文件在 /opt/ClickHouse/build/programs 下,可执行文件为 clickhouse。可以执行如下命令来查看

./clickhouse
Use one of the following commands:
clickhouse local [args]
clickhouse client [args]
clickhouse benchmark [args]
clickhouse server [args]
clickhouse extract-from-config [args]
clickhouse compressor [args]
clickhouse format [args]
clickhouse copier [args]
clickhouse obfuscator [args]
clickhouse git-import [args]
clickhouse keeper [args]
clickhouse keeper-converter [args]
clickhouse install [args]
clickhouse start [args]
clickhouse stop [args]
clickhouse status [args]
clickhouse restart [args]
clickhouse static-files-disk-uploader [args]
clickhouse su [args]
clickhouse hash-binary [args]
clickhouse disks [args]

启动 server

使用 ./clickhouse server 来启动服务

Processing configuration file 'config.xml'.
There is no file 'config.xml', will use embedded config.
2023.05.10 15:30:34.434959 [ 4253 ] {} <Information> SentryWriter: Sending crash reports is disabled
2023.05.10 15:30:34.446996 [ 4253 ] {} <Trace> Pipe: Pipe capacity is 1.00 MiB
2023.05.10 15:30:34.813067 [ 4253 ] {} <Information> Application: Starting ClickHouse 23.3.2.1 (revision: 54472, git hash: 1b144bcd101ddf23466ba67e4fa0fd27afb9c060, build id: <unknown>), PID 4253
2023.05.10 15:30:34.813124 [ 4253 ] {} <Information> Application: starting up

启动 client

使用 ./clickhouse client 来连接 server

ClickHouse client version 23.3.2.1.
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 23.3.2 revision 54462.

Warnings:
* Linux transparent hugepages are set to "always". Check /sys/kernel/mm/transparent_hugepage/enabled

localhost :)

遇到的问题

ld: cannot find -lxxx

缺少编译时库的问题

本次编译解决方案为重新编译 LLVM 的 lld 项目来替代系统原有的 ld。具体编译与安装参见 这里