安装使用环境为MAC,使用工具安装:mysql
brew update brew install grafana
配置Grafana链接本地安装的mysql,mysql安装不作说明,配置文件列表以下:sql
配置文件应该位于/usr/local/etc/grafana/grafana.ini 日志文件应该位于/usr/local/var/log/grafana/grafana.log 若是你想手动安装一个插件放在这里:/usr/local/var/lib/grafana/plugins 默认的sqlite数据库位于 /usr/local/var/lib/grafana
其中,编辑grafana.ini文件链接数据库,重点配置数据库的链接类型,帐号,密码,数据库名(切记数据库创建对应的数据库)数据库
重启Grafana服务工具
brew tap homebrew/services brew services start grafana
此时网页访问localhost:3000即为Grafana配置数据源页面(默认帐号密码:admin / admin)测试
链接测试后保存,这样在图表展现数据源勾选默认即为mysql指定数据库,或者勾选指定链接名spa
数据库构建表结构,录入测试数据插件
编辑,鉴于展现目的为不按照时序排列的柱状图,可是Grafana的展现要求有时间字段在列,命名与time相关,故查询时添加time字段为当前时间,返回结果能够为两种可用形式,故展现两种SQL查询:日志
select now() as time, case when (score >=80) then '[80, ~)' when (score >=60 and score <80) then '[60, 80)' when (score >=40 and score <60) then '[40, 60)' when (score >=20 and score <40) then '[20, 40)' else '(~, 20)' end grade, count(*) num from grade group by case when (score >=80) then '[80, ~)' when (score >=60 and score <80) then '[60, 80)' when (score >=40 and score <60) then '[40, 60)' when (score >=20 and score <40) then '[20, 40)' else '(~, 20)' end order by 1;
展现结果为:code
select *, now() as time from (select count(*) as '[80, ~)' from grade g where g.score >=80) a, (select count(*) as '[60, 80)' from grade g where g.score >=60 and g.score <80) b, (select count(*) as '[40, 60)' from grade g where g.score >=40 and g.score <60) c, (select count(*) as '[20, 40)' from grade g where g.score >=20 and g.score <40) d, (select count(*) as '(~, 20)' from grade g where g.score <20) e;
展现结果为:orm
重要设置,选择series,选个表明数据是按series分组不是按时间,当前所选时间段进行计算。Y轴仍然表示 值。计算series的avg、min、max、total等。:
效果图为:
两种结果都能正常显示,根据显示规律总结为:
附上官网说明文档,http://docs.grafana.org/