测试环境:Redhat 7 源码:clickhouse-master(2020.09.27) # 一、系统 ```bash [root@bogon ~]# adduser clickhouse [root@bogon ~]# passwd clickhouse [root@bogon ~]# vim /etc/sudoers # 添加行 clickhouse ALL=(ALL) NOPASSWD: ALL ``` # 二、源码 ```bash [clickhouse@bogon clickhouse-master]$ git clone --recursive https://github.com/ClickHouse/ClickHouse.git ``` 网络条件不好的情况下,建议使用以下分步骤方法: ```bash [clickhouse@bogon clickhouse-master]$ git clone https://github.com/ClickHouse/ClickHouse.git [clickhouse@bogon clickhouse-master]$ cd ClickHouse/ # 如果以下步骤失败,则删除contrib目录重试该步骤: [clickhouse@bogon ClickHouse]$ git submodule update --init --recursive ``` # 三、准备编译 ## 创建build目录 ```bash [clickhouse@bogon ClickHouse]$ mkdir build [clickhouse@bogon ClickHouse]$ cd build ``` 之后,如果编译失败需要重试时,直接清空build目录即可。 ## 升级cmake ```bash [clickhouse@bogon build]$ cmake ../ CMake Error at CMakeLists.txt:1 (cmake_minimum_required): CMake 3.3 or higher is required. You are running version 2.8.12.2 -- Configuring incomplete, errors occurred! [clickhouse@bogon build]$ cmake --version cmake version 2.8.12.2 ``` 系统自带的cmake版本无法满足要求。需要升级。 ```bash [clickhouse@bogon build]$ sudo yum install cmake3.x86_64 [clickhouse@bogon build]$ vim ~/.bashrc # 添加以下行: alias cmake='cmake3' [clickhouse@bogon build]$ source ~/.bashrc [clickhouse@bogon build]$ cmake --version cmake3 version 3.17.3 ``` ## 升级gcc 参考 [CentOS 7 升级gcc至9.3](../../../Coding/编译/CentOS 7 升级gcc至9.3) ## 安装ninja 参考 [Linux安装与升级ninja](../../../Coding/编译/Linux安装与升级ninja) 这里安装的ninja版本可能不满足编译要求,如果在cmake阶段报告以下warning,建议手动实际ninja到1.8以上版本: ```bash CMake Warning (dev): The detected version of Ninja: 1.7.2 is less than the version of Ninja required by CMake for adding restat dependencies to the build.ninja manifest regeneration target: 1.8 ``` # 四、编译 默认编译release版本: ```bash [clickhouse@bogon build]$ cmake ../ [clickhouse@bogon build]$ ninja -j2 ``` 如果需要编译debug版本,则: ```bash [clickhouse@bogon build]$ cmake ../ -DCMAKE_BUILD_TYPE=Debug [clickhouse@bogon build]$ ninja -j2 ``` 注:并行编译有时会发生莫名其妙的错误,此时可以使用串行继续编译“ ninja -j1 ”。 ## cmake报告缺少文件 ``` …… CMake Error at contrib/CMakeLists.txt:21 (add_subdirectory): add_subdirectory given source "consistent-hashing-sumbur" which is not an existing directory. ``` 使用以上git clone方法,获得的源码contrib目录会缺省一些文件和子目录,解决方法是: 手动从GitHub下载一份同版本的.zip源码压缩包,解压后,从其中拷贝缺少的文件和子目录: ```bash [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/CMakeLists.txt contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/*-cmake contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/consistent-hashing-sumbur contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/consistent-hashing contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/croaring contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/FastMemcpy contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/libdivide contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/libmetrohash contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/murmurhash contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/re2_st contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/cityhash102 contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/libfarmhash contrib/ [clickhouse@bogon ClickHouse]$ cp -r ../ClickHouse-master-zip/contrib/libbtrie contrib/ ``` # 五、问题 ## 问题1 编译阶段报错: ```bash contrib/arrow-cmake/thrift/config.h:1:1: error: expected unqualified-id before ‘.’ token ``` 解决方法: ```bash cd contrib/arrow-cmake/build/cmake ln -s ../../../thrift/build/cmake/config.h.in config.h.in -f ``` ## 问题2 编译阶段报错: ```bash [11/16] Building CXX object utils/db-generator/CMakeFiles/query_db_generator.dir/query_db_generator.cpp.o …… ../utils/db-generator/query_db_generator.cpp:117:33: error: wrong number of template arguments (2, should be 4) 117 | std::map type_definition = ``` 解决方法: 在报错的cpp文件中 include ```cpp [clickhouse@bogon build]$ vim ../utils/db-generator/query_db_generator.cpp // 增加include #include ``` # 六、调试 编译成功后,可执行查询在build/programs/目录下,将该目录加入PATH。 ```bash [clickhouse@bogon ~]$ vim ~/.bash_profile PATH=~/clickhouse-master/ClickHouse/build/programs/:$PATH:$HOME/.local/bin:$HOME/bin ``` 运行server并使用client连接: ```bash [clickhouse@bogon ~]$ clickhouse-server [clickhouse@bogon ~]$ clickhouse-client ClickHouse client version 20.10.1.1. Connecting to localhost:9000 as user default. Connected to ClickHouse server version 20.10.1 revision 54441. bogon :) show databases; SHOW DATABASES ┌─name───────────────────────────┐ │ _temporary_and_external_tables │ │ default │ │ system │ └────────────────────────────────┘ 3 rows in set. Elapsed: 0.011 sec. ``` # 七、参考 - [如何在开发过程中编译 ClickHouse](https://clickhouse.tech/docs/zh/development/build/) - [ClickHouse官网](https://clickhouse.tech/) - [ClickHouse源码仓库](https://github.com/ClickHouse/ClickHouse) - [ClickHouse系列教程六:源码分析之Debug编译运行](https://blog.csdn.net/zhangpeterx/article/details/95320437) - [clickhouse 编译](https://blog.csdn.net/templarzq/article/details/104395622)