Introduction
InfluxDB 2.x is the go-to solution for storing and analyzing large-scale time series data. Whether for monitoring, IoT, or application metrics, its ability to ingest millions of points per second combined with the Flux language provides unmatched power. This tutorial walks you through a professional deployment from installation to writing optimized queries. You will learn how to structure your data, secure access, and avoid common performance pitfalls.
Prerequisites
- Docker and Docker Compose installed
- Basic command line knowledge
- Understanding of time series concepts (timestamp, tags, fields)
- Node.js 20+ (for client examples)
Deployment with Docker Compose
version: '3.8'
services:
influxdb:
image: influxdb:2.7
container_name: influxdb
ports:
- "8086:8086"
volumes:
- influxdb-data:/var/lib/influxdb2
- influxdb-config:/etc/influxdb2
environment:
- DOCKER_INFLUXDB_INIT_MODE=setup
- DOCKER_INFLUXDB_INIT_USERNAME=admin
- DOCKER_INFLUXDB_INIT_PASSWORD=StrongPassword123!
- DOCKER_INFLUXDB_INIT_ORG=learni
- DOCKER_INFLUXDB_INIT_BUCKET=metrics
- DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=mon-token-super-secret-2026
volumes:
influxdb-data:
influxdb-config:This file deploys InfluxDB 2.7 in initialized mode with a bucket and token. Volumes persist data and configuration across restarts.
Bucket Configuration and Retention
docker compose up -d
# Verification
curl -s http://localhost:8086/healthThe container starts and automatically creates the organization, bucket, and token. Verify that the service responds with a healthy status.
Writing Data via the API
curl -XPOST "http://localhost:8086/api/v2/write?org=learni&bucket=metrics&precision=s" \
--header "Authorization: Token mon-token-super-secret-2026" \
--data-raw "cpu,host=server01,region=eu usage=23.5 1700000000"Send a point with tags and a field. The timestamp is in seconds. Always use tags for filterable metadata and fields for measured values.
Simple Flux Query
from(bucket: "metrics")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "cpu")
|> filter(fn: (r) => r.host == "server01")
|> aggregateWindow(every: 5m, fn: mean)This query retrieves the mean every 5 minutes over the last hour. Flux is functional and enables powerful chaining of transformations.
Node.js Client for Writing
import { InfluxDB, Point } from '@influxdata/influxdb-client';
const client = new InfluxDB({ url: 'http://localhost:8086', token: 'mon-token-super-secret-2026' });
const writeApi = client.getWriteApi('learni', 'metrics', 's');
const point = new Point('cpu')
.tag('host', 'server01')
.tag('region', 'eu')
.floatField('usage', 23.5);
writeApi.writePoint(point);
writeApi.close().then(() => console.log('Données écrites'));The official client handles batching and retries. Always call close() to flush the buffer before the script ends.
Best Practices
- Use tags for filtering dimensions (host, region) and fields for values
- Set retention policies that match your needs (7d, 30d, 1y)
- Index your most filtered tags by creating separate measurements when necessary
- Avoid excessively high cardinality on tags
- Enable LZ4 compression for large volumes
Common Errors to Avoid
- Forgetting to close writeApi (data lost in memory)
- Using nanosecond timestamps without correct precision
- Running queries without range() (empty results)
- Storing values in tags instead of fields
Going Further
Explore our advanced training on time series databases and observability: https://learni-group.com/formations