influxdb安装与基本操作(下篇)

  
 创建用户
 influx user create -n <username> -p <password> -o <org-name> -t <your-token>
 
 查看用户
 influx user list -t <your-token>
 删除用户
 influx user delete -t <user-id> -t <your-token>
 
 更新用户
 influx user update -i <user-id> -n <new-username> -t <your-token>
 
 修改密码
 influx user password -n <username> -t <your-token>
# 指令执行后,CLI会引导密码修改

oganization管理
官方建议一个influxdb实例中建组不超过20个,因为influxdb支持大约20个bucket的写入或查询,超过这个值将会对influxdb性能造成影响

创建oganization
创建组有两种方式一种是使用UI管理界面创建,一种是使用influx命令。UI界面根据引导点击就行了

influx命令

influx org create <org-name> -t <your-token>

查看oganization
influx org list -t <your-token>

更新oganization
influx org update -i <org-id> -n <new-org-name>
删除oganization
influx org delete -i <org-id>

bucket管理
bucket内的数据是有保存期限的influxdb称之为retention-period-duration,bucket在创建之初就会指定

bucket创建
influx bucket create -n <bucket-name> -o <org-name> -r <retention-period-duration> -t <your-token>
# -o 指定organization名称,-r指定数据保留时间
# 数据保留时间单位在以下单位中取值:
#纳秒 (ns)
#微秒 (us or μs)
#毫秒 (ms)
#秒 (s)
#分 (m)
#时 (h)
#日 (d)
#周 (w)

#示例
influx bucket create -n test-bucket -o wxm -r 72h5m -t 58s6gl9hD8lk-AS_i6mUaYMMCGe6N1vIfVpJUo2xJ2HkWMlWx2yp7r7IKZsyF6h8vQdTPfIpGyHtbALayLgUQw==
bucket更新
更新bucket名称

influx bucket update -i <bucket-id> -n <new-bucket-name>
更新数据保存时间

influx bucket update -i <bucket-id> -r <retention period with units>
bucket查看
influx bucket list -o <org-name or org-id> -t <your-token>
bucket删除
通过名称删除

influx bucket delete -n <bucket-name> -o <org-name>
通过id删除

influx bucket delete -i <bucket-id>
Line Protocol(数据写入协议)
通过Post请求方式向influxdb中写入数据进行测试,可使用Postman 详细见 Postman Influx http api 调用 章节。

Line Protocol 语法
# 以下取自官方文档
# 语法
<measurement>[,<tag_key>=<tag_value>[,<tag_key>=<tag_value>]] <field_key>=<field_value>[,<field_key>=<field_value>] [<timestamp>]

# 示例
myMeasurement,tag1=value1,tag2=value2 fieldKey="fieldValue" 1556813561098000000
一个\n代表一条数据协议的结束,所以数据内容不支持\n

Line Protocol 支持部分特殊字符但需要进行转义

Flux查询语言
Flux 可归类为一门新的编程语言,这里仅介绍针对查询需要的一些常用函数和操作,了解更多Flux见Flux官方文档

大多数Flux查询遵循如下几个步骤

指定数据源
过滤数据
对数据整形(重新组织数据结构)
数据内容计算
//例
from(bucket: "example-bucket")            // ── Source
  |> range(start: -1d)                    // ── Filter on time
  |> filter(fn: (r) => r._field == "foo") // ── Filter on column values
  |> group(columns: ["sensorID"])         // ── Shape
  |> mean()                               // ── Process
flux查询语言看起来会像下面这样

from(bucket: "wxm-influx")
  |> range(start: 0)
  |> filter(fn:(r)=>(r.owner=="wxm"))
  |> window(every: 1s)
  |> mean()
其中 from,range,filter等以下称作查询函数。flux查询语言允许使用|>分割符将上一个函数的输出传递给下一个函数,类似于linux的管道

基础规定
每个查询语句必须包含数据源,时间区间和过滤器。对应关键字分别为 from,range,filter。实际上不包含filter也是可以的,可以理解为filter过滤条件就是all

Flux 标准库(部分)
该部分列举常用flux标准库函数,根据用法可举一反三使用所有标准库函数

buckets
buckets()返回当前组中所有的桶

from
指定从那个bucket查询数据

from(bucket:"bucket名称")
range
指定时间区间,range接收两个参数,start和stop 其中stop可以省略,缺省值是当前时间。start不能省略,否则会报语法错误。

start和stop 既可以使用相对的时间区间,也可以使用绝对的时间戳。相对时间区间格式为 时间间隔h/m/s例如 -1h,-5m。时间戳格式2021-01-01T00:00:00Z

#示例1
from(bucket:"bucket名称")
  |> range(start: -1h)
#示例2
from(bucket:"bucket名称")
  |> range(start: 2021-01-01T00:00:00Z, stop: 2021-01-01T12:00:00Z)
filter
对数据进行过滤,接收一个判断函数。类似于java中的lamda,它看起来像是下面这样

(r) => (r.recordProperty comparisonOperator comparisonExpression)
判断函数返回一个boolean类型值。只有符合条件的记录才会被返回。

#示例
from(bucket: "wxm-influx")
  |> range(start: -24h)
  |> filter(fn:(r)=>(r.owner=="wxm"))
#取最近24小时,含有owner标签且值为wxm的记录
当filter内含有多个条件时可用 and 或 or 连接各个条件

#示例
from(bucket: "wxm-influx")
  |> range(start: -24h)
  |> filter(fn:(r)=>(r.owner=="wxm" or r.key=="indoor"))
#取最近24小时,含有owner标签且值为wx或含有标签key且值为indoor的记录
contains
参数

value 需要比较的值

set被比较的集合

判断value是否包含在set中。


#示例
fiels = ["wxm","ali"]
from(bucket: "wxm-influx")
  |> range(start: 2022-03-12T08:14:04Z,stop:2022-03-13T08:19:05Z)
  |> filter(fn:(r)=>contains(value:r.owner,set:fiels))


influxdb alerts
alerts中有三个核心概念 Checks,Notification Endpoint 和 Notification Rules。
Checks 负责检测报警并将检测结果分级。
Notification Endpoint 负责定义通知切点,即报警通知会发送到哪里
Notification Rules 负责绑定 Checks 和 Notification Endpoint

Checks
Checks 分为两种,一种是threshold Checks 一种是deadman Checks,Checks会周期执行一段指定查询脚本,并查看结果是否匹配其Checks规则,一旦匹配就会生成一条Checks记录。
threshold checks
threshold alert 为某个点指定阈值和比较规则(大于小于等),判断查询结果是否符合阈值表达式

Postman Influx http api 调用
鉴权设置
进入Postman Collections 或单个请求的 Authorization 功能选项。

Type 选择API Key
Key 填入 Authorization
Value 填入 Token YOUR_TOKEN
Add to 选择Header

Query API
鉴权后调用 /api/query 可使用查询语句进行查询。

如何使用

将org或orgID通过url传参的形式添加到参数中

添加或修改Accept请求头为 appliction/csv(Postman中不需要添加)

添加或修改Content-Type请求头为 application/vnd.flux

将查询语句放在请求体中

Write API
将org,bucket 通过url传参形式添加到参数中
添加或修改Accept请求头为 appliction/json(实测该接口不反回数据,可选填)
添加或修改Content-Type请求头为 text/plain
将Line Protocol的数据添加到请求体中
数据备份与恢复
备份
influxdb 使用 influx 命令进行数据和元数据的备份。1.x 和 2.x备份数据不兼容。以下为2.x的备份方法

influx backup <back-path> -t <root-token>

# 例:
influx backup ./back -t 58s6gl9hD8lk-AS_i6mUaYMMCGe6N1vIfVpJUo2xJ2HkWMlWx2yp7r7IKZsyF6h8vQdTPfIpGyHtbALayLgUQw==
token 可以在管理界面Tokens处找到

恢复
influx 使用influx命令进行数据恢复

influx restore <back-path>
#例:
influx restore ./back
恢复指定 bucket

influx restore <back-path> --bucket exmple-bucket
如果恢复的 bucket 名称已经在现有数据库中存在 则使用--new-bucket 为恢复的数据库指定一个新名称并将数据恢复到新名称的bucket中

https://docs.influxdata.com/influxdb/v2.1/api-guide/client-libraries/go/

yum 
yum update 升级系统
yum install ~ 安装指定软件包
yum update ~ 升级指定软件包
yum remove ~ 卸载指定软件
yum grouplist 查看系统中已经安装的和可用的软件组,可用的可以安装
yum grooupinstall ~安装上一个命令显示的可用的软件组中的一个
yum grooupupdate ~更新指定软件组的软件包
yum grooupremove ~ 卸载指定软件组中的软件包
yum deplist ~ 查询指定软件包的依赖关系
yum list yum* 列出所有以yum开头的软件包
yum localinstall ~ 从硬盘安装rpm包并使用yum解决依赖


清除YUM缓存
yum 会把下载的软件包和header存储在cache中,而不会自动删除。如果我们觉得它们占用了磁盘空间,可以使用yum clean指令进行清除,更精确的用法是yum clean headers清除header,yum clean packages清除下载的rpm包,yum clean all 清除所有。

1.清除缓存目录(/var/cache/yum)下的软件包
命令:yum clean packages
2.清除缓存目录(/var/cache/yum)下的 headers
命令:yum clean headers
3.清除缓存目录(/var/cache/yum)下旧的 headers
命令:yum clean oldheaders
4.清除缓存目录(/var/cache/yum)下的软件包及旧的headers
命令:yum clean, yum clean all (= yum clean packages; yum clean oldheaders)

用yum查询想安装的软件

1.使用YUM查找软件包
命令:yum search ~
2.列出所有可安装的软件包
命令:yum list
3.列出所有可更新的软件包
命令:yum list updates
4.列出所有已安装的软件包
命令:yum list installed
5.列出所有已安装但不在Yum Repository 內的软件包
命令:yum list extras
6.列出所指定软件包
命令:yum list ~
7.使用YUM获取软件包信息
命令:yum info ~
8.列出所有软件包的信息
命令:yum info
9.列出所有可更新的软件包信息
命令:yum info updates
10.列出所有已安裝的软件包信息
命令:yum info installed
11.列出所有已安裝但不在Yum Repository 內的软件包信息
命令:yum info extras
12.列出软件包提供哪些文件
命令:yum provides~


linux 命令行删除


influx delete -org nd -bucket nd -start 2023-04-25T16:00:00.000Z -stop 2023-08-27T16:00:00.000Z -predicate _measurement="EnvironmentDetection" 


终端查询 数据

influx query
from(bucket: "example-bucket")
  |> range(start: -1h)
  |> filter(fn: (r) =>
    r._measurement == "example-measurement" and
    r._field == "example-field"
  )
from(bucket: "nd")
  |> range(start: -1h)
  |> filter(fn: (r) =>
    r._measurement == "ElectricityMeter"
  )
  
  influxdb 查询sql语句时 
  1:可以在sql语句里携带 时区转换  
  2:可以使用sql的 类型转换  聚合函数 等
  https://www.alibabacloud.com/help/zh/time-series-database/latest/data-types-and-cast-operations?spm=a2c63.p38356.0.0.3ddeec135q9yq1
  https://docs.influxdata.com/influxdb/v1.4/tools/api/
  
  
  influxd 是InfluxDB的守护进程,命令支持的参数:
1.backup 数据备份
2.config 显示InlfuxDB的默认配置信息
3.help 显示帮助
4.restore 还原之前通过backup命令备份的数据
5.run 运行程序,默认参数,可忽略
6.version 显示程序版本信息

influx_inspect 是InfluxDB的数据检查工具,可通过influx_inspect查看InfluxDB的TSM格式文件的内容
influx_inspect dumptsm -all /var/lib/influxdb/data/monitor/autogen/6/0000000001-0000000001.tsm

influx_inspect 命令支持的参数:
1.deletetsm 批量删除原始TSM文件
2.dumptsi 显示tsi 1 文件的底层细节信息
3.dumptsm 显示tsm 1 文件的底层细节信息
4.buildtsi 从tsm 1 数据中生成tsi 1 索引信息
5.help 显示帮助
6.export 导出数据
7.report 显示分片级别的数据信息
8.verify 验证TSM文件的完整性
9.verify-seriesfile 验证时序文件的完整性

influx_stress 是InfluxDB的压力测试工具,但从v1.2.0开始弃用,推荐使influx-stress和influxdb-comparisons进行压测

influx_tsm 是InfluxDB的数据库格式转换工具,可以将数据库从b1或bz1格式转换为tsm 1格式,b1和bz1为InfluxDB之前版本支持的格式。
influx_tsm 命令支持的参数:
1.-backup string 备份文件的存放位置,但是不能再当前数据的存储目录中备份
2.-dbs string 以逗号分隔的要转换的数据库列表,默认转换所有数据库的数据
3.debug string 在给定的HTTP地址上开启debug功能
4.-interval duration 打印状态更新的频率,默认为5秒
5.-nobackup 禁用数据库备份(不推荐)
6.-parallel 启用并行转换
7.-profile string CPU profile文件的存储位置
8.-sz uint 单个TSM文件的大小(默认为2147483648)
9.-y 不询问,直接转换
  
  ifluxdb 报错
  2023/09/05 18:22:20 influxdb2client E! Write error: internal error: unexpected error writing points to database: database not found: 3c584dfb31a2c64f, batch kept for retrying
2023/09/05 18:22:20 influxdb2client E! Error flushing batch from retry queue: %!w(<nil>)
2023-09-05 18:22:20.061524266 +0800 CST
2023/09/05 18:22:20 influxdb2client E! Write error: internal error: unexpected error writing points to database: database not found: 3c584dfb31a2c64f, batch kept for retrying
2023/09/05 18:22:20 influxdb2client E! Error flushing batch from retry queue: %!w(<nil>)
2023-09-05 18:22:20.064865969 +0800 CST
2023/09/05 18:22:20 influxdb2client E! Write error: internal error: unexpected error writing points to database: database not found: 3c584dfb31a2c64f, batch kept for retrying
2023/09/05 18:22:20 influxdb2client E! Error flushing batch from retry queue: %!w(<nil>)
2023-09-05 18:22:20.067745067 +0800 CST
2023/09/05 18:22:20 influxdb2client E! Write error: internal error: unexpected error writing points to database: database not found: 3c584dfb31a2c64f, batch kept for retrying
2023/09/05 18:22:20 influxdb2client E! Error flushing batch from retry queue: %!w(<nil>)
2023-09-05 18:22:20.070297645 +0800 CST
2023-09-05 18:22:20.0723837 +0800 CST
2023-09-05 18:22:20.074270669 +0800 CST
2023/09/05 18:22:20 influxdb2client E! Write error: internal error: unexpected error writing points to database: database not found: 3c584dfb31a2c64f, batch kept for retrying
2023/09/05 18:22:20 influxdb2client E! Error flushing batch from retry queue: %!w(<nil>)

Incompatible InfluxDB 2.0 version found. Move all files outside of engine_path before influxd will start
[root@localhost influxdb]# influxd
2023-09-06T02:02:41.293041Z    info    Welcome to InfluxDB    {"log_id": "0k63UV30000", "version": "v2.7.0", "commit": "85f725f8b9", "build_date": "2023-04-05T15:32:25Z", "log_level": "info"}
2023-09-06T02:02:41.293717Z    info    Resources opened    {"log_id": "0k63UV30000", "service": "bolt", "path": "/var/lib/influxdb/influxd.bolt"}
2023-09-06T02:02:41.293889Z    info    Resources opened    {"log_id": "0k63UV30000", "service": "sqlite", "path": "/var/lib/influxdb/influxd.sqlite"}
2023-09-06T02:02:41.295996Z    info    Checking InfluxDB metadata for prior version.    {"log_id": "0k63UV30000", "bolt_path": "/var/lib/influxdb/influxd.bolt"}
2023-09-06T02:02:41.296016Z    error    Missing metadata for bucket.    {"log_id": "0k63UV30000", "bucket": "_monitoring", "bucket_id": "3c584dfb31a2c64f"}
2023-09-06T02:02:41.296023Z    error    Incompatible InfluxDB 2.0 metadata found. File must be moved before influxd will start.    {"log_id": "0k63UV30000", "path": "/var/lib/influxdb/influxd.bolt"}
2023-09-06T02:02:41.296037Z    error    Incompatible InfluxDB 2.0 version found. Move all files outside of engine_path before influxd will start.    {"log_id": "0k63UV30000", "engine_path": "/var/lib/influxdb/engine"}

[root@localhost ~]# systemctl status influxdb
● influxdb.service - InfluxDB is an open-source, distributed, time series database
   Loaded: loaded (/usr/lib/systemd/system/influxdb.service; enabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since 三 2023-09-06 10:26:18 CST; 44s ago
     Docs: https://docs.influxdata.com/influxdb/
  Process: 19219 ExecStart=/usr/lib/influxdb/scripts/influxd-systemd-start.sh (code=exited, status=0/SUCCESS)

9月 06 10:26:18 localhost.localdomain systemd[1]: Failed to start InfluxDB is an open-source, distributed, time series database.
9月 06 10:26:18 localhost.localdomain systemd[1]: Unit influxdb.service entered failed state.
9月 06 10:26:18 localhost.localdomain systemd[1]: influxdb.service failed.
9月 06 10:26:18 localhost.localdomain systemd[1]: influxdb.service holdoff time over, scheduling restart.
9月 06 10:26:18 localhost.localdomain systemd[1]: Stopped InfluxDB is an open-source, distributed, time series database.
9月 06 10:26:18 localhost.localdomain systemd[1]: start request repeated too quickly for influxdb.service
9月 06 10:26:18 localhost.localdomain systemd[1]: Failed to start InfluxDB is an open-source, distributed, time series database.
9月 06 10:26:18 localhost.localdomain systemd[1]: Unit influxdb.service entered failed state.
9月 06 10:26:18 localhost.localdomain systemd[1]: influxdb.service failed.

[root@localhost ~]# systemctl start influxdb
Job for influxdb.service failed because a configured resource limit was exceeded. See "systemctl status influxdb.service" and "journalctl -xe" for details.


删除安装的influxdb2  因为bolt 文件打不开 大概率是文件损坏 或者其它原因没找到  只能卸载重装
rpm -q influxdb2 查看安装包


但是从 v2 开始,配置方式发生了变化。你可以在there确认。

当 influxd 启动时,它会在当前工作目录中检查名为 config.* 的文件。文件扩展名取决于配置文件的语法。

要自定义配置文件的目录路径,请将 INFLUXD_CONFIG_PATH 环境变量设置为您的自定义路径。

在启动时,influxd 将检查 INFLUXD_CONFIG_PATH 目录中的配置。*。

因此,在 v2 中,配置文件因您的起始流入位置而异。如果你想有一个特殊的路径,你应该设置INFLUXD_CONFIG_PATH。


其中engine-path就是InfluxDB存储TSM数据的位置


2.配置参数(主要)

bolt-path:用来存放influx用户数据、组织、库等,用的时boltDB存储

engin-path: 用来存放时序数据的文件夹目录

sqlite-path:用来存放一些元数据

flux-log-enabled:启用日志,默认false,

log-level:日志级别,支持debug、info、error,默认是info

http-bind-address:influxdb HTTP API的地址和端口号,默认“:8086“,冒号前面是ip地址

其他还有很多参数可以参看官网说明

InfluxDB configuration options | InfluxDB OSS 2.1 Documentation (influxdata.com)


influxdb  表里的索引  也会再数据域增加一列 ,如果再在数据域里把索引那列添加到数据域 就会重复

重新安装后  无法配置数据库 提示没有权限

配置优先级:
1.influxd flags
启动时指定配置:

influxd --engine-path=/home/engine
2.Environment variables
配置环境变量:

export INFLUXD_ENGINE_PATH=/home/engine
3.Configuration file settings
配置文件中配置(YAML/TOML/JSON):

#YAML:
engine-path: ~/.influxdbv2/engine
#TOML:
engine-path = "~/.influxdbv2/engine"
#JSON:
{"engine-path": "~/.influxdbv2/engine"
}
(配置文件位置需要先配置到环境变量中,详情见官网)

查看是否配置成功
在influxdb服务启动后,可以通过调用接口看到相关的配置
GET http://localhost:8086/api/v2/config
更改influxdb日志路径规则
https://blog.youkuaiyun.com/hui_hui_hui_hui/article/details/118549875


报错:
influx user list
Error: failed to list users: 401 Unauthorized: unauthorized access
出现此错误的时候 可以使用 -t 参数带上token  例如
[root@localhost main]# influx user list -t S2XQbsOs5ctMFnwN3maOSzURDoCL9r6vR7g_DvSXoHK2vTbzgVF5W0Vhhs1OQaWnVA0f78BKmeFd5uJKe9aSiA==
ID            Name
0bc63b6c53c1d000    admin

influx  -t P2p4trn5mQ7__uh_oZI6Zehfepyvm7GVrL6kmWI-J_jJ7ONHd5roYyLeUCoL3XPjh_cVC6IvHel95k3ii1w6Ug==  backup /opt (备份目录)

修改了一个字段的类型后需要更新measurement   删除指定的表没找到对应语句  只能选个大的时间将所有数据删除
influx delete -org nd -bucket nd -start 2023-04-25T16:00:00.000Z -stop 2023-11-27T16:00:00.000Z -predicate _measurement="EnvironmentDetection" -t S2XQbsOs5ctMFnwN3maOSzURDoCL9r6vR7g_DvSXoHK2vTbzgVF5W0Vhhs1OQaWnVA0f78BKmeFd5uJKe9aSiA==
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值