欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

centos7安装与使用influxdb1.7.8

程序员文章站 2024-03-19 17:13:16
...

1 安装

# wget https://dl.influxdata.com/influxdb/releases/influxdb-1.7.8.x86_64.rpm
# sudo yum localinstall influxdb-1.7.8.x86_64.rpm 

2 启动

#启动
sudo systemctl start influxdb

#配置开机启动
sudo systemctl enable influxdb

3 防火墙配置

#开放端口
# firewall-cmd --add-port=8086/tcp --permanent

#重载防火墙配置
# firewall-cmd --reload

4 启动cli命令

[aaa@qq.com 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 创建数据库

[aaa@qq.com ~]# 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}]}
[aaa@qq.com ~]#

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查询数据

[aaa@qq.com ~]# 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]]}]}]}
[aaa@qq.com ~]#

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 cli终端账号密码登录

[aaa@qq.com 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 http账号密码登录,-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

技术交流

CleverCode是一名架构师,技术交流,咨询问题,请加CleverCode创建的qq群(架构师俱乐部):517133582。加群和腾讯,阿里,百度,新浪等公司的架构师交流。【架构师俱乐部】宗旨:帮助你成长为架构师!
centos7安装与使用influxdb1.7.8

相关标签: 架构师