1.安装
(1) 创建安装目录 mkdir /usr/local/influxdb/ (2) 将 influxdb-1.7.10.x86_64.rpm复制到安装目录下 (3) 安装 cd /usr/local/influxdb/ yum localinstall influxdb-1.7.10.x86_64.rpm (4) 环境准备 mkdir -p /data/influxdb chmod -R 777 /data
或者:
# wget https://dl.influxdata.com/influxdb/releases/influxdb-1.7.8.x86_64.rpm
# sudo yum localinstall influxdb-1.7.8.x86_64.rpm
包文件路径
/usr/bin下 influxd influxdb服务器 influx influxdb命令行客户端 influx_inspect 查看工具 influx_stress 压力测试工具 influx_tsm 数据库转换工具(将数据库从b1或bz1格式转换为tsm1格式) /var/lib/influxdb/下 data 存放最终存储的数据,文件以.tsm结尾 meta 存放数据库元数据 wal 存放预写日志文件 /var/log/influxdb下 influxd.log 日志文件 /etc/influxdb下 influxdb.conf 配置文件 /var/run/influxdb/ influxd.pid PID文件
修改配置文件
(1) 备份配置文件 cp /etc/influxdb/influxdb.conf /etc/influxdb/influxdb.conf.default (2) 修改配置 vi /etc/influxdb/influxdb.conf 找到如下配置并修改或取消注释: [meta] dir = "/data/influxdb/meta" [data] dir = "/data/influxdb/data" wal-dir = "/data/influxdb/wal"
常用命令
启动服务 systemctl start influxd 停止服务 systemctl stop influxd 重启服务 systemctl restart influxd
检查是否已启动influxdb数据库
ps aux | grep influx;
2.启动
#启动
sudo systemctl start influxdb
#配置开机启动
sudo systemctl enable influxdb
3.防火墙配置
#开放端口
# firewall-cmd --add-port=8086/tcp --permanent
#重载防火墙配置
# firewall-cmd --reload
4.启动cli命令
[root@localhost grafana]# influx
Connected to http://localhost:8086 version 1.7.8
InfluxDB shell version: 1.7.8
>
5.配置用户和数据库
创建用户
> CREATE USER dolphin WITH PASSWORD 'dolphininfluxpwd' WITH ALL PRIVILEGES
查询用户
> SHOW USERS
user admin
---- -----
influx true
dolphin true
>
创建数据库
> CREATE DATABASE dolphin
查询数据库
> SHOW DATABASES
name: databases
name
----
_internal
dolphin
>
6.插入数据与查询数据
1.database
数据库,和mysql的数据库相比,没有太大的歧义
2.measurement
对比的是mysql中的table,从实际体验来看,两个之间最明显的区别在于没有单独的创建measurement的方法,直接新增一条数据时,若measurement不存在,则直接创建并插入一条数据
3.Point
这个对比的是mysql中的record,在influxDB中,表示每个表中,某个时刻,满足某个条件的filed数据(简单来说就是 timestamp + tag + filed)的组成一个point
timestamp : 时间戳,ns单位,每个记录都必然有这个属性,没有显示添加时,默认给一个。(纳秒,1s=1000ms 1ms=1000us 1us=1000ns)
tag: 标签,kv结构,在database中, tag + measurement 一起构建索引
参与索引创建,因此适合作为查询的过滤条件
tag的数据量不要太多,最好能有典型的辨别性(和mysql的建立索引的原则差不多)
value为String类型
tag是可选的,在measurement不设置tag也是ok的
field:存储数据,kv结构
数据类型为: long, String, boolean, float
4. Series
Series: tag key 与tag value的唯一组合
在insert执行语句中,tag与tag、field与field之间用都好进行分割,tag与field之间用空格分割
tag的value都是,String类型,不需要加双引号
field的String类型数据,需要放在双引号中,否则会报错
如果需要显示添加时间戳,在filed后添加空格,再添加时间戳
格式
<measurement>[,<tag-key>=<tag-value>...] <field-key>=<field-value>[,<field2-key>=<field2-value>...] [unix-nano-timestamp]
插入
> USE dolphin
Using database dolphin
插入数据(host是tag,load,usage是filed)
> INSERT cpu,host=192.168.1.1 load=0.1,usage=0.2
> INSERT cpu,host=192.168.1.2 load=0.2,usage=0.3
> INSERT cpu,host=192.168.1.3 load=0.6,usage=0.7
插入相同时间数据(tagkey1,tagkey2是tag,fieldkey1,fieldkey2是filed;tag的value都是,String类型,不需要加双引号
field的String类型数据,需要放在双引号中,否则会报错)
> insert table2,tagkey1=1,tagkey2=a fieldkey1=11,fieldkey2="aa" 1581771014614699790
> insert table2,tagkey1=2,tagkey2=a fieldkey1=11,fieldkey2="aa" 1581771014614699790
> select * from table2
name: table2
time fieldkey1 fieldkey2 tagkey1 tagkey2
---- --------- --------- ------- -------
1581771014614699790 11 aa 1 a
1581771014614699790 11 aa 2 a
查询数据全部数据
> SELECT * FROM "cpu"
name: cpu
time host load usage
---- ---- ---- -----
1581756324805070467 192.168.1.1 0.1 0.2
1581756335749318117 192.168.1.2 0.2 0.3
1581756346893987133 192.168.1.3 0.6 0.7
# 筛选条件
> SELECT "host","load","usage" FROM "cpu" WHERE "host" = '192.168.1.1'
name: cpu
time host load usage
---- ---- ---- -----
1581756324805070467 192.168.1.1 0.1 0.2
# 范围
> SELECT "host","load","usage" FROM "cpu" WHERE "usage" > 0.3
name: cpu
time host load usage
---- ---- ---- -----
1581756346893987133 192.168.1.3 0.6 0.7
>
其它
# 创建数据库
CREATE DATABASE "db_name"
# 显示所有数据库
SHOW DATABASES
# 删除数据库
DROP DATABASE "db_name"
# 使用数据库
USE mydb
# 显示该数据库中的表
SHOW MEASUREMENTS
# 删除表
DROP MEASUREMENT "t_name"
7.HTTP API
7.1 创建数据库
[root@localhost ~]# curl -i -XPOST http://172.16.159.132:8086/query --data-urlencode "q=CREATE DATABASE cctestdb"
HTTP/1.1 200 OK
Content-Type: application/json
Request-Id: 2ac0248e-4fd0-11ea-801e-000c29c94ec8
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.7.8
X-Request-Id: 2ac0248e-4fd0-11ea-801e-000c29c94ec8
Date: Sat, 15 Feb 2020 08:50:07 GMT
Transfer-Encoding: chunked
{"results":[{"statement_id":0}]}
[root@localhost ~]#
7.2 插入数据
#写入单条
curl -i -XPOST http://172.16.159.132:8086/write?db=cctestdb --data-binary "cpu,host=192.168.1.3 load=0.1,usage=0.33"
curl -i -XPOST http://172.16.159.132:8086/write?db=cctestdb --data-binary "cpu,host=192.168.1.3 load=0.1,usage=0.33 6666666666666666666"
#写入多条
curl -i -XPOST http://172.16.159.132:8086/write?db=cctestdb --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 1666666666666666663
cpu,host=192.168.1.2 load=0.2,usage=0.22 1666666666666666664
cpu,host=192.168.1.3 load=0.2,usage=0.33 1666666666666666665"
7.3查询数据
[root@localhost ~]# curl -G http://172.16.159.132:8086/query?db=cctestdb --data-urlencode "q=SELECT * FROM "cpu""
{"results":[{"statement_id":0,"series":[{"name":"cpu","columns":["time","host","load","usage"],"values":[["2020-02-15T08:52:00.039671928Z","192.168.1.3",0.1,0.33],["2022-10-25T02:57:46.666666661Z","192.168.1.2",0.1,0.22],["2022-10-25T02:57:46.666666661Z","192.168.1.3",0.1,0.33],["2022-10-25T02:57:46.666666662Z","192.168.1.2",0.2,0.22],["2022-10-25T02:57:46.666666662Z","192.168.1.3",0.2,0.33],["2022-10-25T02:57:46.666666663Z","192.168.1.3",0.1,0.33],["2022-10-25T02:57:46.666666664Z","192.168.1.2",0.2,0.22],["2022-10-25T02:57:46.666666665Z","192.168.1.3",0.2,0.33],["2181-04-04T11:51:06.666666666Z","192.168.1.3",0.1,0.33]]}]}]}
[root@localhost ~]#
8.配置http账号密码
1 创建账号
创建用户
> CREATE USER dolphin WITH PASSWORD 'dolphininfluxpwd' WITH ALL PRIVILEGES
查询用户
> SHOW USERS
user admin
---- -----
influx true
dolphin true
>
2 配置`http密码访问。 auth-enabled = true
vim /etc/influxdb/influxdb.conf
[http]
auth-enabled = true
3.1cli终端账号密码登录
[root@localhost grafana]# influx
Connected to http://localhost:8086 version 1.7.8
InfluxDB shell version: 1.7.8
> auth
username: dolphin
password:
> show databases;
name: databases
name
----
_internal
dolphin
cctestdb
itemdata
>
3.2http账号密码登录,-u dolphin:dolphininfluxpwd
# curl -i -XPOST http://172.16.159.132:8086/write?db=dolphin -u dolphin:dolphininfluxpwd --data-binary "itemdatas,value=100, 1581772819000000000"
参考文档
《CentOS 7下InfluxDB部署与使用入门》:https://cloud.tencent.com/developer/article/1333874
《influxDB基本概念小结》:https://www.cnblogs.com/yihuihui/p/9374561.html
参考链接:https://blog.csdn.net/CleverCode/java/article/details/104329925
参考网址:
https://dl.influxdata.com/influxdb/releases/influxdb-1.7.10.x86_64.rpm
https://s0docs0influxdata0com.icopy.site/influxdb/v1.7
https://www.linuxdaxue.com/influxdb-study-series-manual.html