InfluxDB 时序数据库

Summary: Author: 张亚飞 | 阅读时间: 2 minute read | Published: 2018-08-22
Filed under Categories: LinuxTags: Influxdb,

时序数据库线上数据采集方案

采集数据(Metrics)-> 存储数据(InfluxDB) -> 显示数据(Grafana)

Linux 部署安装 InfluxDB

安装 InfluxDB 数据库

  • Ubuntu
wget https://dl.influxdata.com/influxdb/releases/influxdb_1.5.3_amd64.deb
sudo dpkg -i influxdb_1.5.3_amd64.deb

卸载

sudo dpkg -r influxdb
sudo dpkg -P influxdb
  • CentOS.7
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.5.3.x86_64.rpm
sudo yum localinstall influxdb-1.5.3.x86_64.rpm

管理配置

配置文件 /etc/influxdb/influxdb.conf

systemctl start influxdb
systemctl status influxdb
systemctl stop influxdb

downloads#influxdb

数据管理

创建 httpauth 认证用户

Sat Jun 23 12:31:27 coam@a.us.1:~$ influx -precision rfc3339
Connected to http://localhost:8086 version 1.5.3
InfluxDB shell version: 1.5.3
> CREATE DATABASE coam
> SHOW DATABASES
name: databases
name
----
_internal
coam
> USE coam
Using database coam
> CREATE USER coam WITH PASSWORD '123456' WITH ALL PRIVILEGES
> show users
user admin
---- -----
coam true

登录 InfluxDB 数据库

Sun Jun 24 13:02:50 coam@a.us.1:~$ influx -username coam -password 123456
Connected to http://localhost:8086 version 1.5.3
InfluxDB shell version: 1.5.3
> show databases
name: databases
name
----
_internal
coam
> use coam
Using database coam
> show measurements
name: measurements
name
----
cpu_load_short
tidea
tides

开启 http 授权配置

[http]
  enabled = true
  bind-address = ":8086"
  auth-enabled = true
  log-enabled = true
  write-tracing = false
  pprof-enabled = false
  https-enabled = false
  https-certificate = "/etc/ssl/influxdb.pem"

重启 InfluxDB

systemctl status influxdb

测试授权连接

Sun Jun 24 12:58:13 coam@a.us.1:~$ curl -G http://localhost:8086/query -u coam:123456 --data-urlencode "q=SHOW DATABASES"
{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"],"values":[["_internal"],["coam"]]}]}]}

写入数据

curl -i -XPOST 'http://localhost:8086/write?db=coam' -u coam:123456 --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'

查询数据

curl -G 'http://localhost:8086/query?pretty=true' -u coam:123456 --data-urlencode "db=coam" --data-urlencode "q=SELECT \"value\" FROM \"cpu_load_short\" WHERE \"region\"='us-west'"
curl -G 'http://localhost:8086/query?pretty=true' -u coam:123456 --data-urlencode "db=coam" --data-urlencode "q=SELECT * FROM tide"
curl -G 'http://localhost:8086/query?pretty=true' -u coam:123456 --data-urlencode "db=coam" --data-urlencode "q=SELECT * FROM cpu_load_short"

数据保存策略

influx -host '119.23.150.32' -username 'evs' -password '666666'

> show measurements
name: measurements
name
----
access
api_status
online_evs

首先检索统计数据.

select count(*) from access
select count(*) from api_status
select count(*) from online_evs

设置保留 3 天内的时序策略.

> SHOW RETENTION POLICIES ON "evs"
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        true

CREATE RETENTION POLICY "evs_auto_retain" ON "evs" DURATION 3d REPLICATION 1 DEFAULT
> SHOW RETENTION POLICIES ON "evs"
name            duration shardGroupDuration replicaN default
----            -------- ------------------ -------- -------
autogen         0s       168h0m0s           1        false
evs_auto_retain 72h0m0s  24h0m0s            1        true

修改时序策略

ALTER RETENTION POLICY "evs_auto_retain" ON "evs" DURATION 1d REPLICATION 1 DEFAULT
> SHOW RETENTION POLICIES ON "evs"
name            duration shardGroupDuration replicaN default
----            -------- ------------------ -------- -------
autogen         0s       168h0m0s           1        false
evs_auto_retain 24h0m0s  24h0m0s            1        true

删除时序策略

DROP RETENTION POLICY "evs_auto_retain" ON "evs"

再次查看时序数据.

select count(*) from online_evs

安装 Grafana

  • Ubuntu
wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.1.4_amd64.deb
sudo dpkg -i grafana_5.1.4_amd64.deb

卸载

sudo dpkg -r grafana
sudo dpkg -P grafana
  • CentOS.7
wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.1.4-1.x86_64.rpm
sudo yum localinstall grafana-5.1.4-1.x86_64.rpm

管理配置

配置文件 /etc/grafana/grafana.ini

systemctl start grafana-server
systemctl status grafana-server
systemctl stop grafana-server

管理 Grafana

访问 http://47.244.154.194:3000 使用默认管理员账号密码 admin:admin 登录

注意: 坑:使用 Firefox 添加数据源测试连接提示 Network Auth(401) 错误,后来切换到 Chrome 成功


Comments

  • 牧马人 says: 2019-09-15 22:38:48

    江山代有才人出,各领风骚数百年.

  • 牧马人 says: 2019-09-23 07:26:28

    江山代有才人出,各领风骚数百年.

Cor-Ethan, the beverage → www.iirii.com