0. 介绍
时序数据库,也就是TSDB,是区别于关系数据库的一种数据库,比较适合记录时间为横轴的若干数据点
InfluxDB是一个由InfluxData开发的开源时序型数据。它由Go写成,着力于高性能地查询与存储时序型数据。InfluxDB被广泛应用于存储系统的监控数据,IoT行业的实时数据等场景。
InfluxDB排行28,TSDB中第一:https://db-engines.com/en/ranking
在用TSDB之前,存储一些传感器数值用MySQL表如下:
在web应用中,经常有按时间查找和展示历史数据的需求,如下:
以上的情况相比MySQL 就更适合用TSDB
MySQL | influxDB | |
---|---|---|
数据库 | database | database |
数据表 | table | measurement |
数据列 | column | tag、field、timestemp |
数据行 | row | points |
1. Docker安装
原本想用Linux安装呢,但是看到官网有docker镜像就笑了,那还装什么,直接run
https://docs.influxdata.com/influxdb/v2.2/install/?t=Docker
Linux安装
本文用Dockers,就不介绍了Linux安装了
安装详见:https://docs.influxdata.com/influxdb/v2.2/install/?t=Linux
1.1 搞config
mkdir /etc/influxdb
# 整个默认配置文件出来
docker run --rm influxdb:2.2.0 influxd print-config > /etc/influxdb/config.yml
1.2 搞volume
# 创建目录
mkdir /data/influxdb
# run
docker run -d --name influxdb -p 8086:8086 -v /etc/influxdb/config.yml:/etc/influxdb2/config.yml --volume /data/influxdb:/var/lib/influxdb2 influxdb:2.2.0
首次运行用docker run ,再次运行:docker restart influxdb
influxdb官网推荐的是2.1.1,这个版本总感觉有内存泄露,越用占内存越多,因此换到最新2.2.0版本。
最新版可以到github上看https://github.com/influxdata/influxdb/releases
1.3 web端管理
http://IP:8086/
第一次运行,设置一些东西:
登录用的一些信息、设置一下:
直接点QuickStart:
1.3 搞command(CLI)
由于CLI需要Token,所以需要先用图形界面初始化一下,拿到Token:
UD3qST2Zu57fMZVmxaFTIxAhp6XA_CXt7YuUyQouyYfoAJtpAkH_CzmHP6BBLGY-XDAJVCthTClSLz0j2eg_Cg==
Linux下运行: docker exec -it influxdb /bin/bash
先ping一下:influx ping
看一下用户列表:influx user list -t <your-token>
其他的命令,看help就行了-h
root@b13b898d288c:/# influx -h
NAME:
influx - Influx Client
USAGE:
influx [command]
COMMANDS:
version Print the influx CLI version
ping Check the InfluxDB /health endpoint
setup Setup instance with initial user, org, bucket
write Write points to InfluxDB
bucket Bucket management commands
completion Generates completion scripts
bucket-schema Bucket schema management commands
query Execute a Flux query
config Config management commands
org, organization Organization management commands
delete Delete points from InfluxDB
user User management commands
task Task management commands
backup Backup database
restore Restores a backup directory to InfluxDB
telegrafs List Telegraf configuration(s). Subcommands manage Telegraf configurations.
dashboards List Dashboard(s).
export Export existing resources as a template
secret Secret management commands
v1 InfluxDB v1 management commands
auth, authorization Authorization management commands
apply Apply a template to manage resources
stacks List stack(s) and associated templates. Subcommands manage stacks.
template Summarize the provided template
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--help, -h show help
参考网址:https://docs.influxdata.com/influxdb/v2.2/reference/cli/influx/
最后:Ctrl + D 退出
2. 编程连接influxDB
Go语言:https://blog.csdn.net/xuehu96/article/details/123993551
Python:https://blog.csdn.net/xuehu96/article/details/124036942
------------ EOF ------------
+CLI命令补充
删除整个measurement
influx delete --bucket cpumem --start 1970-01-01T00:00:00Z --stop $(date +"%Y-%m-%dT%H:%M:%SZ") --predicate '_measurement="CPUMEM"' -o "xuehu96" -t 7AEvwcmfWTDZk6mKTlfT-NRRBAiTHcY8zClEoblR8qWfhqZLkxU0WryrZEwEwYVc4hs-pesOmFF9nIsRXZiwSA==
按时间删除
influx delete --bucket cpumem --start 2022-05-01T00:00:00Z --stop 2022-05-02T00:00:00Z --predicate '_measurement="Win"' -o "xuehu96" -t 7AEvwcmfWTDZk6mKTlfT-NRRBAiTHcY8zClEoblR8qWfhqZLkxU0WryrZEwEwYVc4hs-pesOmFF9nIsRXZiwSA==
按Tag删除
influx delete --bucket cpumem --start 2022-04-15T00:00:00Z --stop $(date +"%Y-%m-%dT%H:%M:%SZ") --predicate '_measurement="Win" AND os="windows2019"' -o "xuehu96" -t 7AEvwcmfWTDZk6mKTlfT-NRRBAiTHcY8zClEoblR8qWfhqZLkxU0WryrZEwEwYVc4hs-pesOmFF9nIsRXZiwSA==