6/25/2024
InfluxDB 2.x — migrating from v1 and the new Flux queries
TL;DR: InfluxDB 2.x has a new query language (Flux instead of InfluxQL), a new data organization (buckets instead of databases), and a built-in Grafana (Chronograf). Migration from v1 takes about an hour. For new projects, always use v2 — v1 hasn’t received new features since 2021.
If you’re collecting telemetry data — temperature, voltages, solar power, humidity — InfluxDB is a natural tool. Version 2.x is incompatible with v1, but the differences are logical and worth the switch. Flux is more powerful than InfluxQL, the built-in UI eliminates the need for a separate Chronograf, and you can configure alerting without Grafana.
What changed
| Aspect | InfluxDB v1 | InfluxDB v2 |
|---|---|---|
| Data organization | database + retention policy | organization + bucket (retention built-in) |
| Query language | InfluxQL (SQL-like) | Flux (functional, pipe-based) |
| Visualization | Chronograf (separate service) | built-in UI on port 8086 |
| Authentication | username/password | API token |
| Data collection agent | Telegraf (external) | Telegraf (still external, but better integrated) |
| Alerting | Kapacitor (separate service) | built into v2 as Tasks |
The biggest change is Flux. For someone used to SELECT mean("temp") FROM "sensors" WHERE time > now() - 1h GROUP BY time(5m), switching to pipe-based syntax takes an hour or two. After that, Flux is far more flexible.
Migrating data from v1 to v2
For small datasets (< a few hundred MB): export from v1 using the influx_inspect tool, import to v2 with influx write.
# On the old v1 server
influx_inspect export -database sensors -out /tmp/sensors_export.lp
# On the new v2 server
influx write \
--bucket sensors \
--org my-org \
--token "your-token" \
--file /tmp/sensors_export.lp \
--format lp
The .lp (Line Protocol) format is compatible between versions — good news.
For large datasets (tens of GB), use the influxd upgrade tool, which converts data in place and generates a v2 config file based on your existing v1 configuration. The mapping: each database + retention policy becomes a separate bucket in v2.
Flux queries — examples
Basic query — average temperature over the last 24 hours, aggregated by the hour:
InfluxQL (v1):
SELECT mean("temperature") FROM "sensors"
WHERE time > now() - 24h
GROUP BY time(1h)
Flux (v2):
from(bucket: "sensors")
|> range(start: -24h)
|> filter(fn: (r) => r._measurement == "environment" and r._field == "temperature")
|> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
|> yield(name: "mean_temp")
Flux is more verbose, but has capabilities that InfluxQL lacks: joining two measurements, mathematical field transformations, regex tag filtering, custom functions. A more complex example — calculating solar installation efficiency:
solar = from(bucket: "iot")
|> range(start: -7d)
|> filter(fn: (r) => r._measurement == "solar" and r._field == "power_w")
theoretical = from(bucket: "iot")
|> range(start: -7d)
|> filter(fn: (r) => r._measurement == "solar" and r._field == "irradiance_w_m2")
join(tables: {solar: solar, theoretical: theoretical}, on: ["_time"])
|> map(fn: (r) => ({r with efficiency: r._value_solar / (r._value_theoretical * 0.01)}))
Grafana with InfluxDB 2.x
In Grafana, add a new data source of type “InfluxDB” and set the Query Language to “Flux” (not InfluxQL). Connection settings:
- URL:
http://192.168.10.200:8086 - Organization:
my-org - Token: token generated in the InfluxDB UI (API Tokens section)
- Default Bucket:
sensors
In Grafana panels, you write queries in Flux — the same language as directly in the InfluxDB UI. Debugging is easier because you can first test a query in the InfluxDB UI (which shows raw tabular data), then copy it to Grafana.
Is migration worth it
InfluxDB v1 hasn’t received new features since 2021, only security patches. In 2024, there’s no reason to use v1 for new projects. For existing installations: if it’s working and there are no performance issues, you can wait. But plan the migration — v1 support will eventually end.
Flux has a higher learning curve than InfluxQL, but Tasks (scheduled queries with alerting) built into v2 are a huge time-saver: you don’t need Kapacitor, you don’t need separate Grafana Alerting configured. An alert “if temperature > 80°C for 5 minutes, send an email” is 10 lines of Flux in the Tasks tab.
Summary
InfluxDB 2.x is a more mature platform: one service instead of three (InfluxDB + Chronograf + Kapacitor), token-based authentication, Flux as a more flexible query language. Migration from v1 is straightforward for small datasets and automated for large ones via influxd upgrade. For IoT and monitoring projects in 2024 — start with v2.