Install and configure ClickHouse for high-performance analytics with clustering

Intermediate 45 min Apr 01, 2026 1,061 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 AlmaLinux 9 Rocky Linux 9 Fedora 41

Set up ClickHouse OLAP database with clustering support for real-time analytics workloads. Configure distributed tables, security, SSL encryption, and monitoring for production environments.

Prerequisites

  • Root or sudo access
  • Minimum 4GB RAM
  • Multiple servers for clustering
  • Network connectivity between cluster nodes

What this solves

ClickHouse is a columnar OLAP database designed for real-time analytics on large datasets. This tutorial helps you install and configure a production-ready ClickHouse cluster with distributed tables, SSL encryption, user security, performance optimization, and backup procedures for high-performance analytics workloads.

Step-by-step installation

Update system packages and install dependencies

Start by updating your package manager and installing required dependencies for ClickHouse.

sudo apt update && sudo apt upgrade -y
sudo apt install -y apt-transport-https ca-certificates dirmngr curl gnupg2
sudo dnf update -y
sudo dnf install -y curl gnupg2 ca-certificates

Add ClickHouse repository

Add the official ClickHouse repository to install the latest stable version.

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 update
sudo tee /etc/yum.repos.d/clickhouse.repo <

Install ClickHouse server and client

Install ClickHouse server and client packages. The installer will prompt for a default user password.

`sudo apt install -y clickhouse-server clickhouse-client`
`sudo dnf install -y clickhouse-server clickhouse-client`
Note: During installation, you'll be prompted to set a password for the default user. Choose a strong password and remember it for later configuration steps.

Create ClickHouse system user and directories

Ensure proper ownership and permissions for ClickHouse data directories.

`sudo mkdir -p /var/lib/clickhouse /var/log/clickhouse-server /etc/clickhouse-server/conf.d
sudo chown clickhouse:clickhouse /var/lib/clickhouse /var/log/clickhouse-server
sudo chmod 755 /var/lib/clickhouse /var/log/clickhouse-server
sudo chmod 750 /etc/clickhouse-server`

Configure main server settings

Create the main configuration file with clustering and security settings.

`
`

Configure users and security

Set up user accounts with proper access controls and password authentication.

`
`

Generate password hashes for users

Create secure password hashes for your ClickHouse users and update the configuration.

`# Generate password hash for default user
echo -n 'your_secure_password' | sha256sum

# Generate password hash for analytics user  
echo -n 'analytics_password' | sha256sum

# Generate password hash for readonly user
echo -n 'readonly_password' | sha256sum`

Replace the YOUR_*_PASSWORD_HASH placeholders in users.xml with the generated hashes.

Configure clustering settings

Set up clustering configuration for distributed tables and high availability.

`
`
Note: Adjust the shard and replica numbers in macros section according to your node position in the cluster. Each node should have unique shard/replica identifiers.

Configure SSL/TLS encryption

Enable HTTPS and secure TCP connections with SSL certificates.

`# Generate self-signed certificates for testing (use proper CA certificates in production)
sudo mkdir -p /etc/clickhouse-server/certs
sudo openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
  -subj "/C=US/ST=State/L=City/O=Organization/CN=example.com" \
  -keyout /etc/clickhouse-server/certs/server.key \
  -out /etc/clickhouse-server/certs/server.crt

sudo chown -R clickhouse:clickhouse /etc/clickhouse-server/certs
sudo chmod 600 /etc/clickhouse-server/certs/server.key
sudo chmod 644 /etc/clickhouse-server/certs/server.crt`

Enable SSL in ClickHouse configuration

Configure HTTPS and secure TCP ports with SSL certificates.

`
`

Generate DH parameters for SSL

Create Diffie-Hellman parameters for enhanced SSL security.

`sudo openssl dhparam -out /etc/clickhouse-server/certs/dhparam.pem 2048
sudo chown clickhouse:clickhouse /etc/clickhouse-server/certs/dhparam.pem
sudo chmod 644 /etc/clickhouse-server/certs/dhparam.pem`

Configure firewall rules

Open necessary ports for ClickHouse cluster communication and client access.

`sudo ufw allow 8123/tcp comment 'ClickHouse HTTP'
sudo ufw allow 8443/tcp comment 'ClickHouse HTTPS'
sudo ufw allow 9000/tcp comment 'ClickHouse Native TCP'
sudo ufw allow 9440/tcp comment 'ClickHouse Secure TCP'
sudo ufw allow from 203.0.113.0/24 to any port 9009 comment 'ClickHouse Interserver HTTP'`
`sudo firewall-cmd --permanent --add-port=8123/tcp --add-port=8443/tcp --add-port=9000/tcp --add-port=9440/tcp
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port protocol="tcp" port="9009" accept'
sudo firewall-cmd --reload`

Start and enable ClickHouse service

Start the ClickHouse service and enable it to start automatically on boot.

`sudo systemctl enable clickhouse-server
sudo systemctl start clickhouse-server
sudo systemctl status clickhouse-server`

Create distributed tables

Set up distributed tables that span across your ClickHouse cluster for analytics workloads.

`# Connect to ClickHouse client
clickhouse-client --user default --password

# Create local table on each shard
CREATE TABLE events_local (
    event_id UInt64,
    user_id UInt32,
    event_time DateTime,
    event_type String,
    properties Map(String, String)
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(event_time)
ORDER BY (user_id, event_time)
SETTINGS index_granularity = 8192;

# Create distributed table
CREATE TABLE events_distributed AS events_local
ENGINE = Distributed(cluster_3shards_1replicas, default, events_local, rand());

# Create materialized view for real-time aggregations
CREATE MATERIALIZED VIEW events_hourly_mv
TO events_hourly
AS SELECT
    toStartOfHour(event_time) as hour,
    event_type,
    count() as event_count,
    uniq(user_id) as unique_users
FROM events_local
GROUP BY hour, event_type;`

Performance optimization

Configure memory and cache settings

Optimize ClickHouse performance with proper memory allocation and cache configuration.

`
`

Set up monitoring integration

Configure ClickHouse metrics exposure for monitoring systems like Prometheus.

`
`

You can integrate this with existing monitoring solutions like Grafana and Prometheus.

Backup and maintenance

Configure automated backups

Set up ClickHouse backup configuration for data protection and disaster recovery.

`
`

Create backup script

Create an automated backup script for regular data protection.

`#!/bin/bash
set -euo pipefail

BACKUP_DIR="/var/lib/clickhouse/backups"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="backup_${DATE}"
RETENTION_DAYS=7

# Create backup directory
mkdir -p "${BACKUP_DIR}/${BACKUP_NAME}"

# Backup ClickHouse data
echo "Starting ClickHouse backup at $(date)"
clickhouse-client --query "BACKUP DATABASE default TO Disk('disk', '${BACKUP_NAME}');"

# Backup configuration
tar -czf "${BACKUP_DIR}/${BACKUP_NAME}/config.tar.gz" -C /etc clickhouse-server

# Clean old backups
find "${BACKUP_DIR}" -type d -name "backup_*" -mtime +${RETENTION_DAYS} -exec rm -rf {} +

echo "Backup completed: ${BACKUP_NAME}"

# Optional: Upload to S3 or remote storage
`<h2>`aws s3 sync "${BACKUP_DIR}/${BACKUP_NAME}" s3://your-backup-bucket/clickhouse/${BACKUP_NAME}/`</h2>

Automated install script

Run this to automate the entire setup

Non vuoi gestirlo da solo?

Gestiamo l'infrastruttura di aziende che dipendono dall'uptime. Completamente gestita, con un referente fisso che conosce il tuo ambiente.

Avete un referente fisso che conosce il vostro ambiente

Alla scrivania a Rotterdam 14:58 · raggiungibile con un messaggio, senza modulo ticket