Install and configure ClickHouse for high-performance analytics with clustering

Intermediate 45 min Apr 01, 2026 44 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.



    
        information
        /var/log/clickhouse-server/clickhouse-server.log
        /var/log/clickhouse-server/clickhouse-server.err.log
        100M
        10
    
    
    8123
    9000
    9004
    9005
    
    ::
    
    4096
    3
    100
    8589934592
    5368709120
    
    /var/lib/clickhouse/
    /var/lib/clickhouse/tmp/
    /var/lib/clickhouse/user_files/
    /var/lib/clickhouse/format_schemas/
    
    users.xml
    default
    default
    
    UTC
    022
    
    false
    
    
    
    
    
    3600
    3600
    60

Configure users and security

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



    
        
            10000000000
            0
            random
            268435456
            500000
            500000
            0
        
        
        
            1
            5000000000
            0
            random
        
    
    
    
        
            YOUR_SHA256_PASSWORD_HASH
            
                ::1
                127.0.0.1
                10.0.0.0/8
                172.16.0.0/12
                192.168.0.0/16
            
            default
            default
        
        
        
            YOUR_ANALYTICS_PASSWORD_HASH
            
                10.0.0.0/8
                172.16.0.0/12
                192.168.0.0/16
            
            default
            default
        
        
        
            YOUR_READONLY_PASSWORD_HASH
            
                0.0.0.0/0
            
            readonly
            default
        
    
    
    
        
            
                3600
                0
                0
                0
                0
                0
            
        
    

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.



    
        
            
                
                    203.0.113.10
                    9000
                    default
                    your_secure_password
                
            
            
                
                    203.0.113.11
                    9000
                    default
                    your_secure_password
                
            
            
                
                    203.0.113.12
                    9000
                    default
                    your_secure_password
                
            
        
        
        
            
                
                    203.0.113.10
                    9000
                
                
                    203.0.113.11
                    9000
                
            
            
                
                    203.0.113.12
                    9000
                
                
                    203.0.113.13
                    9000
                
            
        
    
    
    
        cluster_3shards_1replicas
        01
        replica_1
    
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.



    8443
    9440
    
    
        
            /etc/clickhouse-server/certs/server.crt
            /etc/clickhouse-server/certs/server.key
            /etc/clickhouse-server/certs/dhparam.pem
            none
            true
            true
            sslv2,sslv3
            true
        
        
        
            true
            true
            sslv2,sslv3
            true
            none
            
                RejectCertificateHandler
            
        
    

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.



    
    0.8
    20000000000
    
    
    8589934592
    5368709120
    134217728
    
    
    0
    100
    16
    2
    
    
    
        
            lz4
        
    

Set up monitoring integration

Configure ClickHouse metrics exposure for monitoring systems like Prometheus.



    
        /metrics
        9363
        true
        true
        true
        true
    
    
    
        system
        query_log
toYYYYMM(event_date) 7500

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.



    
        
            local
            /var/lib/clickhouse/backups/
        
        
            s3
            https://s3.amazonaws.com
            YOUR_ACCESS_KEY
            YOUR_SECRET_KEY
            clickhouse-backups
            cluster-backups/
        
    

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

aws s3 sync "${BACKUP_DIR}/${BACKUP_NAME}" s3://your-backup-bucket/clickhouse/${BACKUP_NAME}/

Set backup permissions and schedule

Configure proper permissions for the backup script and schedule regular backups.

sudo chmod +x /usr/local/bin/clickhouse-backup.sh
sudo mkdir -p /var/lib/clickhouse/backups
sudo chown clickhouse:clickhouse /var/lib/clickhouse/backups
sudo chmod 755 /var/lib/clickhouse/backups

Add to crontab for daily backups at 2 AM

echo "0 2 * /usr/local/bin/clickhouse-backup.sh >> /var/log/clickhouse-backup.log 2>&1" | sudo crontab -u clickhouse -
Never use chmod 777. It gives every user on the system full access to your files. Instead, use specific ownership with chown and minimal permissions like 755 for directories and 644 for files.

Verify your setup

# Check ClickHouse service status
sudo systemctl status clickhouse-server

Test client connection

clickhouse-client --query "SELECT version()"

Test HTTPS endpoint

curl -k https://localhost:8443/

Check cluster configuration

clickhouse-client --query "SELECT * FROM system.clusters"

Verify distributed tables

clickhouse-client --query "SHOW TABLES"

Test insert and query on distributed table

clickhouse-client --query "INSERT INTO events_distributed VALUES (1, 100, now(), 'click', {'page': '/home', 'referrer': 'google'})" clickhouse-client --query "SELECT * FROM events_distributed LIMIT 5"

Check system metrics

clickhouse-client --query "SELECT * FROM system.metrics WHERE metric LIKE '%Connection%' LIMIT 10"

Common issues

SymptomCauseFix
Service fails to startConfiguration syntax errorsudo clickhouse-server --config-file=/etc/clickhouse-server/config.xml --daemon=false to check config
Connection refused on port 8123Firewall blocking connectionsCheck firewall rules and ensure port 8123 is open
Permission denied accessing data directoryIncorrect ownership or permissionssudo chown -R clickhouse:clickhouse /var/lib/clickhouse && sudo chmod 755 /var/lib/clickhouse
SSL certificate errorsInvalid or expired certificatesRegenerate certificates or check certificate paths in SSL configuration
Cluster nodes cannot connectNetwork configuration or authenticationVerify network connectivity and user credentials in cluster configuration
High memory usageInadequate memory limitsAdjust max_memory_usage and max_server_memory_usage_to_ram_ratio in configuration
Slow query performanceMissing indexes or poor table designAdd appropriate ORDER BY clauses and partition keys, check with EXPLAIN

Next steps

Automated install script

Run this to automate the entire setup

#clickhouse #olap-database #analytics-database #clickhouse-cluster #real-time-analytics #columnar-database #clickhouse-ssl #distributed-tables #clickhouse-backup

Need help?

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.

Talk to an engineer