Docker下安装Influxdb和Grafana

下载influxdb

docker pull influxdb

启动Influxdb

 docker run -d -p 8083:8083 -p 8086:8086 --name yw_influxdb influxdb

进入docker镜像:

docker exec -it yw_influxdb bash

进入/usr/bin目录,这里面有Influxdb的工具

root@3ae7203adc87:/usr/bin# find | grep influx  
./influx
./influx_inspect
./influx_stress
./influx_tsm
./influxd

查看Influxdb版本

./influx -version

进入Influxdb客户端命令行

root@3ae7203adc87:/usr/bin# ./influx
Connected to http://localhost:8086 version 1.8.1
InfluxDB shell version: 1.8.1
> show databases
name: databases
name
----
_internal
>

 创建数据库

复制代码
> create database my_test
> show databases
name: databases
name
----
_internal
my_test

 

 删除数据库

drop database [db_name]

使用数据库

> use my_test
Using database my_test

现在写个定时程序,不断向数据库添加数据

建立一个SpringBoot工程

导入依赖

     <!--influxdb-->
        <dependency>
            <groupId>org.influxdb</groupId>
            <artifactId>influxdb-java</artifactId>
            <version>2.12</version>
        </dependency>

入口类

package com.yw.influxdb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Random;

@SpringBootApplication
@EnableScheduling
public class InfluxdbApplication {

    public static void main(String[] args) {
        SpringApplication.run(InfluxdbApplication.class, args);
    }
    @Scheduled(fixedRate = 1000)
    public void doInsert(){
        Random random = new Random();
        TestInfluxdb.insert(random.nextInt(1000));
    }
}

Influxdb类

package com.yw.influxdb;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;

public class TestInfluxdb {
    public static void insert(int num){
        InfluxDB db = InfluxDBFactory.connect("http://ip:8086", "admin", "admin");
        db.setDatabase("my_test");  // 设置数据库
        Point.Builder builder = Point.measurement("test_demo");  // 创建Builder,设置表名
        builder.addField("count",num);  // 添加Field
        builder.tag("TAG_CODE","TAG_VALUE_" + num);    // 添加Tag
        Point point = builder.build();
        db.write(point);
    }
}

当你启动,每隔1秒自动向数据库添加

这个时候,显示表列表

> show measurements
name: measurements
name
----
test_demo
>

查看表

> select * from test_demo
name: test_demo
time TAG_CODE count
---- -------- -----
1597547629212946974 TAG_VALUE_637 637
1597547629828049735 TAG_VALUE_303 303
1597547630824162758 TAG_VALUE_319 319
1597547631819945755 TAG_VALUE_140 140
1597547632822037463 TAG_VALUE_327 327
1597547633821145219 TAG_VALUE_459 459
1597547634822056642 TAG_VALUE_568 568
1597547635819052214 TAG_VALUE_769 769
1597547636822081944 TAG_VALUE_764 764
1597547637823929501 TAG_VALUE_654 654
1597547638821020371 TAG_VALUE_426 426
1597547639823957155 TAG_VALUE_203 203
1597547640820033307 TAG_VALUE_941 941
1597547641819040398 TAG_VALUE_64 64
1597547642822036636 TAG_VALUE_853 853
1597547643826923504 TAG_VALUE_565 565
1597547644820044359 TAG_VALUE_557 557
1597547645821021851 TAG_VALUE_201 201
1597547646820033078 TAG_VALUE_631 631
1597547647822099932 TAG_VALUE_327 327
1597547648814034190 TAG_VALUE_129 129
1597547649837018799 TAG_VALUE_241 241
1597547650821027851 TAG_VALUE_596 596
1597547651814043973 TAG_VALUE_345 345
1597547652819090551 TAG_VALUE_713 713
1597547653817880349 TAG_VALUE_188 188
1597547654818175956 TAG_VALUE_38 38

删除表

drop measurement 【measurement_name】

第二步、安装Grafana

 docker pull grafana/grafana

运行

docker run -d -p 3000:3000 --name yw_grafana grafana/grafana

访问http://ip:3000

账号密码:admin/admin,进去之后让你修改密码

进入页面

 

 添加数据源

然后点击,选择Home

 

 over

 

posted @ 2020-08-16 11:37  愿无违  阅读(384)  评论(0编辑  收藏  举报