Last active
September 10, 2024 06:41
-
-
Save githubdebugger/58aa1e335f80c732b65333498b48f8da to your computer and use it in GitHub Desktop.
clickhouse_install.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to prompt user for yes/no input | |
ask_yes_no() { | |
while true; do | |
read -p "$1 (y/n): " yn | |
case $yn in | |
[Yy]* ) return 0;; | |
[Nn]* ) return 1;; | |
* ) echo "Please answer yes or no.";; | |
esac | |
done | |
} | |
# Function to get user input with default value | |
get_input_with_default() { | |
read -p "$1 [$2]: " input | |
echo ${input:-$2} | |
} | |
# Function to insert XML content before the closing tag | |
insert_before_closing_tag() { | |
local file="$1" | |
local content="$2" | |
local closing_tag="$3" | |
sudo sed -i "/<\/${closing_tag}>/i ${content}" "$file" | |
} | |
# Help string | |
show_help() { | |
echo "Usage: $0 [OPTION]" | |
echo "Install, configure, or clear ClickHouse installation." | |
echo "" | |
echo "Options:" | |
echo " -h, --help Show this help message and exit" | |
echo " -i, --install Install and configure ClickHouse" | |
echo " -c, --clear Clear ClickHouse installation completely" | |
} | |
# Clear ClickHouse installation | |
clear_clickhouse() { | |
echo "Clearing ClickHouse installation..." | |
# Stop the service | |
sudo systemctl stop clickhouse-server | |
# Remove packages | |
sudo apt-get remove --purge -y clickhouse-server clickhouse-client clickhouse-common-static | |
sudo apt-get autoremove -y | |
# Remove directories | |
sudo rm -rf /var/lib/clickhouse | |
sudo rm -rf /etc/clickhouse-server | |
sudo rm -rf /var/log/clickhouse-server | |
sudo rm -rf /var/run/clickhouse-server | |
# Remove configuration files | |
sudo rm -f /etc/apt/sources.list.d/clickhouse.list | |
sudo rm -f /usr/share/keyrings/clickhouse-keyring.gpg | |
sudo rm -f /etc/security/limits.d/clickhouse.conf | |
# Remove symlinks | |
sudo rm -f /usr/bin/clickhouse* | |
sudo rm -f /usr/bin/ch | |
sudo rm -f /usr/bin/chl | |
sudo rm -f /usr/bin/chc | |
# Remove user and group | |
sudo userdel -r clickhouse | |
sudo groupdel clickhouse | |
# Remove entries from limits.conf | |
sudo sed -i '/clickhouse/d' /etc/security/limits.conf | |
# Remove the main clickhouse binary | |
sudo rm -f /usr/bin/clickhouse | |
# Update package lists | |
sudo apt-get update | |
echo "ClickHouse installation cleared completely." | |
} | |
# Function to backup configuration files | |
backup_config() { | |
local backup_dir="$1" | |
echo "Backing up ClickHouse configuration files to $backup_dir" | |
sudo mkdir -p "$backup_dir" | |
sudo cp -R /etc/clickhouse-server "$backup_dir" | |
echo "Backup completed." | |
} | |
# Install and configure ClickHouse | |
install_clickhouse() { | |
echo "ClickHouse Installation and Configuration Script" | |
echo "================================================" | |
# Install ClickHouse | |
echo "Installing ClickHouse..." | |
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 | |
echo "deb [signed-by=/usr/share/keyrings/clickhouse-keyring.gpg] 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 | |
# Backup configuration | |
if ask_yes_no "Do you want to backup the current configuration before making changes?"; then | |
backup_location=$(get_input_with_default "Enter the backup location" "/tmp/clickhouse_config_backup_$(date +%Y%m%d_%H%M%S)") | |
backup_config "$backup_location" | |
fi | |
# Configuration | |
echo "Configuring ClickHouse..." | |
# Data directory | |
default_data_dir="/var/lib/clickhouse" | |
if ask_yes_no "Do you want to change the default data directory?"; then | |
data_dir=$(get_input_with_default "Enter the new data directory path" "$default_data_dir") | |
sudo mkdir -p "$data_dir" | |
sudo chown -R clickhouse:clickhouse "$data_dir" | |
sudo chmod 700 "$data_dir" | |
sudo sed -i "s|<path>$default_data_dir</path>|<path>$data_dir</path>|" /etc/clickhouse-server/config.xml | |
else | |
data_dir=$default_data_dir | |
fi | |
# Memory settings | |
max_memory=$(get_input_with_default "Enter max memory usage in bytes" "10000000000") | |
sudo sed -i "s|<max_memory_usage>.*</max_memory_usage>|<max_memory_usage>$max_memory</max_memory_usage>|" /etc/clickhouse-server/config.xml | |
# Network settings | |
if ask_yes_no "Do you want to listen on all interfaces?"; then | |
sudo sed -i "s|<listen_host>::1</listen_host>|<listen_host>::</listen_host>|" /etc/clickhouse-server/config.xml | |
fi | |
http_port=$(get_input_with_default "Enter HTTP port" "8123") | |
tcp_port=$(get_input_with_default "Enter TCP port" "9000") | |
sudo sed -i "s|<http_port>.*</http_port>|<http_port>$http_port</http_port>|" /etc/clickhouse-server/config.xml | |
sudo sed -i "s|<tcp_port>.*</tcp_port>|<tcp_port>$tcp_port</tcp_port>|" /etc/clickhouse-server/config.xml | |
# User settings | |
default_password=$(openssl rand -base64 12) | |
user_password=$(get_input_with_default "Enter password for default user" "$default_password") | |
sudo sed -i "s|<password></password>|<password>$user_password</password>|" /etc/clickhouse-server/users.xml | |
# Logging | |
log_level=$(get_input_with_default "Enter log level (trace/debug/information/warning/error)" "information") | |
sudo sed -i "s|<level>.*</level>|<level>$log_level</level>|" /etc/clickhouse-server/config.xml | |
# Compression | |
if ask_yes_no "Do you want to enable Zstd compression?"; then | |
compression_config=$(cat << EOF | |
<compression> | |
<case> | |
<method>zstd</method> | |
<min_part_size>10000000000</min_part_size> | |
<min_part_size_ratio>0.01</min_part_size_ratio> | |
</case> | |
</compression> | |
EOF | |
) | |
insert_before_closing_tag "/etc/clickhouse-server/config.xml" "$compression_config" "clickhouse" | |
fi | |
# Distributed configuration | |
if ask_yes_no "Are you setting up a ClickHouse cluster?"; then | |
echo "Enter the details for each node in the cluster. Enter 'done' when finished." | |
cluster_config="<remote_servers><my_cluster>" | |
shard_num=1 | |
while true; do | |
host=$(get_input_with_default "Enter hostname for shard $shard_num (or 'done' to finish)" "") | |
if [ "$host" == "done" ]; then | |
break | |
fi | |
port=$(get_input_with_default "Enter port for $host" "9000") | |
cluster_config+="<shard><replica><host>$host</host><port>$port</port></replica></shard>" | |
((shard_num++)) | |
done | |
cluster_config+="</my_cluster></remote_servers>" | |
insert_before_closing_tag "/etc/clickhouse-server/config.xml" "$cluster_config" "clickhouse" | |
fi | |
# ZooKeeper configuration | |
if ask_yes_no "Do you want to configure ZooKeeper for distributed DDL?"; then | |
zk_host=$(get_input_with_default "Enter ZooKeeper host" "localhost") | |
zk_port=$(get_input_with_default "Enter ZooKeeper port" "2181") | |
zk_config=$(cat << EOF | |
<zookeeper> | |
<node> | |
<host>$zk_host</host> | |
<port>$zk_port</port> | |
</node> | |
</zookeeper> | |
EOF | |
) | |
insert_before_closing_tag "/etc/clickhouse-server/config.xml" "$zk_config" "clickhouse" | |
fi | |
# In-memory database configuration | |
if ask_yes_no "Do you want to configure an in-memory database?"; then | |
db_name=$(get_input_with_default "Enter the name for the in-memory database" "inmemory_db") | |
inmemory_config=$(cat << EOF | |
<database_atomic_memory> | |
<databases> | |
<$db_name> | |
<engine>Memory</engine> | |
</$db_name> | |
</databases> | |
</database_atomic_memory> | |
EOF | |
) | |
insert_before_closing_tag "/etc/clickhouse-server/config.xml" "$inmemory_config" "clickhouse" | |
echo "In-memory database '$db_name' has been configured." | |
fi | |
# System limits | |
echo "clickhouse soft nofile 262144" | sudo tee -a /etc/security/limits.conf | |
echo "clickhouse hard nofile 262144" | sudo tee -a /etc/security/limits.conf | |
# Start ClickHouse | |
sudo systemctl start clickhouse-server | |
sudo systemctl enable clickhouse-server | |
echo "ClickHouse installation and configuration completed!" | |
echo "You can connect to ClickHouse using: clickhouse-client --password" | |
echo "Default user password: $user_password" | |
if [ -n "$backup_location" ]; then | |
echo "Configuration backup is located at: $backup_location" | |
fi | |
} | |
# Main script logic | |
case "$1" in | |
-h|--help) | |
show_help | |
;; | |
-i|--install) | |
install_clickhouse | |
;; | |
-c|--clear) | |
clear_clickhouse | |
;; | |
*) | |
show_help | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment