influxdb basic

influxdb v2 install

docker compose yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# compose.yaml
services:
  influxdb2:
    image: influxdb:2.7.10-alpine
    ports:
      - 8086:8086
    environment:
      DOCKER_INFLUXDB_INIT_MODE: setup
      DOCKER_INFLUXDB_INIT_USERNAME_FILE: /run/secrets/influxdb2-admin-username
      DOCKER_INFLUXDB_INIT_PASSWORD_FILE: /run/secrets/influxdb2-admin-password 
      DOCKER_INFLUXDB_INIT_ADMIN_TOKEN_FILE: /run/secrets/influxdb2-admin-token
      DOCKER_INFLUXDB_INIT_ORG: docs 
      DOCKER_INFLUXDB_INIT_BUCKET: home
    secrets:
      - influxdb2-admin-username
      - influxdb2-admin-password
      - influxdb2-admin-token
    volumes:
      - type: volume
        source: influxdb2-data
        target: /var/lib/influxdb2
      - type: volume
        source: influxdb2-config
        target: /etc/influxdb2
secrets:
  influxdb2-admin-username:
    file: ~/.env.influxdb2-admin-username
  influxdb2-admin-password:
    file: ~/.env.influxdb2-admin-password
  influxdb2-admin-token:
    file: ~/.env.influxdb2-admin-token
volumes:
  influxdb2-data:
  influxdb2-config:
1
sudo docker compose -f influxdb-compose.yaml up influxdb2

influxdb http api

1
2
3
4
5
# http api
curl -G 'http://localhost:8086/query?pretty=true'  --data-urlencode "q=show databases"
curl -i -XPOST http://localhost:8086/query --data-urlencode "db=mydb" --data-urlencode "q=CREATE DATABASE db0"

curl -i -XPOST "http://192.168.97.1:8086/write?db=monitor" --data-binary 'measurement_0,location=us-midwest temperature=82 1594349970000000000'
1
curl -x http://127.0.0.1:8899/ -i -XPOST "http://192.168.97.1:8086/write?db=monitor" --data-binary 'measurement_0,location=us-midwest temperature=86 1594349970000000000'

database management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#show db
show databases

# 创建数据库,同时配置retention policy
# DURATION: 数据生命周期 30天
# SHARD DURATION: 分片周期 1小时
# NAME: retention policies 名
CREATE DATABASE "database0" WITH DURATION 30d REPLICATION 1 SHARD DURATION 1h NAME "default"

# create db
# 数据库名不能包含"-", 可以用"_"
create database db0
drop database db0

insert

1
2
3
4
5
6
-- float value
INSERT measurement0,tag0=tag_value0 field0=1,field1=field_value1
-- int value
INSERT measurement0,tag0=tag_value0 field0=1i,field1=field_value1
-- with time
INSERT measurement0,tag0=tag_value0 field0=1,field1=field_value1 1570611600000000000

delete

1
DELETE FROM measurement0 WHERE tag0=tag_value0

导出

1
2
3
influx -database 'db0' -execute "select field0,\"field1\" from measurement0 where tag-name0='tag-value0' and time>'2018-05-05 02:00:00' and time<'2018-05-07 11:00:00' order by time tz('Etc/GMT-8')" -format 'csv' -precision 'rfc3339' > xxx.csv

# 使用 tz('Etc/GMT-8') ,过滤条件中的time>'xxx' 填写东8区时间.

安装, influxdb install & config

dnf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
sudo tee  /etc/yum.repos.d/influxdb.repo<<EOF
[influxdb]
name = InfluxDB Repository
baseurl = https://repos.influxdata.com/rhel/7/x86_64/stable/
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdb.key
EOF
dnf install influxdb
vim /etc/influxdb/influxdb.conf
systemctl enable influxdb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# install
# archlinux
yay -S influxdb

# centos
# https://www.influxdata.com/blog/package-repository-for-linux/
sudo yum install influxdb
sudo yum localinstall influxdb-1.2.4.x86_64.rpm

# for Linux Binaries (64-bit)
tar xvfz influxdb-1.3.1_linux_amd64.tar.gz
rsync -r /path/to/influxdb-1.2.4-1/ /

#edit config file
emacs /etc/influxdb/influxdb.conf

/etc/influxdb/influxdb.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#reporting-disabled = false
[meta]

dir = "/var/lib/influxdb/influxdb/meta"

#retention-autocreate = true
[data]

dir = "/var/lib/influxdb/influxdb/data"
wal-dir = "/var/lib/influxdb/influxdb/wal"
wal-fsync-delay = "100ms"

# index-version = "inmem"

index-version = "tsi1"

trace-logging-enabled = false
    
query-log-enabled = true
    
cache-max-memory-size = "512m"
    
cache-snapshot-memory-size = "32m"

# 超过10分钟没有写入, 把cache写到新的TSM文件

cache-snapshot-write-cold-duration = "10m"

[coordinator]

#慢查询
log-queries-after = "10s"

[retention]

#edit file /etc/default/influxdb
    
STDERR=/data/logs/influxdb/influxdb.log

#edit logrotate config, modify log path
    
/etc/logrotate.d/influxdb
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# chown
chown influxdb:influxdb /data/influxdb/
chown influxdb:influxdb /data/logs/influxdb/

#start
systemctl start influxdb
#or
sudo influxd

#connect via cli, rfc3339: 日期格式YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ
influx -precision rfc3339

docker

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
docker pull influxdb:2.7.10-alpine

docker run -d \
    --name influxdb \
    -p 8086:8086 \
    -v "influxdb-data:/var/lib/influxdb2" \
    -v "influxdb-config:/etc/influxdb2" \
    influxdb:2.7.10-alpine


# docker
docker run -d \
--name influxdb \
--net net0 \
--ip 192.168.1.xxx \
-p 8086:8086 \
-p 8083:8083 \
-v influxdb-config:/etc/influxdb:ro \
-v influxdb-storage:/var/lib/influxdb \
-v /etc/localtime:/etc/localtime:ro \
influxdb

# podman
podman run -d \
--name influxdb \
-p 8086:8086 \
-p 8083:8083 \
-p 25826:25826 \
-v influxdb-config:/etc/influxdb:ro \
-v influxdb-storage:/var/lib/influxdb \
-v /etc/localtime:/etc/localtime:ro \
influxdb:1.8.10-alpine

# in pod
podman run -d \
--name influxdb \
--pod monitor \
-v influxdb-config:/etc/influxdb:ro \
-v influxdb-storage:/var/lib/influxdb \
-v /etc/localtime:/etc/localtime:ro \
influxdb

chronograf

1
2
3
4
5
6
podman run -d \
--name chronograf \
--pod monitor \
-v chronograf:/var/lib/chronograf \
-v /etc/localtime:/etc/localtime:ro \
chronograf --influxdb-url=http://monitor:8086

run influx

1
2
sudo podman exec -it influxdb influx
sudo podman run -it --rm influxdb influx -host influxdb.wiloon.com

retention policies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
show retention policies
show retention policies on db0
CREATE RETENTION POLICY "default" ON db0 DURATION 30d REPLICATION 1 SHARD DURATION 1d DEFAULT

# ALTER  RETENTION POLICY "<policy name>" ON <database> DURATION <duration> REPLICATION 1 SHARD DURATION <shard group duration> DEFAULT
ALTER  RETENTION POLICY "default" ON db0 DURATION 3h REPLICATION 1 SHARD DURATION 1h DEFAULT

# policy name: retention policy 名: default 
# database: 库名
# duration 3h: 保留3个小时的数据
# shard group duration 1h: 每1个小时的数据一个分片
# DEFAULT: 设置此策略为默认策略

shard

list shard id

1
2
show shards
DROP SHARD <shard_id_number>

measurement

1
2
3
show measurements
DROP MEASUREMENT <measurement_name>
DROP MEASUREMENT "kernel"

select

1
2
3
4
5
6
7
8
9
select * from ping where time > now()-1s

select average_response_ms from ping where time > now()-1s and url='192.168.53.8'

select "database",id,retentionPolicy,seriesCreate,writeReq from "shard" WHERE time>now()-20s AND "database"='database0' AND retentionPolicy='default' AND writeReq>0

select * from "database0"."rentention-policies-0"."measurement0"
sELECT mean(m1) * 10 FROM metric0."default".m0 WHERE time >= now() - 10m  AND host='host0'  GROUP BY time(10s), host

influx

1
    influx -execute 'select * from "database0"."retention_policies_0"."measurement0" order by time desc limit 1'
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# show tag keys
SHOW TAG KEYS [ON <database_name>] [FROM_clause] [WHERE <tag_key> <operator> ['<tag_value>' | <regular_expression>]] [LIMIT_clause] [OFFSET_clause]

show tag keys
show tag keys on db0 from measurements0
show tag values on db0 from measure0 with key="host"

SHOW FIELD KEYS [ON <database_name>] [FROM <measurement_name>]
show tag values from cpu with key=host where service_name=~/xxx/

influx -precision rfc3339


select * from m0 where tag0='tag-value0' and time > '2018-05-16 13:00:00' and time < '2018-05-16 13:01:00' tz('Etc/GMT-8')


#use
use db0

drop series from series_name

select f0,f1  from s0 where t0=~/xxx.*/ 
ERR: error parsing query: found /, expected regex at line 1, char 56

# =~和后面的正则表达式之间要有空格T_T
select f0,f1  from s0 where t0=~ /xxx.*/ 

协议, 整数, 浮点数

浮点数 —— 默认是浮点数,InfluxDB假定收到的所有field value都是浮点数。 以浮点类型存储上面的82:

weather,location=us-midwest temperature=82 1465839830100400200 整数 —— 添加一个i在field之后,告诉InfluxDB以整数类型存储: 以整数类型存储上面的82:

weather,location=us-midwest temperature=82i 1465839830100400200 字符串 —— 双引号把字段值引起来表示字符串: 以字符串类型存储值too warm:

weather,location=us-midwest temperature="too warm” 1465839830100400200 布尔型 —— 表示TRUE可以用t,T,true,True,TRUE;表示FALSE可以用f,F,false,False或者FALSE: 以布尔类型存储值true:

weather,location=us-midwest too_hot=true 1465839830100400200