普拉多VX

人生一路,不问来时,不知归期

0%

Influxdb for Centos 7 安装部署

Influxdb介绍

InfluxDB是一个时间序列数据库,旨在处理较高的写入和查询负载。它是TICK Stack(Telegraf,InfluxDB,Chronograf,Kapacitor)的开源时间序列数据库组件。旨在处理高写入和查询负载,并提供一种称为InfluxQL的类似于SQL的查询语言,用于与数据进行交互。目前有开源版和企业版本,具体费用及价格参照官网https://www.influxdata.com/products/influxdb-overview/

主要特性有:

  • 内置HTTP接口,使用方便
  • 数据可以打标记,这样查询可以很灵活
  • 类SQL的查询语句
  • 安装管理很简单,并且读写数据很高效
  • 能够实时查询,数据在写入时被索引后就能够被立即查出
  • ……

安装

安装环境系统为 Centos7

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

选择1.8.3
后会弹出如下安装方式

2.安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@iZ2zecgq3cou36re3sxh4bZ ~]# wget https://dl.influxdata.com/influxdb/releases/influxdb-1.8.3.x86_64.rpm  # 下载
sudo yum localinstall influxdb-1.8.3.x86_64.rpm--2020-10-10 10:14:53-- https://dl.influxdata.com/influxdb/releases/influxdb-1.8.3.x86_64.rpm
Resolving dl.influxdata.com (dl.influxdata.com)... 13.227.75.119, 13.227.75.14, 13.227.75.22, ...
Connecting to dl.influxdata.com (dl.influxdata.com)|13.227.75.119|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 64097226 (61M) [application/octet-stream]
Saving to: ‘influxdb-1.8.3.x86_64.rpm’

100%[===========================================================================================================================>] 64,097,226 2.62MB/s in 27s

2020-10-10 10:15:20 (2.28 MB/s) - ‘influxdb-1.8.3.x86_64.rpm’ saved [64097226/64097226]

[root@iZ2zecgq3cou36re3sxh4bZ ~]# sudo yum localinstall influxdb-1.8.3.x86_64.rpm # 安装
[root@iZ2zecgq3cou36re3sxh4bZ ~]#

网络端口

默认情况下,InfluxDB使用以下端口

  • TCP端口8086通过InfluxDB HTTP API进行客户端-服务器通信。
  • TCP端口8088可用于RPC服务执行备份和还原操作。

生成文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/etc/influxdb/influxdb.conf    # 配置文件
/etc/logrotate.d/influxdb # 日志文件轮询配置
/usr/bin/influx # cli 命令
/usr/bin/influx_inspect
/usr/bin/influx_stress
/usr/bin/influx_tsm
/usr/bin/influxd # 二进制文件
/usr/lib/influxdb/scripts/influxdb.service # 开机启动文件
/usr/lib/influxdb/scripts/init.sh
/usr/share/man/man1/influx.1.gz
/usr/share/man/man1/influx_inspect.1.gz
/usr/share/man/man1/influx_stress.1.gz
/usr/share/man/man1/influx_tsm.1.gz
/usr/share/man/man1/influxd-backup.1.gz
/usr/share/man/man1/influxd-config.1.gz
/usr/share/man/man1/influxd-restore.1.gz
/usr/share/man/man1/influxd-run.1.gz
/usr/share/man/man1/influxd-version.1.gz
/usr/share/man/man1/influxd.1.gz
/var/lib/influxdb # 库文件
/var/log/influxdb # 日志存放路径

配置文件

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
[meta]
# Where the metadata/raft database is stored # 元数据存放路径
dir = "/var/lib/influxdb/meta"

# Automatically create a default retention policy when creating a database.
# retention-autocreate = true

# If log messages are printed for the meta service
# logging-enabled = true

###
### [data]
###
### Controls where the actual shard data for InfluxDB lives and how it is
### flushed from the WAL. "dir" may need to be changed to a suitable place
### for your system, but the WAL settings are an advanced configuration. The
### defaults should work for most systems.
###

[data]
# The directory where the TSM storage engine stores TSM files.
dir = "/var/lib/influxdb/data" # 数据存储目录

# The directory where the TSM storage engine stores WAL files.
wal-dir = "/var/lib/influxdb/wal" # wal 数据存储目录

# The amount of time that a write will wait before fsyncing. A duration
# greater than 0 can be used to batch up multiple fsync calls. This is useful for slower
# disks or when WAL write contention is seen. A value of 0s fsyncs every write to the WAL.
# Values in the range of 0-100ms are recommended for non-SSD disks.
# wal-fsync-delay = "0s"


# The type of shard index to use for new shards. The default is an in-memory index that is
# recreated at startup. A value of "tsi1" will use a disk based index that supports higher
# cardinality datasets.
# index-version = "inmem"

# Trace logging provides more verbose output around the tsm engine. Turning
# this on can provide more useful output for debugging tsm engine issues.
# trace-logging-enabled = false

# Whether queries should be logged before execution. Very useful for troubleshooting, but will
# log any sensitive data contained within a query.
# query-log-enabled = true

# Validates incoming writes to ensure keys only have valid unicode characters.
# This setting will incur a small overhead because every key must be checked.
# validate-keys = false

# Settings for the TSM engine

# CacheMaxMemorySize is the maximum size a shard's cache can
# reach before it starts rejecting writes.
# Valid size suffixes are k, m, or g (case insensitive, 1024 = 1k).
# Values without a size suffix are in bytes.
cache-max-memory-size = "1g" # 最大缓存大小,可以设置k,m,g

[coordinator]
# The default time a write request will wait until a "timeout" error is returned to the caller.
write-timeout = "10s"

# The maximum number of concurrent queries allowed to be executing at one time. If a query is
# executed and exceeds this limit, an error is returned to the caller. This limit can be disabled
# by setting it to 0.
#
# max-concurrent-queries项是配置最大的可执行的命令数,此项值为零则表示无限制。
# 如果你执行的命令数超过这个配置项的数量,则会报如下错误:
# ERR: max concurrent queries reached
#
max-concurrent-queries = 0

# The maximum time a query will is allowed to execute before being killed by the system. This limit
# can help prevent run away queries. Setting the value to 0 disables the limit.
#
# query-timeout项用来配置命令的超时时间,如果命令的执行时长超过了此时间,则influxDB会杀掉这条语句并报出如下错误:
# ERR: query timeout reached
# 如果配置了连续查询,那么最好不要配置query-timeout超时时间,因为随着数据量的增加,连续查询生成的数据所需要的时间更长,配置之后会导致数据生成不成功。
query-timeout = "0"

# The time threshold when a query will be logged as a slow query. This limit can be set to help
# discover slow or resource intensive queries. Setting the value to 0 disables the slow query logging.
#
# log-queries-after用来配置执行时长为多少的语句会被记录为慢查询。配置为0则表示不会记录这些语句。
# 比如,改项配置为“1s”,则执行时长超过1秒的语句会被标记为慢查询,并记录在日志里。
#
log-queries-after = "10s"

[http]
# Determines whether HTTP endpoint is enabled.
# enabled = true

# The bind address used by the HTTP service.
bind-address = ":8066"

# Determines whether user authentication is enabled over HTTP/HTTPS.
# auth-enabled = false

# The default realm sent back when issuing a basic auth challenge.
# realm = "InfluxDB"

# Determines whether HTTP request logging is enabled.
# 默认为true,会生成很多http请求的数据,建议关闭,不然日志文件跟插入数据量成正比,大致1:1的关系
#
log-enabled = false

# The default chunk size for result sets that should be chunked.
# 查询页面显示最大记录数
max-row-limit = 10000

[continuous_queries]
# Determines whether the continuous query service is enabled.
# //开启连续查询
#
enabled = true

# Controls whether queries are logged when executed by the CQ service.
# //开启连续查询的日志,有助于异常发现
#
log-enabled = true

# Controls whether queries are logged to the self-monitoring data store.
# query-stats-enabled = false

# interval for how often continuous queries will be checked if they need to run
# run-interval = "1s"

启动

可以看到目前两个端口都已启动

1
2
3
4
5
[root@iZ2zecgq3cou36re3sxh4bZ ~]# systemctl  start influxdb.service
[root@iZ2zecgq3cou36re3sxh4bZ ~]# netstat -nlpt | grep influxd
tcp 0 0 127.0.0.1:8088 0.0.0.0:* LISTEN 2844/influxd
tcp6 0 0 :::8086 :::* LISTEN 2844/influxd
[root@iZ2zecgq3cou36re3sxh4bZ ~]#

WEBUI

从influxdb1.3+都已经取消了web管理界面,只有安装低版本的influxdb,或者两种并存,但是需要修改数据目录指向1.8版本 参考https://blog.csdn.net/wsdc0521/article/details/106064914/。

命令行基本使用

安装完成后输入influx即可进入命令行

1.进入命令行

1
2
3
4
5
[root@iZ2zecgq3cou36re3sxh4bZ ~]# influx
Connected to http://localhost:8086 version 1.8.3
InfluxDB shell version: 1.8.3
>
>

2.查看数据库

1
2
3
4
5
6
> show databases; # 查看数据库
name: databases
name
----
_internal # 默认系统自带数据库_internal
>

3.创建数据库

1
2
3
4
5
6
7
8
> create database roddydb;
> show databases;
name: databases
name
----
_internal
roddydb
>

4.使用数据库

1
2
3
> use roddydb
Using database roddydb
>

5.写入数据

向cpu中新增一条记录,如果cpu这个measurement不存在,则新建一个。host,ip 称为TAG,value为值。

1
> INSERT cpu,host=WEB,IP=172.16.2.3 value=0.64

6.查询measurements(表)

1
2
3
4
5
6
> show MEASUREMENTS;
name: measurements
name
----
cpu
>

7.查询全部记录

1
2
3
4
5
6
7
> select * from cpu
name: cpu
time IP host value
---- -- ---- -----
1602299302885589799 172.16.2.3 WEB 0.64
1602299673721103381 172.16.243 WEB 0.95
>

8.筛选条件

1
2
3
4
5
6
> select * from "cpu" where value > 0.8
name: cpu
time IP host value
---- -- ---- -----
1602299673721103381 172.16.243 WEB 0.95
>

注意 :在WHERE子句中,如果是string类型的field value,一定要用单引号括起来。如果不适用引号括起来,或者使用的是双引号,将不会返回任何数据,有时甚至都不报错!

1
2
3
4
5
6
> select * from "cpu" where "IP" = '172.16.2.3' # value要为单引号
name: cpu
time IP host value
---- -- ---- -----
1602299302885589799 172.16.2.3 WEB 0.64
>
1
2
3
4
5
6
7
> select * from "cpu" where "host" = 'WEB'
name: cpu
time IP host value
---- -- ---- -----
1602299302885589799 172.16.2.3 WEB 0.64
1602299673721103381 172.16.243 WEB 0.95
>

9.删除表/measurements

方法1.删除表里的数据,直到没有数据那么该measurements就被删除。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> show MEASUREMENTs
name: measurements
name
----
cpu
> select * from "cpu"
name: cpu
time IP host value
---- -- ---- -----
1602300774790998139 192.168.2.3 DB 0.12

> delete from cpu where "IP"='192.168.2.3'
> select * from "cpu"
> show MEASUREMENTs
>

方法2.使用drop

1
> drop MEASUREMENT cpu

更多语法参考官网:https://docs.influxdata.com/influxdb/v1.8/query_language/sample-data/

参考