Master InfluxDB: From Basics to Advanced Queries and Retention Policies
This guide explains InfluxDB's architecture, data model, CRUD commands, measurement handling, retention policies, query syntax, time‑zone considerations, and service management, providing a comprehensive tutorial for developers working with time‑series databases.
What is InfluxDB
InfluxDB is an open‑source time‑series database developed by InfluxData, written in Go, designed for high‑performance storage and querying of time‑series data. It is widely used for system monitoring, IoT real‑time data, and similar scenarios.
Key Features
Single‑binary deployment without external dependencies, leveraging Go’s features.
SQL‑like query language with many built‑in functions.
Data model composed of measurements, tag sets, field sets and a timestamp.
Data Model Details
Measurement : a string that identifies the type of record, e.g. cpu_load or average_temperature.
Tag set : a schema‑free collection of key‑value pairs that are indexed by default.
Field set : a collection of key‑value pairs storing the actual values (int64, float64, string, boolean); fields are not indexed.
Timestamp : the time attribute of the record; if omitted, the server uses the write time.
Supports data insertion and queries over HTTP, TCP or UDP.
Retention Policies can be defined to automatically delete or down‑sample data older than a specified duration.
CRUD Operations
Enter the InfluxDB CLI: influx -precision rfc3339 Show databases: show databases Create database: create database shhnwangjian Drop database: drop database shhnwangjian Use database:
use shhnwangjianMeasurements (Tables) Operations
InfluxDB does not have explicit tables; measurements serve the same purpose.
Show all measurements: SHOW MEASUREMENTS Create measurement by inserting data, e.g.:
insert disk_free,hostname=server01 value=442221834240i
insert cpu_virtual_used_num,host=1 value=4 1556593150In the line above, disk_free is the measurement name, hostname is a tag, and value=… is a field. A custom timestamp can be appended.
insert disk_free,hostname=server01 value=442221834240i 1435362189575692182Drop measurement:
drop measurement disk_freeRetention Policies
Retention Policies (RP) define how long data is kept.
Show RPs: show retention policies on "db_name" Create RP:
create retention policy "rp_name" on "db_name" duration 3w replication 1 defaultAlter RP:
alter retention policy "rp_name" on "db_name" duration 30d defaultDrop RP: drop retention policy "rp_name" on "db_name" Parameters: duration (e.g., 3w, 1h), replication factor, and the default flag.
Querying Data
select * from cpu_virtual_used_numInserting Data
Inserting data also creates the measurement if it does not exist:
insert disk_free,hostname=server01 value=442221834240i
insert cpu_virtual_used_num,host=470b14f0-e869-43ed-a8e6-fd634258271f,hostname=server01 value=0.3 1557023160Deleting Data
Direct row deletion is not supported; data is removed by configuring a Retention Policy with a suitable duration.
Inspecting Measurement Schema
Show tag keys: show tag keys from cluster_metric Show field keys:
show field keys from cluster_metricTime‑zone Handling
InfluxDB stores timestamps in UTC. Queries can use different time formats: epoch_time (nanoseconds, microseconds, etc.) rfc3339_date_time_string – e.g., ‘YYYY‑MM‑DDTHH:MM:SS.nnnnnnnnnZ’ rfc3339_like_date_time_string – e.g., ‘YYYY‑MM‑DD HH:MM:SS.nnnnnnnnn’
Precision can be adjusted in queries, e.g., using seconds ( ... >= 1435333209s) or milliseconds ( ... >= 1435333209000ms).
To query in Beijing time, add the tz('Asia/Shanghai') clause:
select * from cpu_virtual_used_num where time >= '2018-11-23 14:30:39' and time <= '2019-11-23 14:32:32' tz('Asia/Shanghai')Service Management
sudo service influxdb start
service influxdb restart
exit // return to normal userSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
