| # ClickHouse Database Connection Guide | |
| ## Connection Details | |
| - **Host**: localhost | |
| - **Port**: 9000 (HTTP), 9004 (HTTPS), 9009 (Native TCP) | |
| - **Protocol**: HTTP/TCP (ClickHouse native) | |
| - **Default Database**: default | |
| - **Health Check**: `clickhouse-client --query "SELECT version()"` | |
| ## Authentication | |
| - **Default User**: default (no password) | |
| - **Access**: Localhost only, no authentication required | |
| - **Security**: Development mode - add authentication for production | |
| ## ClickHouse-CLI Examples | |
| ```bash | |
| # Connect to ClickHouse | |
| clickhouse-client | |
| # Show databases | |
| SHOW DATABASES; | |
| # Create nova database | |
| CREATE DATABASE nova_analytics; | |
| # Basic query | |
| SELECT version(), now(), currentDatabase(); | |
| # Table operations | |
| CREATE TABLE nova_analytics.events ( | |
| timestamp DateTime, | |
| event_type String, | |
| data String | |
| ) ENGINE = MergeTree() | |
| ORDER BY timestamp; | |
| INSERT INTO nova_analytics.events VALUES | |
| (now(), 'session_start', '{"user": "test"}'); | |
| SELECT * FROM nova_analytics.events; | |
| ``` | |
| ## HTTP API Examples | |
| ```bash | |
| # Health check via HTTP | |
| curl "http://localhost:8123/?query=SELECT%20version()" | |
| # Create table via HTTP | |
| curl -X POST "http://localhost:8123/?query=CREATE%20TABLE%20test%20(id%20UInt32)%20ENGINE%20%3D%20Memory" | |
| # Insert data | |
| curl -X POST "http://localhost:8123/?query=INSERT%20INTO%20test%20VALUES%20(1)%2C%20(2)%2C%20(3)" | |
| # Query data | |
| curl "http://localhost:8123/?query=SELECT%20*%20FROM%20test" | |
| ``` | |
| ## Python Client Example | |
| ```python | |
| from clickhouse_driver import Client | |
| # Connect to ClickHouse | |
| client = Client( | |
| host='localhost', | |
| port=9000, | |
| user='default', | |
| database='default' | |
| ) | |
| # Execute queries | |
| version = client.execute('SELECT version()') | |
| print(f"ClickHouse version: {version[0][0]}") | |
| # Create analytics table | |
| client.execute(''' | |
| CREATE TABLE IF NOT EXISTS nova_analytics.metrics ( | |
| timestamp DateTime, | |
| metric_name String, | |
| value Float64, | |
| tags String | |
| ) ENGINE = MergeTree() | |
| ORDER BY (metric_name, timestamp) | |
| ''') | |
| # Insert metrics data | |
| client.execute(''' | |
| INSERT INTO nova_analytics.metrics VALUES | |
| (now(), 'memory_usage', 75.5, '{"host": "vast1"}'), | |
| (now(), 'cpu_usage', 45.2, '{"host": "vast1"}') | |
| ''') | |
| # Query analytics | |
| results = client.execute(''' | |
| SELECT | |
| metric_name, | |
| avg(value) as avg_value, | |
| max(timestamp) as last_seen | |
| FROM nova_analytics.metrics | |
| GROUP BY metric_name | |
| ''') | |
| for row in results: | |
| print(f"Metric: {row[0]}, Avg: {row[1]:.2f}, Last: {row[2]}") | |
| ``` | |
| ## Configuration Notes | |
| - **Data Directory**: `/data/data/clickhouse/data/` | |
| - **Log Directory**: `/data/data/clickhouse/logs/` | |
| - **Max Memory**: 50GB (configurable) | |
| - **Backup Location**: `/data/adaptai/backups/clickhouse/` | |
| - **Port Configuration**: | |
| - 8123: HTTP interface | |
| - 9000: Native TCP interface | |
| - 9004: HTTPS interface | |
| - 9009: Native TCP with SSL | |
| ## Performance Tuning | |
| ```sql | |
| -- Monitor system metrics | |
| SELECT * FROM system.metrics LIMIT 10; | |
| -- Check query performance | |
| SELECT | |
| query, | |
| elapsed, | |
| read_rows, | |
| memory_usage | |
| FROM system.processes; | |
| -- Table sizes and parts | |
| SELECT | |
| database, | |
| table, | |
| sum(bytes) as size_bytes | |
| FROM system.parts | |
| GROUP BY database, table | |
| ORDER BY size_bytes DESC; | |
| ``` | |
| ## Health Checks | |
| ```bash | |
| # Basic connectivity | |
| clickhouse-client --query "SELECT 1" | |
| # System health | |
| clickhouse-client --query "SELECT * FROM system.metrics WHERE metric LIKE '%memory%'" | |
| # Disk usage | |
| clickhouse-client --query " | |
| SELECT | |
| name, | |
| free_space, | |
| total_space, | |
| formatReadableSize(free_space) as free, | |
| formatReadableSize(total_space) as total | |
| FROM system.disks | |
| " | |
| # Active queries | |
| clickhouse-client --query "SELECT query, elapsed FROM system.processes" | |
| ``` | |
| ## Security | |
| - β Localhost binding only | |
| - β No authentication configured (development mode) | |
| - β Add password authentication for production | |
| - β Monitor disk usage on /data partition | |
| - β Regular backups recommended | |
| - β Consider enabling SSL for encrypted connections | |
| ## Backup Procedures | |
| ```bash | |
| # Create backup | |
| clickhouse-client --query "BACKUP DATABASE nova_analytics TO '/data/adaptai/backups/clickhouse/nova_analytics_backup'" | |
| # Restore backup | |
| clickhouse-client --query "RESTORE DATABASE nova_analytics FROM '/data/adaptai/backups/clickhouse/nova_analytics_backup'" | |
| # Manual file backup | |
| sudo rsync -av /data/data/clickhouse/data/ /data/adaptai/backups/clickhouse/full_backup/ | |
| ``` | |
| --- | |
| **Last Updated:** September 4, 2025 |