0%

influxdb简单入门

这里我们介绍简单的influxdb安装和一些运行配置

项目地址

官网:https://influxdata.com/

下载:https://influxdata.com/downloads/

文档:https://docs.influxdata.com/influxdb/v0.10/

安装

ubuntu添加源:

1
2
3
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/lsb-release
echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list

安装命令:

1
sudo apt-get update && sudo apt-get install influxdb

启动脚本:

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

echo 'kill influxdb ...'
pkill influxdb
ps -ef|grep influxd

echo 'run inflxd ...'
nohup influxd -config influxdb.conf 2>&1 1>> influxdb.log &

ps -ef|grep influxd

echo 'end ...'

这里要注意一下配置文件influxdb.conf,可以通过系统生成一个配置文件:

influxd config > 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
reporting-disabled = true

[meta]
dir = "/srv/influxdb_home/meta"
hostname = "localhost"
bind-address = ":9190"
retention-autocreate = true
election-timeout = "1s"
heartbeat-timeout = "1s"
leader-lease-timeout = "500ms"
commit-timeout = "50ms"

[data]
dir = "/srv/influxdb_home/data"
retention-auto-create = true
retention-check-enabled = true
retention-check-period = "10m0s"
retention-create-period = "45m0s"
wal-dir = "/srv/influxdb_home/log"
wal-enable-logging = true

[cluster]
shard-writer-timeout = "5s"

[retention]
enabled = true
check-interval = "5m0s"

[shard-precreation]
enabled = true
check-interval = "10m0s"
advance-period = "30m0s"

[admin]
enabled = true
bind-address = ":9191"

[http]
enabled = true
bind-address = ":9192"
auth-enabled = true
log-enabled = true
write-tracing = false
pprof-enabled = false

[collectd]
enabled = false
bind-address = ":9193"
database = "collectd"
retention-policy = ""
batch-size = 5000
batch-timeout = "10s"
typesdb = "/srv/influxdb_home/collectd/types.db"

[opentsdb]
enabled = false
bind-address = ":9194"
database = "opentsdb"
retention-policy = ""
consistency-level = "one"

[[udp]]
enabled = false
bind-address = ""
database = ""
batch-size = 0
batch-timeout = "0"

[monitoring]
enabled = false
write-interval = "1m0s"

[continuous_queries]
enabled = true
recompute-previous-n = 2
recompute-no-older-than = "10m0s"
compute-runs-per-interval = 10
compute-no-more-than = "1m0s"

[hinted-handoff]
enabled = true
dir = "/srv/influxdb_home/hh"
max-size = 1073741824
max-age = "168h0m0s"
retry-rate-limit = 0
retry-interval = "1s"

各参数含义:https://docs.influxdata.com/influxdb/v0.10/administration/config/

cli操作

授权

先关掉权限启动,然后创建用户CREATE USER <username> WITH PASSWORD '<password>' WITH ALL PRIVILEGES,再开启权限验证,修改配置文件auth-enabled = false,设为true

统计

按照时间创建统计,每1分钟统计一次用户量

1
create continuous query min_5 on user_online_ begin select count(distinct userId) into min_5 from user_list group by time(1m) end

上面表示在databases:user_online_上面(统计该数据库的表),统计measurement:user_list上面字段userId的去重总量,放到measurement:min_5(这里可以是其他数据库databases)里面去,这个continuous名字为min_5

查看:SHOW CONTINUOUS QUERIES

数据库有效期

可以给表创建一个有效期时间,一旦数据超过指定时间会被influxdb删除,下面创建一个2hRETENTION POLICY:

1
CREATE RETENTION POLICY two_hours ON food_data DURATION 2h REPLICATION 1 DEFAULT

之后就可以使用two_hours,需要查看数据库的:SHOW RETENTION POLICIES ON db

create continuous query count_min_30 on a8_client begin select count(distinct userId) into user_online.”default”.min_30 from user_list group by time(30m) end