influxDB簡介
influxDB是近年流行的一種時(shí)序數(shù)據(jù)庫婉商,專用于時(shí)序數(shù)據(jù)的存儲和查詢似忧, 從其功能介紹和適用場景看,可以說是專為監(jiān)控系統(tǒng)而設(shè)計(jì)丈秩。
以下測試基于CentOS 7.5盯捌,使用influxDB 1.7.9。
安裝
RPM本地安裝
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.7.9.x86_64.rpm
sudo yum localinstall influxdb-1.7.9.x86_64.rpm
docker安裝
docker search influxdb
docker pull influxdb
sudo docker run -d -p 8083:8083 -p8086:8086 --expose 8090 --expose 8099 --name influxDbService influxdb
詳見:https://docs.influxdata.com/influxdb/v1.7/introduction/installation/
服務(wù)管理
啟動(dòng)
安裝后成為系統(tǒng)服務(wù)蘑秽,如centos7.5系統(tǒng)下饺著,使用systemctl start influxdb啟動(dòng)
查看服務(wù)狀態(tài):systemctl status influxdb
停止服務(wù):systemctl stop influxdb
配置
配置文件路徑:/etc/influxdb/influxdb.conf
典型配置項(xiàng):
數(shù)據(jù)存儲路徑,默認(rèn)為/var/lib/influxdb/data肠牲。
端口幼衰,默認(rèn)為8086。
基本概念
- database
類比MySQL中的數(shù)據(jù)庫
- measurements
類比MySQL中的表埂材,對于監(jiān)控系統(tǒng)塑顺,可對應(yīng)一個(gè)指標(biāo)。
- tag、field
tag和field可類比MySQL表中的字段严拒,區(qū)別是tag可認(rèn)為是加了索引的字段扬绪,而field則是未加索引的字段。
tag的值只能是string裤唠,field的值可以是string, float, integer, Boolean挤牛。
- point
類比MySQL表中的一行,都會(huì)帶時(shí)間戳种蘸。
- retention policy
數(shù)據(jù)保持策略墓赴,指明數(shù)據(jù)多久失效、數(shù)據(jù)保存幾個(gè)副本
- series
相同retention policy航瞭,相同measurement诫硕、tag key和value的一組數(shù)據(jù)(point)。
- 數(shù)據(jù)格式
<measurement>[,<tag-key>=<tag-value>...] <field-key>=<field-value>[,<field2-key>=<field2-value>...] [unix-nano-timestamp]
如:cpu,host=serverA,region=us_west value=0.64
注意不要習(xí)慣性地多加空格刊侯,會(huì)報(bào)錯(cuò)章办。
詳見:https://docs.influxdata.com/influxdb/v1.7/concepts/key_concepts/
使用
客戶端CLI
influxDB支持的類sql名為influxQL,與SQL比較接近滨彻。
執(zhí)行命令influx進(jìn)入客戶端藕届。
- 查看數(shù)據(jù)庫:
show databases;
- 創(chuàng)建數(shù)據(jù)庫:
create database my_db;
- 查看measurements(類似MySQL表):
show measurements;
- 創(chuàng)建measurements:不需要提前創(chuàng)建,直接寫數(shù)據(jù)即可亭饵。
- 寫數(shù)據(jù)
按照上面的數(shù)據(jù)格式休偶,注意不要多加空格。
insert cpu,host=serverA,region=us_west value=0.64
- 查詢數(shù)據(jù)
select count(*) from "cpu";
select * from "cpu";
select value from "cpu";
select * from "cpu" limit 1;
//注意不支持limit 1,10這種寫法辜羊。
select * from "cpu" order by time desc limit 1;
select * from cpu where host::tag='serverA';//條件查詢踏兜,按tag查
select * from cpu where value::field > 0.5; //條件查詢,按field查
select * from cpu where host::tag='serverA' and value::field > 0.5;//多條件查詢
select max(value) from cpu where host::tag='serverA';//使用聚合函數(shù)
詳見:https://docs.influxdata.com/influxdb/v1.7/query_language/spec/
http接口
CLI方式只適合調(diào)試和維護(hù)八秃,程序調(diào)用需要用http接口庇麦。
使用方式與CLI類似,傳入適當(dāng)參數(shù)即可喜德。
以curl命令為例:
創(chuàng)建數(shù)據(jù)庫:
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb"
寫一條數(shù)據(jù):
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
寫多條數(shù)據(jù):
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257'
從文件加載數(shù)據(jù):
如文件cpu_data.txt中存儲了數(shù)據(jù):
cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
執(zhí)行命令:
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary @cpu_data.txt`