访问 localhost:8086
第一次登入会初始化
这个TOKEN用于后期代码链接访问数据库,忘记了只能删除重新生成
点击QUCK START进入管理页面
windows:在用户文件夹下 C:\Users\Administrator.influxdbv2
linux: /etc/influxdb/influxdb.conf
安装InfluxDB客户端
using System.Linq;
using System.Threading.Tasks;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core;
using InfluxDB.Client.Writes;
namespace Examples
{
public class Examples
{
public static async Task Main(string[] args)
{
// You can generate an API token from the "API Tokens Tab" in the UI
var token = Environment.GetEnvironmentVariable("INFLUX_TOKEN")!;
const string bucket = "Test";
const string org = "CC";
using var client = new InfluxDBClient("http://127.0.0.1:8086", token);
}
}
}
//方式一、使用WriteRecord
const string data = "mem,host=host1 used_percent=23.43234543";
using (var writeApi = client.GetWriteApi())
{
writeApi.WriteRecord(data,bucket, org, WritePrecision.Ns );
}
//方式二、使用WritePoint
var point = PointData
.Measurement("mem")
.Tag("host", "host1")
.Field("used_percent", 23.43234543)
.Timestamp(DateTime.UtcNow, WritePrecision.Ns);
using (var writeApi = client.GetWriteApi())
{
writeApi.WritePoint(point,bucket, org);
}
//方式三、使用实体类
var mem = new Mem { Host = "host1", UsedPercent = 23.43234543, Time = DateTime.UtcNow };
using (var writeApi = client.GetWriteApi())
{
writeApi.WriteMeasurement( mem,bucket, org, WritePrecision.Ns);
}
[Measurement("mem")]
private class Mem
{
[Column("host", IsTag = true)] public string Host { get; set; }
[Column("used_percent")] public double? UsedPercent { get; set; }
[Column(IsTimestamp = true)] public DateTime Time { get; set; }
}
// See https://aka.ms/new-console-template for more information
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Writes;
Console.WriteLine("Hello, World!");
Environment.SetEnvironmentVariable("INFLUX_TOKEN", "O9I2Kpeg...kLPSrQLWhTiJCQPWy6HJFjN9hK33UoLnG34vfFdqZ5KmoDLS-kkw==");
var token = Environment.GetEnvironmentVariable("INFLUX_TOKEN")!;
const string bucket = "Test";
const string org = "CC";
using (var client = new InfluxDBClient("http://localhost", token))
{
using (var writeApi = client.GetWriteApi())
{
while (true)
{
var randon = new Random();
var point = PointData
.Measurement("mem")
.Tag("host", "host1")
.Field("used_percent", randon.Next(10, 100)) //可以添加多个字段
.Field("memory_percent",randon.Next(0,10))
.Timestamp(DateTime.UtcNow, WritePrecision.Ns);
writeApi.WritePoint(point, bucket, org);
Thread.Sleep(2000);
}
}
}