0%

influxdb_learn_init

端口信息

端口
8083 Web admin
8086 http api
8088 集群端口

概念

database 数据库
measurement 表
points 表中的一行数据

points 由time、field、tags组成
time 时间戳,数据库中的主索引,会自动生成
field 列(字段)
tags 相比field有索引
series 数据集合

retention policy(RP) 保留策略

points 格式

1
<measurement>[,<tag-key>=<tag-value>...] <field-key>=<field-value>[,<field2-key>=<field2-value>...] [unix-nano-timestamp]

基本命令

influx 默认连接的本地8086端口

1
influx -precision rfc3339

查看有哪些数据库

1
SHOW DATABASES

_internal是安装InfluxDB时自动创建的数据库,用来存储一些运行时的指标数据

创建数据库

1
CREATE DATABASE mydb

切换数据库

1
USE mydb

插入数据

1
INSERT cpu,host=serverA,region=us_west value=0.64

查看有哪些表(MEASUREMENTS)

1
SHOW MEASUREMENTS

查询

1
SELECT * FROM cpu LIMIT 1
1
SHOW SERIES FROM cpu

查询schema相关

1
2
3
4
5
6
7
8
SHOW DATABASES
SHOW MEASUREMENTS
SHOW FIELD KEYS
SHOW TAG KEYS
SHOW TAG VALUES
SHOW SERIES
SHOW RETENTION POLICIES
Filter meta queries by time

数据库管理相关

1
2
3
4
5
6
CREATE DATABASE
DROP DATABASE
DROP SERIES
DELETE
DROP MEASUREMENT
DROP SHARD

Retention Policy(RP) 保留策略相关

语法

1
2
3
CREATE RETENTION POLICY <retention_policy_name> ON <database_name> DURATION <duration> REPLICATION <n> [SHARD DURATION <duration>] [DEFAULT]
ALTER RETENTION POLICY <retention_policy_name> ON <database_name> DURATION <duration> REPLICATION <n> SHARD DURATION <duration> DEFAULT
DROP RETENTION POLICY <retention_policy_name> ON <database_name>

DURATION: 数据保存时间,最小1 hour,最大无限INF(infinite)
REPLICATION: 副本数量,单实例不起作用

示例

1
2
3
CREATE RETENTION POLICY "one_day_only" ON "NOAA_water_database" DURATION 1d REPLICATION 1
ALTER RETENTION POLICY "what_is_time" ON "NOAA_water_database" DURATION 3w SHARD DURATION 2h DEFAULT
DROP RETENTION POLICY "what_is_time" ON "NOAA_water_database"

Continuous queries(CQ) 连续查询相关

管理相关

1
2
SHOW CONTINUOUS QUERIES
DROP CONTINUOUS QUERY <cq_name> ON <database_name>

alter未提供,需先DROP再CREATE

基本语法

1
2
3
4
CREATE CONTINUOUS QUERY <cq_name> ON <database_name>
BEGIN
<cq_query>
END

cq_query要求1个函数、1个INTO语句、1个GROUP BY time()语句

1
SELECT <function[s]> INTO <destination_measurement> FROM <measurement> [WHERE <stuff>] GROUP BY time(<interval>)[,<tag_key[s]>]

示例

1
2
3
4
CREATE CONTINUOUS QUERY "cq_basic" ON "transportation"
BEGIN
SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(1h)
END

高级语法 - 定时

1
2
3
4
5
CREATE CONTINUOUS QUERY <cq_name> ON <database_name>
RESAMPLE EVERY <interval> FOR <interval>
BEGIN
<cq_query>
END

示例,每30m执行一次

1
2
3
4
5
CREATE CONTINUOUS QUERY "cq_advanced_every_for" ON "transportation"
RESAMPLE EVERY 1h FOR 90m
BEGIN
SELECT mean("passengers") INTO "average_passengers" FROM "bus_data" GROUP BY time(30m)
END

写入非默认RP需指定,例如

1
INTO "one_day_only"."average_passengers"

示例

1
CREATE RETENTION POLICY 'one_day_only' ON mydb DURATION 1d REPLICATION 1
1
CREATE CONTINUOUS QUERY "cq_advanced_every_for" ON "mydb" RESAMPLE EVERY 1h FOR 90m BEGIN SELECT mean("value") INTO "one_day_only"."average_cpu" FROM "cpu" GROUP BY time(30m) END

权限

创建管理员

1
CREATE USER admin WITH PASSWORD 'testtest' WITH ALL PRIVILEGES

创建普通

1
CREATE USER telegraf WITH PASSWORD 'testtest'

授权

1
GRANT ALL on telegraf TO telegraf