目录
引言
InfluxDB是一个由InfluxData开发的开源时序数据库,专为高性能地查询与存储时序数据而设计。它使用Go语言编写,没有外部依赖,提供了简单、高性能的写入和查询HTTP API。InfluxDB广泛应用于存储系统的监控数据、IoT行业的实时数据等场景。
一、Windows下安装InfluxDB教程
1、安装和配置
1.1、解压influxdb-1.8.0_windows_amd64.zip
1.2、以管理员身份运行cmd,进入influxdb-1.8.0-1文件夹,然后执行以下命令,生成配置文件influxd.config
influxd config>influxd.config
1.3、修改influxd.config以下配置项信息
bind-address = "127.0.0.1:8088"
[meta]
dir = "D:\\data\\influxdb\\meta"
[data]
dir = "D:\\data\\influxdb\\data"
wal-dir = "D:\\data\\influxdb\\wal"
[http]
auth-enabled = true
1.4、安装目录下新建如下startup.bat脚本
influxd -config influxdb.conf
双击startup.bat启动influxdb服务
1.5、双击influx.exe,打开influxdb客户端
1.6、创建用户并分配权限,验证是否安装成功
#Influxdb客户端下执行
CREATE USER admin WITH PASSWORD 'admin' WITH ALL PRIVILEGES
#用户认证
auth
打开influxdb客户端,验证身份认证功能是否已打开
命令需要在用户认证后才可执行,说明身份认证功能已打开
2、安装go语言
配置GOROOT、PATH环境变量
最后,记得重启电脑使环境变量生效
二、Centos7下离线安装InfluxDB教程
1、安装和配置
1.1、安装
#1、创建目录,并将依赖复制到该目录下
mkdir -p /home/soft/influxdb/
#2、安装
yum localinstall /home/soft/influxdb/influxdb-1.7.10.x86_64.rpm
#3、创建数据存储目录
mkdir -p /data/influxdb
chmod -R 777 /data
#4、备份配置文件
cp /etc/influxdb/influxdb.conf /etc/influxdb/influxdb.conf.default
1.2、修改influxd.config以下配置项信息
[meta]
# Where the metadata/raft database is stored
dir = "/data/influxdb/meta"
[data]
# The directory where the TSM storage engine stores TSM files.
dir = "/data/influxdb/data"
# The directory where the TSM storage engine stores WAL files.
wal-dir = "/data/influxdb/wal"
[http]
# Determines whether HTTP endpoint is enabled.
enabled = true
# Determines whether user authentication is enabled over HTTP/HTTPS.
auth-enabled = true
1.3、启动服务
systemctl start influxd
1.4、创建用户并分配权限
#进入influxdb客户端
influx
#Influxdb客户端下执行如下命令,创建用户并分配权限
CREATE USER admin WITH PASSWORD 'admin' WITH ALL PRIVILEGES
#用户认证
auth
1.5、重启服务,验证身份认证是否成功
systemctl restart influxd
命令需要在用户认证后才可执行,说明身份认证功能已打开
2、服务命令
#启动服务
systemctl start influxd
#停止服务
systemctl stop influxd
#重启服务
systemctl restart influxd
#查看服务状态
systemctl status influxd
三、InfluxDB常用命令
1、登录认证命令
#进入influxDB命令行界面
influx
#认证登录
influx -username "<username>" -password '<password>'
#退出命令行界面
Ctrl+D、exit、quit
#用户认证
auth
2、库表管理命令
#创建数据库,<database>是要创建的数据库名称,仅包含ascii、数字的话,可以不加双引号
create database "<database>"
#显示所有现有数据库
show databases
#删除数据库
drop database "<database>"
#针对特定数据库进行操作
use "<database>"
#显示所有表
show measurements on "<database>"
#显示表的索引字段
show tag keys on "<database>" from "<measurement>"
#显示表的非索引字段
show field keys on "<database>" from "<measurement>"
#显示表的数据结合,由tags排列组合计算出来
show series on "<database>" from "<measurement>"
#删除表
drop measurement "<measurement>"
#正确的姿势删除measurement
#先删除tag
use <measurement>
drop series from "<database>"
#再删除表
drop measurement "<measurement>"
#查看保留策略
show retention policies on "<database>"
#创建保留策略
create retention POLICY "<policy>" on "<database>" duration 7d replication 1 shard duration 72h default
#删除保留策略
drop retention POLICY "<policy>" on "<database>"
#修改保留策略
alter retention POLICY "<policy>" on "<database>" shard duration 72h default
3、用户管理命令
#查看用户
show users
#创建普通用户
create user "<username>" with password '<password>'
#创建管理员用户
create user "<username>" with password '<password>' with all privileges;
#删除用户
drop user "<username>"
#修改密码
set password for "<username>" = '<password>'
#用户授权
grant[read,write,all] on <database> to <username>
grant all on "<database>" to "<username>"
#撤销权限
revoke all privileges from <username>
#查看用户权限
show grants for <username>
4、查询统计命令
#查看所有连续查询
show continuous queries
#查看数据量大小
select sum(diskBytes) / 1024 / 1024 /1024 from _internal."monitor"."shard" where time > now() - 10s group by "<database>"
#查询一天的数据量
select count(<field>) from "<database>" where time >= '2020-04-16 00:00:00' and time <= '2020-04-16 23:59:59'