非root使用者一鍵安裝clickhouse叢集
- 1. 環境要求
- 2. 安裝腳本
- 3. 配置說明
- 4. 編寫啟停腳本
-
- 1. 執行啟動
- 2. 執行停止
- 3. 連接配接服務
- 5. 叢集搭建
-
- zookeeper叢集搭建
-
- 1.環境準備
- 2. 安裝zookeeper
- 3.啟動
- clickhouse叢集配置
-
- 1. 建立一個metrika.xml
- 2.引入merika.xml
- 6. 問題整理
官方中文文檔:https://clickhouse.tech/docs/zh/
1. 環境要求
檢查目前CPU是否支援SSE 4.2的指令,咱也不知道為什麼,官方這樣說的
官方安裝包下載下傳位址:https://github.com/ClickHouse/ClickHouse/releases
2. 安裝腳本
安裝使用者名:clickhouse
home目錄:/home/clickhouse
編輯安裝腳本
vim install.sh
内容如下,可以根據實際情況修改腳本
#預設使用者密碼
PLAIN_PASSWORD=123123
HOUSE_VERSION=v21.5.6.6-stable
LATEST_VERSION=21.5.6.6
wget https://github.91chifun.workers.dev/https://github.com//ClickHouse/ClickHouse/releases/download/$HOUSE_VERSION/clickhouse-common-static-$LATEST_VERSION.tgz
wget https://github.91chifun.workers.dev/https://github.com//ClickHouse/ClickHouse/releases/download/$HOUSE_VERSION/clickhouse-server-$LATEST_VERSION.tgz
#解壓
tar -zxvf clickhouse-common-static-$LATEST_VERSION.tgz
tar -zxvf clickhouse-server-$LATEST_VERSION.tgz
#修改配置檔案路徑
CONFIG_DIR=/home/clickhouse/clickhouse-server/conf
mkdir -p $CONFIG_DIR
mkdir -p /home/clickhouse/clickhouse-server/logs
cp /home/clickhouse/clickhouse-server-$LATEST_VERSION/etc/clickhouse-server/config.xml $CONFIG_DIR/config.xml
cp /home/clickhouse/clickhouse-server-$LATEST_VERSION/etc/clickhouse-server/users.xml $CONFIG_DIR/users.xml
#配置環境變量
echo "CLICK_HOUSE_HOME=/home/clickhouse/clickhouse-common-static-$LATEST_VERSION/usr/bin" >> ~/.bash_profile
echo 'export PATH=$PATH:$CLICK_HOUSE_HOME' >> ~/.bash_profile
source ~/.bash_profile
# 腳本修改配置檔案路徑
# sed -i 's/源字元串/新字元串/g'
SERVER_CONFIG_PATH=$CONFIG_DIR/config.xml
#修改日志存儲
sed -i 's/\/var\/log\/clickhouse-server\/clickhouse-server.log/\/home\/clickhouse\/clickhouse-server\/logs\/clickhouse-server.log/g' $SERVER_CONFIG_PATH
sed -i 's/\/var\/log\/clickhouse-server\/clickhouse-server.err.log/\/home\/clickhouse\/clickhouse-server\/logs\/clickhouse-server.err.log/g' $SERVER_CONFIG_PATH
#修改資料存儲
sed -i 's/\/var\/lib\/clickhouse\//\/home\/clickhouse\/data\//g' $SERVER_CONFIG_PATH
#修改ddl存儲路徑
sed -i 's/\/clickhouse\/task_queue\/ddl/\/home\/clickhouse\/task_queue\/ddl/g' $SERVER_CONFIG_PATH
echo "修改監聽host 所有用戶端都可以通路"
sed -r -i "/<listen_host>::<\/listen_host>/ s/.*/<listen_host>::<\/listen_host>/" $SERVER_CONFIG_PATH
# 修改預設使用者腳本
echo 生成sha256 密碼
PASSWORD=$(printf "%s" $(printf "%s" "$PLAIN_PASSWORD"|sha256sum| tr -d '-')| sed 's/ //g')
echo "設定hash密碼 : $PASSWORD"
sed -r -i "/<password><\/password>/ s/.*/<password_sha256_hex>$PASSWORD<\/password_sha256_hex>/" $CONFIG_DIR/users.xml
echo "修改使用者通路權限"
sed -r -i "/<access_management>1<\/access_management>/ s/.*/<access_management>1<\/access_management>/" $CONFIG_DIR/users.xml
echo "執行完成!"
執行安裝腳本
sh install.sh
3. 配置說明
配置檔案:clickhouse-server/conf/config.xml
詳細配置說明請參考官方文檔:https://clickhouse.tech/docs/zh/operations/server-configuration-parameters/settings/
4. 編寫啟停腳本
vim server.sh
配置請根據實際情況配置,内容如下:
#程序id位址
CH_PID_FILE=$HOME/clickhouse-server/clickhouse_server.pid
#啟動的配置位址
CH_CONFIG_PATH=$HOME/clickhouse-server/conf/config.xml
#nohup 輸出log 位址
CH_DAEMON_OUT=$HOME/clickhouse_server.log
case $1 in
start)
echo -n "Starting clickhouse sever ... "
if [ -f "$CH_PID_FILE" ]; then
if kill -0 `cat "$CH_PID_FILE"` > /dev/null 2>&1; then
echo $command already running as process `cat "$CH_PID_FILE"`.
exit 1
fi
fi
. ~/.bash_profile
clickhouse server -P=$CH_PID_FILE -C=$CH_CONFIG_PATH --daemon
sleep 2
pid=$(cat "${CH_PID_FILE}")
if ps -p "${pid}" > /dev/null 2>&1; then
echo STARTED
else
echo FAILED TO START Please check the logs
exit 1
fi
;;
stop)
echo -n "stopping clickhouse server ..."
if [ ! -f "$CH_PID_FILE" ]
then
echo "no clickhouse sever to stop (could not find file $CH_PID_FILE)"
else
kill $(cat "$CH_PID_FILE")
sleep 1
echo STOPPED
fi
exit 0
;;
*)
echo "Usage: $0 {start|stop}"
;;
esac
1. 執行啟動
sh server.sh start
2. 執行停止
sh server.sh stop
3. 連接配接服務
5. 叢集搭建
clickhouse叢集=zookeeper叢集+clickhouse叢集配置
zookeeper叢集搭建
1.環境準備
- 需要安裝java運作環境,我這裡安裝的是1.8,略。
- 下載下傳zookeeper包
wget https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz
2. 安裝zookeeper
- 解壓tar包
tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz
- 進入
cd apache-zookeeper-3.6.3-bin/conf
- 複制配置檔案
cp zoo_sample.cfg zoo.cfg
- 修改配置檔案如下
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/data/zookeeper/ # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 ## Metrics Providers # # https://prometheus.io Metrics Exporter #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider #metricsProvider.httpPort=7000 #metricsProvider.exportJvmInfo=true server.0=192.168.2.2:2888:3888 server.1=192.168.2.3:2888:3888 server.2=192.168.2.4:2888:3888
- 在對應機器上設定myid
#假設目前機器是server.0,需要在$dataDir目錄下寫一個myid檔案。其他機器myid的值也是與配置對應的。 echo "0" > /data/zookeeper/myid
- 配置說明
參數名 | 說明 |
---|---|
tickTime | 預設值:3000(ms),不支援系統屬性方式配置。用于配置zookeeper叢集中節點之間維持心跳的時間間隔,同時,它也是zookeeper中最小的時間長度單元,很多運作時的時間間隔都是使用它的倍數來表示的。 |
initLimit | Zookeeper叢集中的Leader 節點接收其他Follower節點初始化連接配接等待的最大時間(10*tickTime),如果Leader 節點在這段時間内還未接收到Follower節點的傳回資訊,則連接配接失敗。 |
syncLimit | Zookeeper叢集中的Leader 節點和其他Follower節點通信的最大等待時間(5*tickTime)。 |
dataDir | 無預設值,必須配置。伺服器存儲快照檔案目錄。預設情況下,如果沒有配置dataLogDir,那麼事務日志也會存儲在這個目錄中。建議将事務日志目錄獨立配置。 |
clientPort | 無預設值,必須配置,不支援系統屬性方式配置。目前伺服器對外提供服務端口,客戶通過此端口與伺服器建立連接配接,一般設定為2181。叢集中不需要統一端口,可任意配置端口。 |
server.A=B:C:D | A:節點在叢集中的唯一ID,需要與dataDir/myid檔案中的内容保持一緻,取值範圍為1~255 B:節點的伺服器IP位址。 C:叢集中Follower節點與Leader 節點之間通信的端口。 D:當叢集中的Follower節點當機或出現故障時,叢集進行重新選舉Leader時所使用的端口。 |
3.啟動
- 進入zookeeper的 bin目錄依次啟動上面的叢集
./zkServer.sh start
- 全部啟動完畢後,檢視節點狀态
./zkServer.sh status
clickhouse叢集配置
推薦一個幫助了解的資料分片、備份文章:https://tianjinsong.blog.csdn.net/article/details/83011794
1. 建立一個metrika.xml
vim /home/clickhouse/clickhouse-server/conf/metrika.xml
内容如下:
<yandex>
<!-- 叢集配置 -->
<clickhouse_remote_servers>
<!-- 叢集名稱dev -->
<dev>
<!-- 資料分片1 -->
<shard>
<!-- 開啟表自身同步建議生産開啟 -->
<internal_replication>true</internal_replication>
<!-- 副本 s1r1 -->
<replica>
<host>ck31.xxxx.com.cn</host>
<port>9000</port>
<user>default</user>
<password>123123</password>
</replica>
<!-- 副本 s1r2-->
<replica>
<host>ck31.xxxx.com.cn</host>
<port>9000</port>
<user>default</user>
<password>123123</password>
</replica>
</shard>
<!-- 資料分片2 -->
<shard>
<internal_replication>true</internal_replication>
<!-- 副本 s2r1-->
<replica>
<host>ck32.xxxx.sina.com.cn</host>
<port>9000</port>
<user>default</user>
<password>123123</password>
</replica>
<!-- 副本 s2r2-->
<replica>
<host>ck31.xxxx.com.cn</host>
<port>9000</port>
<user>default</user>
<password>123123</password>
</replica>
</shard>
<!-- 資料分片3 -->
<shard>
<internal_replication>true</internal_replication>
<!-- 副本 s3r1-->
<replica>
<host>ck33.xxxxa.com.cn</host>
<port>9000</port>
<user>default</user>
<password>123123</password>
</replica>
<!-- 副本 s3r2-->
<replica>
<host>ck33.xxxxa.com.cn</host>
<port>9000</port>
<user>default</user>
<password>123123</password>
</replica>
</shard>
</dev>
</clickhouse_remote_servers>
<!-- 本節點分片id+ 副本id -->
<macros>
<shard>01</shard>
<replica>01</replica>
</macros>
<!-- 監聽網絡(貌似重複) -->
<networks>
<ip>::/0</ip>
</networks>
<!-- ZK -->
<zookeeper-servers>
<node index="1">
<host>1.xxxx.sina.com.cn</host>
<port>2181</port>
</node>
<node index="2">
<host>2.xxxx.sina.com.cn</host>
<port>2181</port>
</node>
<node index="3">
<host>3.xxxxp.sina.com.cn</host>
<port>2181</port>
</node>
</zookeeper-servers>
<!-- 資料壓縮算法 -->
<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>
2.引入merika.xml
在config.xml檔案中的yandex 标簽内 修改對應标簽如下 沒有則添加。
<!-- incl 指定配置标簽 對應metrika的标簽配置 -->
<!-- 叢集配置 -->
<remote_servers incl="clickhouse_remote_servers" />
<!-- zookeeper配置 -->
<zookeeper incl="zookeeper-servers" optional="true" />
<!-- macros配置,複制表的參數替換,如果不使用複制表,則可以省略 -->
<macros incl="macros" optional="true" />
<!-- 壓縮算法配置 -->
<compression incl="clickhouse_compression" />
<!-- 引入上面的配置檔案 -->
<include_from>/home/clickhouse/clickhouse-server/conf/metrika.xml</include_from>
config.xml 如果是正确修改,那麼服務會自動加載,無需重新開機。
6. 問題整理
- 因為8123端口被其他應用占用,我修改為<http_port>8122</http_port>,啟動報錯:
DB::Exception: Listen [::]:8122 failed: Poco::Exception. Code: 1000, e.code() = 0, e.displayText() = DNS error: EAI: Address family for hostname not supported
解決辦法:修改config.xml檔案
記錄一小步,成長一大步。有問題歡迎大家在評論區讨論。