大数据-134 ClickHouse 单机+集群节点落地手册 | 安装配置 | systemd 管理 / config.d

TL;DR

  • 场景:Ubuntu 上快速装 ClickHouse,先跑通单机,再搭 3 节点集群。
  • 结论 :使用官方源 + signed-by 方式安装;用 systemd 管服务 ,别手动 sudo -u clickhouse clickhouse-server;集群建议用 ClickHouse Keeper 替代 ZooKeeper。
  • 产出:一键安装脚本、单机 MRE(3 分钟)

官网网站

shell 复制代码
https://clickhouse.com/

最小可运行示例

shell 复制代码
# 1) 依赖 & keyring
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg
curl -fsSL 'https://packages.clickhouse.com/rpm/lts/repodata/repomd.xml.key' \
  | sudo gpg --dearmor -o /usr/share/keyrings/clickhouse-keyring.gpg

# 2) 源(stable 或 lts 二选一)
ARCH=$(dpkg --print-architecture)
echo "deb [signed-by=/usr/share/keyrings/clickhouse-keyring.gpg arch=${ARCH}] 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

启动与开机自启

shell 复制代码
sudo systemctl enable --now clickhouse-server
sudo systemctl status clickhouse-server --no-pager

单机安装

我是 Ubuntu 的服务器:

shell 复制代码
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates dirmngr

sudo apt-key adv --keyserver keyserver.ubuntu.com --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

等待安装 ClickHouse

等待之后,可以看到已经顺利的安装完毕: 默认的密码路径是:

shell 复制代码
/etc/clickhouse-server/users.d/default-password.xml

配置权限

不配置后续可能会报错

shell 复制代码
sudo chown -R clickhouse:clickhouse /etc/clickhouse-server
sudo chmod -R 755 /etc/clickhouse-server
sudo chown -R clickhouse:clickhouse /var/lib/clickhouse
sudo chmod -R 755 /var/lib/clickhouse

单机测试

启动ClickServer

shell 复制代码
sudo -u clickhouse clickhouse-server

启动ClickClient

shell 复制代码
clickhouse-client -m

集群安装

集群安装

目前我有的节点是三台:

  • h121 2C4G
  • h122 2C4G
  • h123 2C4G 刚才单机安装是在 h121 节点上,我们需要对三台云服务都执行相应的Shell:
shell 复制代码
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates dirmngr
sudo apt-key adv --keyserver keyserver.ubuntu.com --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

停止服务

我们需要停止之前的服务,来空出一定的内存和端口给 ClickHouse 使用。 (如果你的内存比较富裕可以忽略) 只保留 ZooKeeper 即可,其他服务可以都关闭掉。比如Hadoop集群、Kafka集群、Redis集群等。

配置文件

config.xml

我们需要三台云服务器都修改config.xml

shell 复制代码
cd /etc/clickhouse-server
vim config.xml

修改如下内容:

shell 复制代码
<!-- Path to data directory, with trailing slash. -->
<path>/var/lib/clickhouse/</path>

开放远程访问,允许所有连接:

shell 复制代码
<listen_host>::</listen_host>

在根标签下加入内容(我是放入了头部):

shell 复制代码
<include_from>/etc/clickhouse-server/config.d/metrika.xml</include_from>

内容修改为如下的样子:

metrika.xml

shell 复制代码
cd /etc/clickhouse-server/config.d
# 该文件是新增的
vim metrika.xml

注意,下面的内容需要根据你的服务器IP来进行配置:

xml 复制代码
<yandex>
  <remote_servers>
    <perftest_3shards_1replicas>
      <shard>
        <internal_replication>true</internal_replication>
        <replica>
          <host>h121.wzk.icu</host>
          <port>9000</port>
          <user>default</user>
          <password>clickhouse@wzk.icu</password>
        </replica>
      </shard>
      <shard>
        <internal_replication>true</internal_replication>
        <replica>
          <host>h122.wzk.icu</host>
          <port>9000</port>
          <user>default</user>
          <password>clickhouse@wzk.icu</password>
        </replica>
      </shard>
      <shard>
        <internal_replication>true</internal_replication>
        <replica>
          <host>h123.wzk.icu</host>
          <port>9000</port>
          <user>default</user>
          <password>clickhouse@wzk.icu</password>
        </replica>
      </shard>
    </perftest_3shards_1replicas>
  </remote_servers>
  <zookeeper-servers>
    <node index="1">
      <host>h121.wzk.icu</host>
      <port>2181</port>
    </node>
    <node index="2">
      <host>h122.wzk.icu</host>
      <port>2181</port>
    </node>
    <node index="3">
      <host>h123.wzk.icu</host>
      <port>2181</port>
    </node>
  </zookeeper-servers>
  <macros>
    <shard>01</shard>
    <replica>h121.wzk.icu</replica>
  </macros>
  <networks>
    <ip>::/0</ip>
  </networks>
  <clickhouse_compression>
    <case>
      <min_part_size>10000000000</min_part_size>
      <min_part_size_ratio>0.01</min_part_size_ratio>
      <method>lz4</method>
    </case>
  </clickhouse_compression>
</yandex>

保存之后,修改一下权限:

shell 复制代码
sudo chown -R clickhouse:clickhouse /etc/clickhouse-server/config.d/metrika.xml

修改密码

shell 复制代码
vim /etc/clickhouse-server/users.d/default-password.xml

为了测试方便,修改为如下的内容:

xml 复制代码
<clickhouse>
    <users>
        <default>
            <password>clickhouse@wzk.icu</password>
            <networks>
                <ip>::/0</ip>
            </networks>
        </default>
    </users>
</clickhouse>
shell 复制代码
vim /etc/clickhouse-server/users.xml

为了测试方便,修改的密码如下:

xml 复制代码
省略其他内容
<password>clickhouse@wzk.icu</password>

启动测试

shell 复制代码
sudo -u clickhouse clickhouse-server --config-file=/etc/clickhouse-server/config.xml

正式启动

我们需要编辑一下 system 的文件,来实现优化的启停

shell 复制代码
vim /etc/systemd/system/clickhouse-server.service

可以看到,里边的配置已经在我们安装的时候就配置好了,你可以根据你的需要来修改: 接着我们使用系统命令启动:

shell 复制代码
systemctl start clickhouse-server
systemctl status clickhouse-server

我们可以看到程序已经顺利的运行了:

相关推荐
不早睡不改名2 分钟前
网络编程基础:从BIO到NIO再到AIO(一)
后端
开源之眼3 分钟前
《github star 加星 Taimili.com 艾米莉 》为什么Java里面,Service 层不直接返回 Result 对象?
java·后端·github
心在飞扬4 分钟前
RAPTOR 递归文档树优化策略
前端·后端
zone773914 分钟前
003:RAG 入门-LangChain 读取图片数据
后端·python·面试
心在飞扬18 分钟前
LangChain Parent Document Retriever (父文档检索器)
后端
zone773927 分钟前
002:RAG 入门-LangChain 读取文本
后端·算法·面试
用户83562907805128 分钟前
在 PowerPoint 中用 Python 添加和定制形状的完整教程
后端·python
武子康32 分钟前
大数据-240 离线数仓 - 广告业务 Hive ADS 实战:DataX 将 HDFS 分区表导出到 MySQL
大数据·后端·apache hive
心在飞扬1 小时前
MultiVector 多向量检索
前端·后端
Gopher_HBo1 小时前
Go之基于TCP/IP协议栈的socket通信
后端