iT邦幫忙

2022 iThome 鐵人賽

DAY 6
0
Software Development

ClickHouse:時序資料庫建置與運行系列 第 6

day6-如何從ClickHouse原始碼進行安裝

  • 分享至 

  • xImage
  •  

前言

在上一章節中,我們介紹了如何使用單一個二進位編譯好的執行檔與給非標準的環境使用預先編譯好的二進位執行檔來安裝ClickHouse資料庫。在本章節中,我們將會展示使用ClickHouse的專案原始碼進行編譯與安裝。

從原始碼來源安裝

為了要進行手動編譯與安裝ClickHouse資料庫,我們選擇了Ubuntu 18.04的Linux發行版本的作業系統來進行示範,目前支援的CPU架構如下:

  1. x86_64 (64位元的架構)
  2. AArch64 (ARM 64位元的架構)
  3. Power9 (目前官方標註為實驗階段的架構)

接著,準備一個乾淨的Ubuntu 18.04的作業系統,並執行下列的指令進行安裝下列這些需要的套件:

sudo apt-get update
sudo apt-get install git cmake ccache python3 ninja-build wget

從上面的指令得知,首先先更新來源的鏡像,接著進行套件的安裝。其中git套件主要是用來下載ClickHouse專案使用;cmakeccachepython3以及ninja-build等套件是用來編譯與設定ClickHouse會使用到的,執行上述的指令過程與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ sudo apt-get update
[sudo] password for peter:
Get:1 http://mirrors.digitalocean.com/ubuntu bionic InRelease [242 kB]
Hit:2 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease
Get:3 http://mirrors.digitalocean.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://mirrors.digitalocean.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:5 http://mirrors.digitalocean.com/ubuntu bionic-updates/main amd64 Packages [2729 kB]
Get:6 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:7 http://mirrors.digitalocean.com/ubuntu bionic-updates/main Translation-en [503 kB]
Get:8 http://mirrors.digitalocean.com/ubuntu bionic-updates/restricted amd64 Packages [913 kB]
Get:9 http://mirrors.digitalocean.com/ubuntu bionic-updates/restricted Translation-en [126 kB]
Get:10 http://mirrors.digitalocean.com/ubuntu bionic-updates/universe amd64 Packages [1842 kB]
Get:11 http://mirrors.digitalocean.com/ubuntu bionic-updates/universe Translation-en [399 kB]
Get:12 http://mirrors.digitalocean.com/ubuntu bionic-updates/multiverse amd64 Packages [24.9 kB]
Get:13 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [2389 kB]
Get:14 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [414 kB]
......
Fetched 12.4 MB in 4s (3119 kB/s)
Reading package lists... Done
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ sudo apt-get install git cmake ccache python3 ninja-build wget
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3 is already the newest version (3.6.7-1~18.04).
python3 set to manually installed.
wget is already the newest version (1.19.4-1ubuntu2.2).
wget set to manually installed.
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu cmake-data cpp cpp-7 gcc gcc-7 gcc-7-base
  libarchive13 libasan4 libatomic1 libbinutils libc-dev-bin libc6-dev libcc1-0 libcilkrts5 ......
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$

為了要避免安裝最新版本的clang會有相依性的問題發生,因 此需要先執行下列的指令,將ppa:ubuntu-toolchain-r/test加入到當前機器中,相關的執行指令如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ sudo apt-get install software-properties-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
software-properties-common is already the newest version (0.96.24.32.18).
software-properties-common set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
Get:1 http://mirrors.digitalocean.com/ubuntu bionic InRelease [242 kB]
Hit:2 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease
Hit:3 http://mirrors.digitalocean.com/ubuntu bionic-updates InRelease
Hit:4 http://mirrors.digitalocean.com/ubuntu bionic-backports InRelease
Hit:5 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic InRelease
Hit:7 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:6 https://apt.llvm.org/bionic llvm-toolchain-bionic-14 InRelease
Hit:8 https://apt.llvm.org/bionic llvm-toolchain-bionic-15 InRelease
Fetched 242 kB in 2s (102 kB/s)
Reading package lists... Done

在ClickHouse官方的建議下,使用下列的指令可以在Ubuntu 18.04上面安裝最新版本的clang,相關執行的安裝指令如下:

sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"

執行上述的指令過程與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
--2022-09-08 09:55:49--  https://apt.llvm.org/llvm.sh
Resolving apt.llvm.org (apt.llvm.org)... 151.101.42.49, 2a04:4e42:a::561
Connecting to apt.llvm.org (apt.llvm.org)|151.101.42.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4874 (4.8K) [application/octet-stream]
Saving to: ‘STDOUT’

-                          100%[========================================>]   4.76K  --.-KB/s    in 0s

2022-09-08 09:55:50 (74.6 MB/s) - written to stdout [4874/4874]

[sudo] password for peter:
+ CURRENT_LLVM_STABLE=15
+ BASE_URL=http://apt.llvm.org
+ needed_binaries=(lsb_release wget add-apt-repository gpg)
+ missing_binaries=()
+ for binary in "${needed_binaries[@]}"
+ which lsb_release
+ for binary in "${needed_binaries[@]}"
+ which wget
+ for binary in "${needed_binaries[@]}"
+ which add-apt-repository
+ for binary in "${needed_binaries[@]}"
+ which gpg
+ [[ 0 -gt 0 ]]
+ LLVM_VERSION=15
+ ALL=0
++ lsb_release -is
+ DISTRO=Ubuntu
++ lsb_release -sr
+ VERSION=18.04
+ UBUNTU_CODENAME=
+ CODENAME_FROM_ARGUMENTS=
+ source /etc/os-release
++ NAME=Ubuntu
++ VERSION='18.04.6 LTS (Bionic Beaver)'
++ ID=ubuntu
++ ID_LIKE=debian
++ PRETTY_NAME='Ubuntu 18.04.6 LTS'
++ VERSION_ID=18.04
++ HOME_URL=https://www.ubuntu.com/
++ SUPPORT_URL=https://help.ubuntu.com/
++ BUG_REPORT_URL=https://bugs.launchpad.net/ubuntu/
++ PRIVACY_POLICY_URL=https://www.ubuntu.com/legal/terms-and-policies/privacy-policy
++ VERSION_CODENAME=bionic
++ UBUNTU_CODENAME=bionic
+ DISTRO=ubuntu
+ case ${DISTRO} in
+ [[ -n bionic ]]
+ CODENAME=bionic
+ [[ -n bionic ]]
+ LINKNAME=-bionic
+ '[' 0 -ge 1 ']'
+ getopts :hm:n: arg
+ [[ 0 -ne 0 ]]
+ declare -A LLVM_VERSION_PATTERNS
+ LLVM_VERSION_PATTERNS[9]=-9
+ LLVM_VERSION_PATTERNS[10]=-10
+ LLVM_VERSION_PATTERNS[11]=-11
+ LLVM_VERSION_PATTERNS[12]=-12
+ LLVM_VERSION_PATTERNS[13]=-13
+ LLVM_VERSION_PATTERNS[14]=-14
+ LLVM_VERSION_PATTERNS[15]=-15
+ LLVM_VERSION_PATTERNS[16]=
+ '[' '!' _ ']'
+ LLVM_VERSION_STRING=-15
+ [[ -n bionic ]]
+ REPO_NAME='deb http://apt.llvm.org/bionic/  llvm-toolchain-bionic-15 main'
+ wget -q --method=HEAD http://apt.llvm.org/bionic
++ apt-key list
++ grep -i llvm
Warning: apt-key output should not be parsed (stdout is not a terminal)
+ [[ -z uid           [ unknown] Sylvestre Ledru - Debian LLVM packages <sylvestre@debian.org> ]]
+ add-apt-repository 'deb http://apt.llvm.org/bionic/  llvm-toolchain-bionic-15 main'
Get:1 http://mirrors.digitalocean.com/ubuntu bionic InRelease [242 kB]
Get:2 http://mirrors.digitalocean.com/ubuntu bionic-updates InRelease [88.7 kB]
Hit:3 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease
Get:4 http://mirrors.digitalocean.com/ubuntu bionic-backports InRelease [74.6 kB]
Hit:5 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic InRelease
Get:6 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:7 https://apt.llvm.org/bionic llvm-toolchain-bionic-14 InRelease
Get:8 https://apt.llvm.org/bionic llvm-toolchain-bionic-15 InRelease [6828 B]
Get:9 https://apt.llvm.org/bionic llvm-toolchain-bionic-15/main amd64 Packages [10.5 kB]
Fetched 511 kB in 2s (229 kB/s)
Reading package lists... Done
+ apt-get update
Get:1 http://mirrors.digitalocean.com/ubuntu bionic InRelease [242 kB]
Hit:2 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease
Hit:3 http://mirrors.digitalocean.com/ubuntu bionic-updates InRelease
Hit:4 http://mirrors.digitalocean.com/ubuntu bionic-backports InRelease
Hit:5 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:6 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic InRelease
Hit:7 https://apt.llvm.org/bionic llvm-toolchain-bionic-14 InRelease
Hit:8 https://apt.llvm.org/bionic llvm-toolchain-bionic-15 InRelease
Fetched 242 kB in 1s (179 kB/s)
Reading package lists... Done
+ PKG='clang-15 lldb-15 lld-15 clangd-15'
+ [[ 0 -eq 1 ]]
+ apt-get install -y clang-15 lldb-15 lld-15 clangd-15
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  liblldb-14
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  libclang-common-15-dev libclang-cpp15 libclang1-15 liblldb-15 libllvm15 llvm-15 llvm-15-dev
  llvm-15-linker-tools llvm-15-runtime llvm-15-tools python3-lldb-15
Suggested packages:
  clang-15-doc llvm-15-doc
The following packages will be REMOVED:
  lldb-14 python3-lldb-14
The following NEW packages will be installed:
  clang-15 clangd-15 libclang-common-15-dev libclang-cpp15 libclang1-15 liblldb-15 libllvm15 lld-15
  lldb-15 llvm-15 llvm-15-dev llvm-15-linker-tools llvm-15-runtime llvm-15-tools python3-lldb-15
0 upgraded, 15 newly installed, 2 to remove and 1 not upgraded.
Need to get 108 MB of archives.
After this operation, 666 MB of additional disk space will be used.
......
Processing triggers for libc-bin (2.27-3ubuntu1.6) ...

從上述的指令來看,執行llvm.sh的自動化安裝腳本將會安裝最新版的clang,舉例來說,現在目前最新版本是clang 15,則會安裝此版本,依據ClickHouse官方的建議,clang的版本最好是14版本以上,若要透過自動化安裝的腳本來指定安裝clang版本,則可以使用下列的指令來做到:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ wget -O llvm.sh https://apt.llvm.org/llvm.sh
--2022-09-08 10:02:46--  https://apt.llvm.org/llvm.sh
Resolving apt.llvm.org (apt.llvm.org)... 151.101.26.49, 2a04:4e42:a::561
Connecting to apt.llvm.org (apt.llvm.org)|151.101.26.49|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4874 (4.8K) [application/octet-stream]
Saving to: ‘llvm.sh’

llvm.sh                    100%[========================================>]   4.76K  --.-KB/s    in 0s

2022-09-08 10:02:47 (55.2 MB/s) - ‘llvm.sh’ saved [4874/4874]
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ chmod +x llvm.sh
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ sudo ./llvm.sh 14

從上面指令在執行之後,就會安裝clang 14版本了,相關的安裝clang過程的訊息與先前提到使用sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"來安裝最新版的clang是差不多的,當安裝好clang 14版與clang 15版(目前的最新版本)之後,可以用下面的指令來確定相關的clang執行指令已經存在:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ ls /usr/bin/ | grep clang
clang++-14
clang++-15
clang-14
clang-15
clang-cpp-14
clang-cpp-15
clangd-14
clangd-15

在安裝好clang之後,接著使用下列的指令設定在建置時候使用的clang,相關設定環境變數的指令如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ export CC=clang-14
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ export CXX=clang++-14

下載ClickHouse專案原始碼

接著,使用下列的指令將ClickHouse專案原始碼利用git指令從GitHub上下載回來,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ git clone --recursive https://github.com/ClickHouse/ClickHouse.git
Cloning into 'ClickHouse'...
remote: Enumerating objects: 983488, done.
remote: Counting objects: 100% (155/155), done.
remote: Compressing objects: 100% (101/101), done.
remote: Total 983488 (delta 80), reused 113 (delta 53), pack-reused 983333
Receiving objects: 100% (983488/983488), 800.73 MiB | 13.95 MiB/s, done.
Resolving deltas: 100% (778252/778252), done.
Checking out files: 100% (19645/19645), done.
Submodule 'contrib/AMQP-CPP' (https://github.com/ClickHouse/AMQP-CPP.git) registered for path 'contrib/AMQP-CPP'
......
 Submodule path 'contrib/zstd': checked out 'b944db0c451ba1bc6bbd8e201d5f88f9041bf1f9'

從上面的指令得知,使用--recursive的參數使用在下載ClickHouse專案原始碼上是因為ClickHouse專案用了sub module,因此要將所有的Git專案都下載回來才可以進行完整的編譯。

將ClickHouse原始碼專案整個下載回來之後,接著執行下列這些指令來進行ClickHouse專案的原始碼進行編譯,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ cd ClickHouse/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse$ mkdir build
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse$ cd build/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ cmake ..
CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
  CMake 3.15 or higher is required.  You are running version 3.10.2


-- Configuring incomplete, errors occurred!

從上面的訊息來看,有發生錯誤的訊息,原因是cmake的版本預設在Ubuntu 18.04上太舊,因此我們需要先更新cmake版本,相關的指令如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ sudo apt-get purge --auto-remove cmake
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
  cmake* cmake-data* libarchive13* libjsoncpp1* librhash0*
0 upgraded, 0 newly installed, 5 to remove and 1 not upgraded.
After this operation, 25.4 MB disk space will be freed.
Do you want to continue? [Y/n]
(Reading database ... 107909 files and directories currently installed.)
Removing cmake (3.10.2-1ubuntu2.18.04.2) ...
Removing cmake-data (3.10.2-1ubuntu2.18.04.2) ...
Removing libarchive13:amd64 (3.2.2-3.1ubuntu0.7) ...
Removing libjsoncpp1:amd64 (1.7.4-3) ...
Removing librhash0:amd64 (1.3.6-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.6) ...
(Reading database ... 105511 files and directories currently installed.)
Purging configuration files for cmake-data (3.10.2-1ubuntu2.18.04.2) ...
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc | sudo apt-key add -
--2022-09-08 10:48:07--  https://apt.kitware.com/keys/kitware-archive-latest.asc
Resolving apt.kitware.com (apt.kitware.com)... 66.194.253.25
Connecting to apt.kitware.com (apt.kitware.com)|66.194.253.25|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3963 (3.9K) [text/plain]
Saving to: ‘STDOUT’

-                          100%[========================================>]   3.87K  --.-KB/s    in 0s

2022-09-08 10:48:08 (95.6 MB/s) - written to stdout [3963/3963]

OK
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ sudo apt-add-repository -y 'deb https://apt.kitware.com/ubuntu/ bionic main'
Get:1 http://mirrors.digitalocean.com/ubuntu bionic InRelease [242 kB]
Hit:2 http://mirrors.digitalocean.com/ubuntu bionic-updates InRelease
Hit:3 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease
Hit:4 http://mirrors.digitalocean.com/ubuntu bionic-backports InRelease
Hit:5 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic InRelease
Get:7 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:6 https://apt.llvm.org/bionic llvm-toolchain-bionic-14 InRelease
Hit:8 https://apt.llvm.org/bionic llvm-toolchain-bionic-15 InRelease
Get:9 https://apt.kitware.com/ubuntu bionic InRelease [11.0 kB]
Get:10 https://apt.kitware.com/ubuntu bionic/main amd64 Packages [60.9 kB]
Fetched 403 kB in 2s (199 kB/s)
Reading package lists... Done
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ sudo apt-get install -y cmake
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  cmake-data
Suggested packages:
  cmake-doc
The following NEW packages will be installed:
  cmake cmake-data
0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded.
Need to get 13.6 MB of archives.
......
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$

從上面的指令可以看到,首先要先移除先前安裝舊版的cmake,接著引入Kitware GPG金鑰以及使用apt-add-repository指令設定其鏡像來源,都設定完成之後,接著可以執行apt-get指令來安裝cmake,安裝好cmake之後,可以執行cmake --version檢查目前安裝的版本。

cmake版本更新之後,接著再執行一次cmake ..指令,相關執行結果的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ cmake ..
-- The C compiler identification is Clang 14.0.6
-- The CXX compiler identification is Clang 14.0.6
-- The ASM compiler identification is Clang with GNU-like command-line
-- Found assembler: /usr/bin/clang-14
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/clang-14 - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++-14 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Cross-compiling for target:
-- Using compiler:
Ubuntu clang version 14.0.6-++20220622053019+f28c006a5895-1~exp1~20220622173056.159
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
-- Using linker: /usr/bin/ld.lld-14
-- Using archiver: /usr/bin/llvm-ar-14
-- Using ranlib: /usr/bin/llvm-ranlib-14
-- Using install-name-tool: /usr/bin/llvm-install-name-tool-14
-- Using objcopy: /usr/bin/llvm-objcopy-14
-- Using strip: /usr/bin/llvm-strip-14
-- Using ccache: /usr/bin/ccache (version 3.4.1)
-- Found Git: /usr/bin/git (found version "2.17.1")
-- HEAD's commit hash 90647cb0c5a4538898ee166a93abb4faa7bfc761
On branch master
Your branch is up to date with 'origin/master'.
......
cores)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/peter/ClickHouse/build

執行完成cmake ..指令之後,接著執行下列的指令,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ ninja
[0/2] Re-checking globbed directories...
[1109/10594] Building CXX object contrib/vectorscan-..._/vectorscan/src/rose/rose_build_role_aliasing.cpp.o 

由上述的執行指令輸出的訊息可以知道,因為會進行編譯,因此需要一段時間,編譯的時間長短取決於當前的機器硬體效能與規格,當編譯完成之後,會出現如下的訊息:

......
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$
[10594/10594] Linking CXX executable utils/keeper-bench/keeper-bench

完成編譯之後,可以執行下列的指令來列出當前的目錄有什麼相關的檔案,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ ls
CMakeCache.txt     CPackSourceConfig.cmake  build.ninja            contrib   ld.lld     src
CMakeFiles         CTestTestfile.cmake      cmake_install.cmake    gens      nohup.out  tests
CPackConfig.cmake  base                     compile_commands.json  includes  programs   utils

從上述的指令可以看到,有個叫做programs的目錄裡面有ClickHouse相關的執行檔,接著再使用ls programs/指令則可以看到此目錄底下的檔案與目錄,相關的執行指令所輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ ls programs/
CMakeFiles                      clickhouse-format                      client               lib
CTestTestfile.cmake             clickhouse-git-import                  cmake_install.cmake  library-bridge
bash-completion                 clickhouse-keeper                      compressor           local
benchmark                       clickhouse-keeper-converter            copier               obfuscator
clickhouse                      clickhouse-library-bridge              disks                odbc-bridge
clickhouse-benchmark            clickhouse-local                       extract-from-config  server
clickhouse-client               clickhouse-obfuscator                  format               static-files-disk-uploader
clickhouse-compressor           clickhouse-odbc-bridge                 git-import           su
clickhouse-copier               clickhouse-server                      install
clickhouse-disks                clickhouse-static-files-disk-uploader  keeper
clickhouse-extract-from-config  clickhouse-su                          keeper-converter

從上述的輸出訊息來看,clickhouse-serverclickhouse-client分別為ClickHouse伺服器與客戶端程式,但其實這兩個都是軟連結(symbolic link),都是指向到此目錄的clickhouse之二進位執行檔。

可以用ls -al programs/clickhouse-server programs/clickhouse-client指令來知道上述的所描述的事情,相關的執行指令所輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ ls -al programs/clickhouse-server programs/clickhouse-client
lrwxrwxrwx 1 peter peter 10 Sep  8 15:20 programs/clickhouse-client -> clickhouse
lrwxrwxrwx 1 peter peter 10 Sep  8 15:20 programs/clickhouse-server -> clickhouse
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$

再完成編譯ClickHouse原始碼編譯之後,接著需要建立下列這兩個目錄,這兩個目錄是給ClickHouse產生後設資料metadata,預設需要建立下列這兩個目錄:

/var/lib/clickhouse/data/default/
/var/lib/clickhouse/metadata/default/

這相關的設定可以在./programs/server/config.xml之設定檔中做設定,相關的設定檔的定義、解釋與操作會在後續的章節中提到,這邊就使用上述的預設即可,接著我們使用下列的指令將相關的目錄建立起來。

相關執行指令與輸入的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ sudo mkdir -p /var/lib/clickhouse/data/default/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ sudo mkdir -p /var/lib/clickhouse/metadata/default/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$

從上述的指令與輸出的訊息可以得知,mkdir指令的-p參數是將指定的新增目錄若路徑中有目錄沒有新增,則會一併新增。

接著透過chown指令將上述建立好的目錄的使用者改為peter,這樣才可以與當前相關目錄的使用者為同一個擁有者,以避免啟動資料庫伺服器的時候會有權限的問題發生,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ sudo chown -R peter:peter /var/lib/clickhouse/data/default/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ sudo chown -R peter:peter /var/lib/clickhouse/metadata/default/

新增好之後,接著切換到programs目錄底下,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build$ cd programs/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$

從上面的指令可以得知,使用了cd指令切換到programs的目錄,接著我們可以執行下列的指令將ClickHouse資料庫伺服器給運行起來,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$ ./clickhouse-server start
Processing configuration file 'config.xml'.
There is no file 'config.xml', will use embedded config.
Couldn't save preprocessed config to ./preprocessed_configs/config.xml: Access to file denied: ./preprocessed_configs/config.xml
2022.09.09 04:16:07.479388 [ 9909 ] {} <Information> SentryWriter: Sending crash reports is disabled
2022.09.09 04:16:07.496167 [ 9909 ] {} <Trace> Pipe: Pipe capacity is 1.00 MiB
2022.09.09 04:16:07.752028 [ 9909 ] {} <Information> : Starting ClickHouse 22.9.1.1 with revision 54466, build id: FED908D9B3D00B47030CFC1288CE7D2147936B41, PID 9909
2022.09.09 04:16:07.752135 [ 9909 ] {} <Information> Application: starting up
2022.09.09 04:16:07.752182 [ 9909 ] {} <Information> Application: OS name: Linux, version: 4.15.0-189-generic, architecture: x86_64
2022.09.09 04:16:07.767176 [ 9909 ] {} <Warning> Context: Linux is not using a fast TSC clock source. Performance can be degraded. Check 
......
2022.09.09 04:16:23.330693 [ 9961 ] {} <Debug> DNSResolver: Updating DNS cache
2022.09.09 04:16:23.330797 [ 9961 ] {} <Debug> DNSResolver: Updated DNS cache
2022.09.09 04:16:38.331190 [ 9966 ] {} <Debug> DNSResolver: Updating DNS cache
2022.09.09 04:16:38.331280 [ 9966 ] {} <Debug> DNSResolver: Updated DNS cache

從上述的指令可以知道,這樣就會把ClickHouse資料庫跑在foreground前景的終端機上面,若要停止則可以按下「ctrl」與「c」的組合鍵進行停止,按下去之後的輸出訊息如下:

^C2022.09.09 04:17:55.616137 [ 9910 ] {} <Trace> BaseDaemon: Received signal 2
2022.09.09 04:17:55.616203 [ 9910 ] {} <Information> Application: Received termination signal (Interrupt)
2022.09.09 04:17:55.616224 [ 9909 ] {} <Debug> Application: Received termination signal.
2022.09.09 04:17:55.616247 [ 9909 ] {} <Debug> Application: Waiting for current connections to close.
2022.09.09 04:17:57.020168 [ 9909 ] {} <Information> Application: Closed all listening sockets.
2022.09.09 04:17:57.020239 [ 9909 ] {} <Information> Application: Closed connections.
2022.09.09 04:17:57.020496 [ 9909 ] {} <Information> Application: Shutting down storages.
2022.09.09 04:17:57.020518 [ 9909 ] {} <Information> Context: Shutdown disk default
2022.09.09 04:17:57.022568 [ 9909 ] {} <Trace> BackgroundSchedulePool/BgSchPool: Waiting for threads to finish.
2022.09.09 04:17:57.037914 [ 9909 ] {} <Debug> MemoryTracker: Peak memory usage (for query): 68.16 KiB.
2022.09.09 04:17:57.038040 [ 9909 ] {} <Debug> Application: Shut down storages.
2022.09.09 04:17:57.995929 [ 9909 ] {} <Debug> Application: Destroyed global context.
2022.09.09 04:17:57.997527 [ 9909 ] {} <Information> Application: shutting down
2022.09.09 04:17:57.997583 [ 9909 ] {} <Debug> Application: Uninitializing subsystem: Logging Subsystem
2022.09.09 04:17:57.997629 [ 9910 ] {} <Trace> BaseDaemon: Received signal -2
2022.09.09 04:17:57.997671 [ 9910 ] {} <Information> BaseDaemon: Stop SignalListener thread

若我們要將ClickHouse資料庫伺服器運行在背景的話,則我們可以使用nohup指令並搭配&來讓上述啟動資料庫的指令跑在背景中,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$ nohup ./clickhouse-server start &
[1] 10108
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$ nohup: ignoring input and appending output to 'nohup.out'

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$

從上述的執行指令輸出的訊息可以得知,當輸出nohup: ignoring input and appending output to 'nohup.out'訊息時候會卡一個游標,需要按下「enter」鍵才會回到下一個互動式shell的行,而自動產生nohup.out的檔案裡面則是會有目前ClickHouse資料庫伺服器所有運行中所產生的日誌log,而上述輸出的訊息中的[1] 10108則是表示目前運行的process程序id為10108,而前面的[1]為背景工作job編號為1

接著我們可以利用下面的ps指令並搭配aux參數來確認ClickHouse資料庫已經正確的運行在背景,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$ ps aux | grep clickhouse
peter    10108  0.0  1.0 475360 85968 pts/0    S    04:20   0:00 clickhouse-watchdog start
peter    10109  0.7  6.4 4432448 523148 pts/0  Sl   04:20   0:01 ./clickhouse-server start
peter    10299  0.0  0.0  14860  1056 pts/0    S+   04:23   0:00 grep --color=auto clickhouse
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$

從上述的輸出的指令可以得知,執行ps aux指令可以得到該prcoess程序的使用者名稱、process程序id以及執行的指令,若我們要只列出有關於clickhouse關鍵字的話,則再搭配管線將前面輸出的內容轉給管線後面的grep指令當作輸入,找到含有clickhouse關鍵字的所有行數。

接著我們可以透過下列的tail -F nohup.out指令可以查看目前ClickHouse資料庫的日誌,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$ tail -F nohup.out
2022.09.09 04:34:27.255153 [ 10224 ] {} <Debug> DNSResolver: Updated DNS cache
2022.09.09 04:34:42.255473 [ 10227 ] {} <Debug> DNSResolver: Updating DNS cache
2022.09.09 04:34:42.255556 [ 10227 ] {} <Debug> DNSResolver: Updated DNS cache
2022.09.09 04:34:43.000901 [ 10290 ] {} <Trace> AsynchronousMetrics: Update heavy metrics. Update period 1 sec. Update heavy metrics period 120 sec.  Heavy metrics calculation elapsed: 1.9459e-05 sec.
2022.09.09 04:34:57.255822 [ 10220 ] {} <Debug> DNSResolver: Updating DNS cache
2022.09.09 04:34:57.255903 [ 10220 ] {} <Debug> DNSResolver: Updated DNS cache
2022.09.09 04:35:11.015085 [ 10111 ] {} <Debug> TCPHandler: Done processing connection.
2022.09.09 04:35:11.015178 [ 10111 ] {} <Debug> TCP-Session: 8481f855-7609-43c8-bc2a-b10edee959ba Destroying unnamed session
2022.09.09 04:35:12.256284 [ 10204 ] {} <Debug> DNSResolver: Updating DNS cache
2022.09.09 04:35:12.256405 [ 10204 ] {} <Debug> DNSResolver: Updated DNS cache
2022.09.09 04:35:27.256812 [ 10215 ] {} <Debug> DNSResolver: Updating DNS cache
2022.09.09 04:35:27.256923 [ 10215 ] {} <Debug> DNSResolver: Updated DNS cache
^C
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$

從上述的指令可以得知,該指令會將nohup.out檔案前十行印出並持續列出目前最新日誌中的內容,按下「ctrl」與「c」按鍵之後,則可以跳出此指令的模式,

接著我們可以使用clickhouse-client之客戶端指令連上該ClickHouse資料庫,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$ ./clickhouse-client
ClickHouse client version 22.9.1.1.
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 22.9.1 revision 54460.

Warnings:
 * Linux is not using a fast TSC clock source. Performance can be degraded. Check /sys/devices/system/clocksource/clocksource0/current_clocksource

ubuntu-s-4vcpu-8gb-amd-sgp1-01 :) exit
Bye.
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$

從上述的指令可以得知,已經使用了clickhouse-client成功地連上ClickHouse資料庫了,接著可以輸入exit來離開目前互動式ClickHouse客戶端shell介面。

若要停止上述所執行的ClickHouse資料庫,則我們可以使用killall指令來完成,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$ killall clickhouse-server
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$
[1]+  Done                    nohup ./clickhouse-server start

透過上述的方式來啟動ClickHouse資料庫伺服器,若是該機器重新啟動之後,該資料庫伺服器就會不見,需要自行再次啟動,若要設定成讓ClickHouse資料庫可以自動啟動,則我們可以使用下列的方式來做到,相關執行的指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/ClickHouse/build/programs$ crontab -e
no crontab for peter - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 2

從上述的指令可以得知,我們使用了cron排程指令來完成ClickHouse資料庫自動啟動的方法,因為是第一次執行,所以需要選擇文字編輯器來編輯排程的設定檔案,這邊選擇的是選項2使用vim指令來編輯排程的設定。

選擇完文字編輯器之後,接著會使用vim指令將排程設定檔開啟,進入到下面的輸出訊息:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

接著我們可以按下「i」鍵進入vim文字編輯器輸入的模式,並移動到最後一行並加入@reboot cd /home/peter/ClickHouse/build/programs && ./clickhouse-server start此行,接著就會變成如下的結果:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
@reboot cd /home/peter/ClickHouse/build/programs && ./clickhouse-server start > ./server.log 2>&1

完成上述的排程設定之後,ClickHouse資料庫伺服器就會在該機器開機或是重新啟動的時候自動啟動了,完成好排程設定的編輯之後,可以先使用「esc」鍵,接著分別輸入:wq等鍵就可以離開vim文字編輯器了。

其中,./clickhouse-server start > ./server.log 2>&1指令指的是將運行ClickHouse資料庫的紀錄存放到/home/peter/ClickHouse/build/programs的目錄底下,包含STDOUT與STDERR的訊息都存放到server.log檔案中。

離開編輯器之後,相關的輸出訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ crontab -e
crontab: installing new crontab
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$

接著可以試著使用sudo reboot或是sudo systemctl reboot將機器重新開機,再重新進入機器之後,使用下列指令則會發現ClickHouse資料庫在機器啟動時候透過cron排程機制自己啟動了,相關的執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ ps aux | grep clickhouse
peter     1089  0.0  0.0   4636   780 ?        Ss   04:50   0:00 /bin/sh -c cd /home/peter/ClickHouse/build/programs && ./clickhouse-server start > ./server.log 2>&1
peter     1112  0.0  0.9 479456 81060 ?        S    04:50   0:00 clickhouse-watchdog start
peter     1237  0.3  6.4 4430400 524720 ?      Sl   04:50   0:02 ./clickhouse-server start
peter     1709  0.0  0.0  14860  1092 pts/0    S+   05:03   0:00 grep --color=auto clickhouse

在任意的Linux為基礎的發行版本建置ClickHouse

要在任意一個Linux為基礎的發行版本作業系統進行編譯ClickHouse專案原始碼,則需要有下列的套件需求:

  • Git指令可以使用,這是用來下載ClickHouse專案,並指定下載相關的版本使用,並不是用來編譯與建置ClickHouse專案的。
  • CMake的版本需要在3.15以上。
  • 需要Ninja的指令且版本沒有指定,但是應使用最新的版本。
  • C++編譯器,需要clang版本在14或以上
  • Linker,需要有lld(The LLVM Linker)的支援。

如果上述的需求都已經準備好的話,則可以用先前所提到建置步驟來進行編譯ClickHouse專案原始碼。

結論

從本章節中可以知道,編譯ClickHouse原始碼專案不是一個簡單的事情,因此ClickHouse官方建議,我們不應該自己編譯ClickHouse原始碼來進行安裝,應該要多使用已經預先編譯與建置好的ClickHouse二進位原始執行檔,這些執行檔可以帶著走,並任意的執行在Linux為基礎發行版本的作業系統。

上述這些預先編譯與建置好的ClickHouse的二進位執行檔或是套件,都是用ClickHouse所釋出的穩定版(stable)或是長期支援板(LTS, long term support),筆者建議也是如此,除非是要進行開發,需要將ClickHouse的原始程式碼進行修改,才需要自行編譯,這樣一來才可以測試自己所修改的原始程式碼是否有照著預期的行為執行。

在下一章節中,筆者將會介紹如何在Linux發行版本的作業系統上進行跨編譯ClickHouse的專案原始碼。

參考資料


上一篇
day5-介紹ClickHouse安裝方法(下)
下一篇
day7-如何從ClickHouse專案原始碼進行跨編譯
系列文
ClickHouse:時序資料庫建置與運行30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言