Installing InfluxDB on Linux: The Ultimate Solution for Time-series Data

Database tutorial - IT technology blog
Database tutorial - IT technology blog

The Problem: When Traditional SQL Falls Short for Sensor Data

When I first started a smart home project, I foolishly dumped all the temperature and humidity sensor data sent every 5 seconds into MySQL. After just 3 months, the data table hit 50 million rows, making every SELECT command for charting take over 20 seconds to respond. This is a classic case where analyzing MySQL slow query logs is necessary to identify the bottleneck. The server constantly reported disk I/O overload.

The problem is that SQL databases are designed for complex relationships. They weren’t built to “swallow” millions of time-stamped records per second. For processing billions of records in real-time, specialized time-series databases like InfluxDB are necessary. This tool offers incredible data compression, saving up to 90% of disk space compared to standard SQL.

Quickly Installing InfluxDB 2.x on Ubuntu/Debian

For system stability, I recommend using version 2.x. This version comes with a built-in Dashboard and the highly flexible Flux query engine.

1. Add the Official Repository

First, import the GPG key to ensure the installation package hasn’t been tampered with:

wget -q https://repos.influxdata.com/influxdata-archive_compat.key
echo "393e8772240a2ed510e964359196b001944813580536c4b268593649646487e4 influxdata-archive_compat.key" | sha256sum -c && cat influxdata-archive_compat.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg > /dev/null

echo "deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg] https://repos.influxdata.com/debian stable main" | sudo tee /etc/apt/sources.list.d/influxdata.list

2. Install and Enable the Service

Run the update command and install the influxdb2 package:

sudo apt-get update && sudo apt-get install influxdb2 -y
sudo systemctl start influxdb
sudo systemctl enable influxdb

3. Set Up the Administrator Account

Unlike version 1.x, version 2.x requires initialization via the CLI or Web interface. Run the following command:

influx setup

You need to enter a Username, Organization (e.g., LabHome), and your first Bucket. Don’t forget to save the Operator Token. If you lose this token, you won’t be able to connect your applications to the database later.

Thinking Correctly About Time-series Data Structures

Don’t just port your SQL Table/Row mindset over. In InfluxDB, performance depends on how you name your Tags and Fields:

  • Measurement: Think of it as the table name (e.g., air_quality).
  • Tag: Labels used to filter data (e.g., sensor_id="SN-001"). Tags are indexed, so queries are very fast.
  • Field: The actual values (e.g., pm25=15.2). Fields are NOT indexed.
  • Timestamp: The time axis, which is the primary key by default.

Important note: Avoid putting values with very high variance into Tags (like random user IDs). This “High Cardinality” error will cause InfluxDB to consume all available RAM and crash the server within minutes.

Managing Retention Policy: Don’t Let Your Hard Drive Explode

IoT data accumulates very quickly. If you store it forever, you’ll soon run out of resources. InfluxDB allows you to set up automatic deletion of old data through Bucket Retention.

Suppose you only want to keep system logs for 30 days:

influx bucket update --name system_logs --retention 720h

Once set up, InfluxDB handles the cleanup automatically in the background. You don’t need to write cronjob scripts or use pg_cron to manually delete data as you would with traditional databases.

Real-world Connection: Writing Data with Python

Here is how to push data from a Raspberry Pi to InfluxDB using the official library:

import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS

client = influxdb_client.InfluxDBClient(
    url="http://localhost:8086",
    token="YOUR_TOKEN",
    org="LabHome"
)

write_api = client.write_api(write_options=SYNCHRONOUS)

# Write data using Line Protocol
point = influxdb_client.Point("environment") \
  .tag("room", "bedroom") \
  .field("humidity", 65.0)

write_api.write(bucket="SensorData", record=point)

Pro-tips from the Field

Use it for the right purpose: Only use InfluxDB for metrics and logs. Information requiring strict relationships, like user profiles or orders, should still reside in PostgreSQL, where you can find and optimize resource-hungry SQL queries.

Leverage Telegraf: Instead of writing your own code to collect server metrics, use Telegraf. It’s a lightweight agent supporting over 200 plugins to push data to InfluxDB with just a few lines of configuration.

Monitor storage capacity: InfluxDB compresses data very well (often achieving a 10:1 ratio). However, always set an alert when the disk reaches 80% to avoid the database entering read-only mode.

Mastering InfluxDB is a smart move if you want to handle Big Data in IoT or Monitoring professionally while being resource-efficient. If you need to visualize this data later, consider a self-service BI solution to replace manual data extraction.

Share: