0%

InfluxDB-安装部署

前言

  • 转载自

https://wsgzao.github.io/post/influxdb/

InfluxDB 是专为时序数据设计的数据库,能支撑大量的读写负载,是一个高性能的时序数据 datastore。

InfluxDB is the Time Series Database in the TICK Stack


扩展阅读

InfluxDB - https://www.influxdata.com/time-series-platform/influxdb/


InfluxDB 简介

InfluxDB is the Time Series Database in the TICK Stack

InfluxData’s TICK Stack is built around InfluxDB to handle massive amounts of time-stamped information. This time series database provides support for your metrics analysis needs, from DevOps Monitoring, IoT Sensor data, and Real-Time Analytics. Users can adapt their SQL skills with InfluxQL, so they can get up to speed on this time series database.

默认预留端口:

8086,HTTP API
8088,RPC 端口,用于备份和恢复

NTP 服务:
InfluxDB 使用机器本地时间作为 timestamp,需要机器之间使用 NTP 进行同步;如果没有同步的话,写入的时间序列数据可能会不准确

InfluxDB 安装

InfluxDB 里存储的数据被称为时间序列数据, InfluxDB 存储方式跟传统关系型数据库不同的是:传统关系型数据库通过数据库 + 表 + 字段组织数据,InfluxDB 通过指标、标签、字段组织数据,时间戳是默认的索引列,标签跟字段其实就相当于关系型数据库中的字段,只不过标签会被索引,而字段不会。

Grafana 默认支持的数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch
Grafana 支持同时绑定多套数据源,根据自己需求管理即可,这里以 InfluxDB 为例。

https://portal.influxdata.com/downloads

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
# install influxdb
cat > /etc/yum.repos.d/influxdb.repo << 'EOF'
[influxdb]
name = InfluxDB Repository - RHEL \$releasever
baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdb.key
EOF

# yum install influxdb
yum install -y influxdb

# start and enable influxdb
sudo systemctl start influxdb
sudo systemctl enable influxdb
sudo systemctl status influxdb

# 通过 influx 命令进入 cli 命令行
influx
Connected to http://localhost:8086 version 1.4.2
InfluxDB shell version: 1.4.2
>

# 查看用户
SHOW USERS

# 创建用户
CREATE USER influx WITH PASSWORD 'influx' WITH ALL PRIVILEGES

# 查看用户
SHOW USERS

# 创建数据库
CREATE DATABASE test

# 查看数据库
SHOW DATABASES

# Using 数据库
USE test

# 插入数据
INSERT cpu,host=192.168.1.1 load=0.1,usage=0.2

# 查询所有数据
SELECT * FROM "cpu"
SELECT "host","load","usage" FROM "cpu"

# 根据条件查询
SELECT "host","load","usage" FROM "cpu" WHERE "host" = '192.168.1.1'
SELECT "host","load","usage" FROM "cpu" WHERE "usage" > 0.1

# 创建数据库
CREATE DATABASE "db_name"
# 显示所有数据库
SHOW DATABASES
# 删除数据库
DROP DATABASE "db_name"

# 使用数据库
USE mydb
# 显示该数据库中的表
SHOW MEASUREMENTS
# 删除表
DROP MEASUREMENT "t_name"

# 创建数据库 API
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE test"

# 写入数据 API

# 写入单条
curl -i -XPOST http://localhost:8086/write?db=test --data-binary "cpu,host=192.168.1.3 load=0.1,usage=0.33"
curl -i -XPOST http://localhost:8086/write?db=test --data-binary "cpu,host=192.168.1.3 load=0.1,usage=0.33 6666666666666666666"

# 写入多条
curl -i -XPOST http://localhost:8086/write?db=test --data-binary "cpu,host=192.168.1.2 load=0.1,usage=0.22 1666666666666666661
cpu,host=192.168.1.3 load=0.1,usage=0.33 1666666666666666661
cpu,host=192.168.1.2 load=0.2,usage=0.22 1666666666666666662
cpu,host=192.168.1.3 load=0.2,usage=0.33 1666666666666666662"

# 查询数据 API
curl -G http://localhost:8086/query?db=test --data-urlencode "q=SELECT * FROM \"cpu\""

InfluxDB 配置优化

配置文件默认全部注释掉,使用默认的配置项,可以根据需要配置。每个配置项有对应的、相同功能的环境变量。

配置文件:/etc/influxdb/influxdb.conf

查看配置:influxd config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[meta]                                # 元数据 
dir = /var/lib/influxdb/meta

[data] # 数据 / WAL
dir = /var/lib/influxdb/data
wal-dir = /var/lib/influxdb/wal
cache-max-memory-size = "1g" # Cache 最大可用内存

[coordinator] # 查询相关
query-timeout = "0s" # 查询最大执行时间
log-queries-after = "0s" # 打印慢查询

[http] # HTTP 服务
auth-enabled = false # 启用安全认证
max-connection-limit = 0 # 最大连接数

更多帮助信息请参考官网
InfluxDB documentation - https://docs.influxdata.com/platform/introduction