文件版本:mysql |
V1.0linux |
文件编号:git |
R&D0031github |
发布日期:golang |
编 制:web |
||
审 批:sql |
Influxdb relay + grafana服务器shell
目录数据库
1.简介3ubuntu
2.5.5 数据库保存策略(Retention Policies)10
4.6 influxdb-relay节点故障数据写入同步17
修订记录
版本号 |
发布日期 |
拟制人 |
修订描述 |
V1.0 |
2017-01-17 |
首次发布 |
|
V1.1 |
2017-02-14 |
新增帐号和密码 |
|
influxdb服务器
InfluxDB用Go语言编写的一个开源分布式时序、事件和指标数据库,和传统是数据库相比有很多不一样的地方。相似的数据库有Elasticsearch、Graphite等。
1.提供了Http接口的API来操做数据
2.提供了相似sql的数据库语句
3.基于时间序列,支持与时间有关的相关函数(如最大,最小,求和等)
4.可度量性:你能够实时对大量数据进行计算
5.基于事件:它支持任意的事件数据
6.无结构(无模式):能够是任意数量的列
7.可拓展的支持min, max, sum, count, mean, median 等一系列函数,方便统计
8.原生的HTTP支持,内置HTTP API
9.强大的类SQL语法
10.自带管理界面,方便使用
库、表等比较:
influxDB |
传统数据库中的概念 |
database |
数据库 |
measurement |
数据库中的表 |
points |
表里面的一行数据 |
influxdb数据的构成:
Point由时间戳(time)、数据(field)、标签(tags)组成。
Point属性 |
传统数据库中的概念 |
time |
每一个数据记录时间,是数据库中的主索引(会自动生成) |
fields |
各类记录值(没有索引的属性)也就是记录的值:温度, 湿度 |
tags |
各类有索引的属性:地区,海拔 |
这里不得不提另外一个名词:series:
全部在数据库中的数据,都须要经过图表来展现,而这个series表示这个表里面的数据,能够在图表上画成几条线:经过tags排列组合算出来。具体能够经过SHOW SERIES FROM "表名" 进行查询。
通常用来储存实时数据,配合一套UI界面来展现信息。
基本环境,准备。
root@ubuntu:~# ls
grafana_4.1.1-1484211277_amd64.deb influxdb_1.1.1_amd64.deb
root@ubuntu:~# cat /etc/issue
Ubuntu 14.04.5 LTS \n \l
root@ubuntu:~# uname -a
Linux ubuntu 3.16.0-30-generic #40~14.04.1-Ubuntu SMP Thu Jan 15 17:43:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
开始安装
root@ubuntu:~# dpkg -i influxdb_1.1.1_amd64.deb
Selecting previously unselected package influxdb.
(Reading database ... 90134 files and directories currently installed.)
Preparing to unpack influxdb_1.1.1_amd64.deb ...
Unpacking influxdb (1.1.1-1) ...
Setting up influxdb (1.1.1-1) ...
Adding system startup for /etc/init.d/influxdb ...
/etc/rc0.d/K20influxdb -> ../init.d/influxdb
/etc/rc1.d/K20influxdb -> ../init.d/influxdb
/etc/rc6.d/K20influxdb -> ../init.d/influxdb
/etc/rc2.d/S20influxdb -> ../init.d/influxdb
/etc/rc3.d/S20influxdb -> ../init.d/influxdb
/etc/rc4.d/S20influxdb -> ../init.d/influxdb
/etc/rc5.d/S20influxdb -> ../init.d/influxdb
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
root@ubuntu:~# /etc/init.d/influxdb start
Starting the process influxdb [ OK ]
influxdb process was started [ OK ]
root@ubuntu:~# netstat -lnp|grep influ
tcp6 0 0 :::8083 :::* LISTEN 5057/influxd
tcp6 0 0 :::8086 :::* LISTEN 5057/influxd
tcp6 0 0 :::8088 :::* LISTEN 5057/influxd
8083 web管理端 http://ip:8083 用户名和密码都是admin, InfluxDB 的 Web 管理界面端口是 8083
8086 HTTP API 监听端口是 8086
8088 集群端口(目前还不是很清楚, 配置在全局的bind-address,默认不配置就是开启的)
启动成功以后,咱们就能够开始使用influxdb啦!
root@ubuntu:~# which influx
/usr/bin/influx
root@ubuntu:~# influx
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 0.13.0
InfluxDB shell version: 0.13.0
>
> show databases
name: databases
name
----
_internal
> CREATE DATABASE "testDB" #建立数据库
> show databases #建立数据库
name: databases
name
----
_internal
testDB
> use testDB #使用testDB数据库类mysql
Using database testDB
Web界面访问http://ip:8083,
注意:新版本默认是关闭该web展现,详见后面FAQ具体解决方法:
经过界面操做命令。
1. 先添加用户
设置TS的authorized,提升安全性,针对指定用户拥有权限才能访问数据库的数据,TS默认用户分为普通用户和管理员用户,权限分为read,write,all privileges三种权限
root@ubuntu:/home/leco# influx
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 1.1.1
InfluxDB shell version: 1.1.1
> CREATE USER "influxdb" WITH PASSWORD 'influxdb' WITH ALL PRIVILEGES
先添加帐号和密码,均为influxdb
Web界面数据库的切换。此时登陆web的时候须要输入帐号和密码,验证经过后才能够正常进入。配置以下:
2. 重启influxdb
root@ubuntu:~# /etc/init.d/influxdb restart
Stopping influxdb...
influxdb process was stopped [ OK ]
Starting influxdb...
influxdb process was started [ OK ]
经过界面上输入正确的IP,端口,用户名和密码点击save进入。
如果设置了密码验证的话必须就要输入帐号和密码才能正常登陆操做,不然报错以下
1. 错误登陆
root@ubuntu:/home/leco# influx
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 1.1.1
InfluxDB shell version: 1.1.1
> show databases;
ERR: unable to parse authentication credentials #说明已经添加了验证方式
Warning: It is possible this error is due to not setting a database.
Please set a database with the command "use <database>".
> quit
2. 正确登陆
root@ubuntu:/home/leco# influx -host 'localhost' -port '8086' -username 'influxdb' -password 'influxdb'
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 1.1.1
InfluxDB shell version: 1.1.1
> show databases;
name: databases
name
----
_internal
cmz
#此时localhost是链接本机,如果远程链接就换成对应的IP。Port是influxdb默认端口8086,username 和password就是受权的帐号和密码。
> use testDB
Using database testDB
> insert weather,altitude=1000,area=北 temperature=11,humidity=-4
或者经过HTTP接口增长
curl -i -XPOST 'http://localhost:8086/write?db=testDB' --data-binary 'weather,altitude=1000,area=北 temperature=11,humidity=-4'
Line Protocol格式
插入数据的格式彷佛比较奇怪,这是由于influxDB储存数据所采用的是Line Protocol格式。
在上面两个插入数据的方法中,都有同样的部分。
weather,altitude=1000,area=北 temperature=11,humidity=-4
其中:
1. weather : 表名
2. altitude=1000,area=北 : tag
3. temperature=11,humidity=-4 :field
参考官方文档:
https://docs.influxdata.com/influxdb/v0.10/write_protocols/line/
> use testDB
Using database testDB
> SELECT * FROM weather ORDER BY time
name: weather
timealtitudeareahumiditytemperature
-----------------------------------
14850746132673340411000北-411
经过Http接口
curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=testDB" --data-urlencode "q=SELECT * FROM weather ORDER BY time DESC LIMIT 3"
InfluxDB是支持类SQL语句的,具体的查询语法都差很少,就再也不详细描述了。详见官方文档
#建立数据库,库名为db_name
> CREATE DATABASE "db_name"
#显示全部数据库
> SHOW DATABASES
name: databases
name
----
_internal
testDB
db_name
#删除库名字为db_name的库
> DROP DATABASE "db_name"
#切换到testDB数据库
> USE testDB
Using database testDB
#显示该数据库中的表
> SHOW MEASUREMENTS
name: measurements
name
----
weather
#建立表
#直接在插入数据的时候指定表名字(weather就是表名)
> insert weather,altitude=1000,area=北 temperature=11,humidity=-4
#删除weather表
> DROP MEASUREMENT "weather"
InfluxDB没有提供直接删除Points的方法,可是它提供了Retention Policies。主要用于指定数据的保留时间:当数据超过了指定的时间以后,就会被删除。
#1.查看当前数据库的Retention Policies
> SHOW RETENTION POLICIES ON "testDB"
namedurationshardGroupDurationreplicaNdefault
---------------------------------------------
autogen0s168h0m0s1true
#2.建立新的Retention Policies
> CREATE RETENTION POLICY "rp_name" ON "testDB" DURATION 30d REPLICATION 1 DEFAULT
说明:
1. rp_name:策略名
2. testDB:具体的数据库名
3. 30d:保存30天,30天以前的数据将被删除
它具备各类时间参数,好比:h(小时),w(星期)
4. REPLICATION 1:副本个数,这里填1就能够了
5. DEFAULT 设为默认的策略
#2.修改Retention Policies
> ALTER RETENTION POLICY "rp_name" ON "testDB" DURATION 3w DEFAULT
#3.删除Retention Policies
> DROP RETENTION POLICY "rp_name" ON "testDB"
当数据超过保存策略里指定的时间以后,就会被删除。若是咱们不想彻底删除掉,好比作一个数据统计采样:把原先每秒的数据,存为每小时的数据,让数据占用的空间大大减小(以下降精度为代价)。这就须要InfluxDB提供的:连续查询(Continuous Queries)。
#1.当前数据库的continus Queries
> SHOW CONTINUOUS QUERIES
name: _internal
namequery
---------
name: testDB
namequery
---------
#2.建立新的continus Queries
#显示用户
> SHOW USERS
useradmin
---------
#建立用户
> CREATE USER "realcloud" WITH PASSWORD '123456'
#建立管理员权限的用户
> CREATE USER "cmz" WITH PASSWORD 'cmz' WITH ALL PRIVILEGES
#删除用户
> DROP USER "cmz"
数据最终是须要一套UI来展现的,而这种实时数据的展现,已经有很多项目了。 好比:
1. 官方的Chronograf
2. Grafana
3. 其它...
此时我选择的使用Grafana
root@ubuntu:~# ls
grafana_4.1.1-1484211277_amd64.deb influxdb_1.1.1_amd64.deb
#安装
root@ubuntu:~# dpkg -i grafana_4.1.1-1484211277_amd64.deb
Selecting previously unselected package grafana.
(Reading database ... 61968 files and directories currently installed.)
Preparing to unpack grafana_4.1.1-1484211277_amd64.deb ...
Unpacking grafana (4.1.1-1484211277) ...
Setting up grafana (4.1.1-1484211277) ...
Adding system user `grafana' (UID 106) ...
Adding new user `grafana' (UID 106) with group `grafana' ...
Not creating home directory `/usr/share/grafana'.
### NOT starting grafana-server by default on bootup, please execute
sudo update-rc.d grafana-server defaults 95 10
### In order to start grafana-server, execute
sudo service grafana-server start
Processing triggers for ureadahead (0.100.0-16) ...
ureadahead will be reprofiled on next reboot
#启动
root@ubuntu:~# /etc/init.d/grafana-server start
* Starting Grafana Server [ OK ]
安装步骤参考:http://grafana.org/download/
1) Web登陆,直接访问:http://your_ip:3000
默认账号:admin
默认密码:admin
2) 在Data Sources中添加数据库testDB
其中user和password,若是没有设置过,能够随便填下。
保存以后,能够经过Test Connection来测试,是否填写正确。
点击New按钮就能够了。
该项目为influxdb添加了一个基本的高可用性层。经过正确的体系结构和灾难恢复过程,实现了高度可用的设置。
提示: influxdb-relay must be built with Go 1.5+
参考连接:
https://github.com/influxdata/influxdb-relay/blob/master/README.md
体系结构至关简单,包括负载平衡器、两个或多个influxdb中继进程和两个或多个influxdb进程。负载平衡器应使用路径/写入将UDP通讯和http post请求指向两个继电器,同时将路径/查询指向两个influxdb服务器。安装程序应该以下:
给个人感受,这个 influxdb-relay是一个数据库中间件的角色,起到了一个代理的做用
1.实验环境
IP |
系统环境 |
安装软件 |
|
192.168.5.200 |
Ubuntu14.04 x64 |
influxdb |
influxdb-relay |
192.168.5.202 |
Ubuntu14.04 x64 |
influxdb |
此时上文只在200机器上安装了influxdb,此时要在202上也要安装influxdb。也能够只安装在一个机器上,我分开安装了,更贴切实际生产状况。
2. 安装golang环境
influxdb release的deb包自己是已经编译好的,并不须要golang的环境。而influxdb-relay目前还只是存放在github上,所以须要先安装golang环境。 手动下载更高版本的golang是须要的。
3. 下载并解压1.6.2版本的golang压缩包
root@ubuntu:~# apt-get install git
root@ubuntu:~# wget http://www.golangtc.com/static/go/1.6.2/go1.6.2.linux-amd64.tar.gz
root@ubuntu:~# ls go1.6.2.linux-amd64.tar.gz
go1.6.2.linux-amd64.tar.gz
root@ubuntu:~# tar -zxf go1.6.2.linux-amd64.tar.gz -C /usr/local/
4. 配置环境变量
root@ubuntu:~# tail -9 /etc/profile
#add by caimengzhi at 2017-01-23 for go. start
export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export GOPKG=$GOROOT/pkg/tool/linux_amd64
export GOARCH=amd64
export GOOS=linux
export GOPATH=$HOME
export PATH=$PATH:$GOBIN:$GOPKG:$GOPATH/bin
#add by caimengzhi at 2017-01-23 for go. End
root@ubuntu:~# source /etc/profile
root@ubuntu:~# go version
go version go1.6.2 linux/amd64
root@ubuntu:~# GOPATH=/root/go
root@ubuntu:~# go get github.com/influxdata/influxdb-relay
root@ubuntu:~# ls
go go1.6.2.linux-amd64.tar.gz grafana_4.1.1-1484211277_amd64.deb influxdb_1.1.1_amd64.deb src
#此时会在/root下发现go文件
root@ubuntu:~# cp $GOPATH/src/github.com/influxdata/influxdb-relay/sample.toml ./relay.toml
root@ubuntu:~# vim relay.toml
-bash: /root/go/bin/influxdb-relay: No such file or directory
root@ubuntu:~# which influxdb-relay
/usr/local/go/bin/influxdb-relay
5. 配置文件解释
root@ubuntu:~# cat relay.toml
`http`
name = "example-http"
bind-addr = "192.168.5.200:9096"
output = [
{ name="local1", location = "http://192.168.5.200:8086/write" },
{ name="local2", location = "http://192.168.5.202:8086/write" },
]
`udp`
name = "example-udp"
bind-addr = "192.168.5.200:9096"
read-buffer = 0 # default
output = [
{ name="local1", location="192.168.5.200:8089", mtu=512 },
{ name="local2", location="192.168.5.202:8089", mtu=1024 },
]
root@ubuntu:~# /usr/local/go/bin/influxdb-relay -config relay.toml >/tmp/influxdb_relay.log 2>/dev/null &
[1] 10943
再指定以前influxdb-relay配置的端口9096,成功进入其余的influxdb
root@ubuntu:~# influx -port 9096
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:9096 version relay
InfluxDB shell version: 1.1.1
验证能够经过influxdb-relay写数据
root@ubuntu:~#
curl -i -XPOST 'http://localhost:9096/write?db=testDB' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
2017/01/23 17:53:43 Problem posting to relay "example-http" backend "local2": Post http://127.0.0.1:7086/write?db=testDB: dial tcp 127.0.0.1:7086: getsockopt: connection refused
HTTP/1.1 204 No Content
Date: Mon, 23 Jan 2017 09:53:43 GMT
1. 进入节点1,influxdb验证数据写入
root@ubuntu:~# influx
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:8086 version 1.1.1
InfluxDB shell version: 1.1.1
> use testDB
Using database testDB
> select * from cpu_load_short
name: cpu_load_short
timehostregionvalue
-------------------
1434055562000000000server01us-west0.64
2. 进入节点2,influxdb验证数据写入
> use testDB;
Using database testDB
> select * from cpu_load_short
name: cpu_load_short
timehostregionvalue
-------------------
1434055562000000000server01us-west0.64
从上能够看出经过调用relay的端口9096.写入数据,两个节点都会有相对数据写入。
注意点:
### 缺陷,不知道是否是坑,反正在官网的github上没看到这个注意点
# 手动在全部节点上建立数据库
write操做并不包含建立数据库,因此须要手动在全部节点上提早建立数据库
# 故障节点操做
1.中止节点1的influxdb
root@loocha15:~# /etc/init.d/influxdb stop
Stopping influxdb...
influxdb process was stopped [ OK ]
2.此时继续写入数据
curl -i -XPOST 'http://localhost:9096/write?db=cmz' --data-binary 'cpu_load_short,host=beijing,region=east value=2 2222222222'
2017/02/06 15:19:26 Problem posting to relay "example-http" backend "local1": Post http://192.168.3.35:8086/write?db=cmz: dial tcp 192.168.3.35:8086: getsockopt: connection refused
HTTP/1.1 204 No Content
Date: Mon, 06 Feb 2017 07:19:26 GMT
3.节点2 验证数据
> use cmz;
Using database cmz
> select * from cpu_load_short
name: cpu_load_short
timehostregionvalue
-------------------
1111111111nanjingeast123
2222222222beijingeast2 #这一条就是节点1故障后,写入的数据
1434055562000000000server01us-west0.64
到此截止influxdb的HA已经测试完毕。
1. WEB显示404
root@ubuntu:~# curl localhost:8086 -I
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
X-Influxdb-Version: 1.1.1
Date: Sun, 22 Jan 2017 09:36:48 GMT
Content-Length: 19
Influxdb新版本web界面不展现解决方法
1.新安装influxdb web界面不展现
访问的时候404.
解决方法:
1、问题缘由
InfluxDB在0.13版本之后,就默认关闭了web管理页面,而国内的文档大多都以旧版的InfluxDB为标准写的,因此下载安装好最新版本之后,就会出现8083端口的web管理页面访问不了的问题。
2、解决方案
新版的InfluxDB虽然默认关闭了web管理页面,但咱们能够经过很简单的方式进行开启。
打开配置文件,vim /etc/influxdb/influxdb.conf 找到以下几行:
修改后以下:
重启服务
root@ubuntu:~# /etc/init.d/influxdb restart
influxdb process already stopped [ OK ]
Starting influxdb...
influxdb process was started [ OK ]
查看端口号8083(以前是没有的)
root@ubuntu:~# netstat -lnp|grep influ
tcp6 0 0 :::8083 :::* LISTEN 5057/influxd
tcp6 0 0 :::8086 :::* LISTEN 5057/influxd
tcp6 0 0 :::8088 :::* LISTEN 5057/influxd
Web界面访问展现:
http://ip:8083
OK.
2. command
root@ubuntu:~# go get -u github.com/influxdata/influxdb-relay
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/influxdata/influxdb-relay: exec: "git": executable file not found in $PATH
解决:
root@ubuntu:~# apt-get install git
3.relay命令行管理不了
root@ubuntu:~# !net
netstat -lnp|grep 9096
tcp 0 0 127.0.0.1:9096 0.0.0.0:* LISTEN 3502/influxdb-relay
udp 0 0 127.0.0.1:9096 0.0.0.0:* 3502/influxdb-relay
root@ubuntu:~# influx -port 9096
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
Connected to http://localhost:9096 version relay
InfluxDB shell version: 1.1.1
> show database;
ERR: invalid write endpoint
Warning: It is possible this error is due to not setting a database.
Please set a database with the command "use <database>".