ClickHouse 多实例环境安装

环境

  • 已安装单机版的ClickHouse,官方推荐使用 rpm 安装,也可以安装好一台后复制文件过去
  • CentOS Linux release 7.8.2003 (Core)
  • ClickHouse 20.11.6.6

多实例配置

创建目录

cd /clickhouse
mkdir data9200 log9200
chown -R clickhouse.clickhouse /clickhouse/data9200 /clickhouse/log9200

复制一份已有的配置文件

复制一份已有配置修改下,配置第二个实例

cp /etc/clickhouse-server/config.xml /etc/clickhouse-server/config9200.xml

授权

chown clickhouse.clickhouse /etc/clickhouse-server/config9200.xml

修改默认存放路径

由于之前安装时已经将默认路径从 /var/log/clickhouse-server 修改为了 /clickhouse/log,这次需要修改为如下:

# 更改日志路径
sed -i "s:/clickhouse/log:/clickhouse/log9200:g" /etc/clickhouse-server/config9200.xml
# 更改数据路径
sed -i "s:/clickhouse/data:/clickhouse/data9200:g" /etc/clickhouse-server/config9200.xml

/** 实际修改的地方
<log>//clickhouse/log9200/clickhouse-server.log</log>
<errorlog>//clickhouse/log9200/clickhouse-server.err.log</errorlog>
<path>/clickhouse/data9200/</path>
<tmp_path>/clickhouse/data9200/tmp/</tmp_path>
<user_files_path>/clickhouse/data9200/user_files/</user_files_path>
<path>/clickhouse/data9200/access/</path>
<format_schema_path>/clickhouse/data9200/format_schemas/</format_schema_path>
*/

更改端口

修改第二个实例的配置
vi /etc/clickhouse-server/config9200.xml

/** 
<http_port>8123</http_port>
<tcp_port>9000</tcp_port>
<mysql_port>9004</mysql_port>
<interserver_http_port>9009</interserver_http_port> # 复制副本之间通信的端口。用于数据交换。
*/
改成
<http_port>8223</http_port>
<tcp_port>9200</tcp_port>
<mysql_port>9204</mysql_port>
<interserver_http_port>9209</interserver_http_port>

启动或停止

方式1 systemctl

  • 复制启动脚本
cp /etc/systemd/system/clickhouse-server.service /etc/systemd/system/clickhouse-server9200.service
  • 修改启动脚本
    只需要修改一行
/**
ExecStart=/usr/bin/clickhouse-server --config=/etc/clickhouse-server/config.xml --pid-file=/run/clickhouse-server/clickhouse-server.pid
*/
改为
ExecStart=/usr/bin/clickhouse-server --config=/etc/clickhouse-server/config9200.xml --pid-file=/run/clickhouse-server/clickhouse-server9200.pid
  • 启动/关闭
# 启动
systemctl start clickhouse-server
systemctl start clickhouse-server9200
# 关闭
systemctl stop clickhouse-server
systemctl stop clickhouse-server9200

方式2 编写脚本启动/关闭

  • 启动
    vi start_clickhouse.sh
#!/bin/bash
nohup sudo -u clickhouse /usr/bin/clickhouse-server --config=/etc/clickhouse-server/config.xml &
sleep 3
nohup sudo -u clickhouse /usr/bin/clickhouse-server --config=/etc/clickhouse-server/config9200.xml &
  • 关闭
    vi stop_clickhouse.sh
#!/bin/bash
ps -ef|grep clickhouse|grep config.xml|grep -v grep|awk '{print "kill -9 "$2}'|sh
ps -ef|grep clickhouse|grep config9200.xml|grep -v grep|awk '{print "kill -9 "$2}'|sh
  • 授权
chmod +x start_clickhouse.sh
chmod +x stop_clickhouse.sh

验证是否正常启动

# 端口
ss -ntpl|grep 9000
ss -ntpl|grep 9200

# 登陆查看
clickhouse-client --port 9000
clickhouse-client --port 9200

# 数据
ll /clickhouse/data9200/

推荐阅读更多精彩内容