InfluxDB数据库操作

查看数据库

> show databases;
name: databases
name
----
_internal

创建数据库

> create database testDB
> show databases;
name: databases
name
----
_internal
testDB

删除数据库

> drop database testDB
> show databases;
name: databases
name
----
_internal

指定使用数据库

> create database testDB
> show databases;
name: databases
name
----
_internal
testDB
> use  testDB
Using database testDB

InfluxDB数据表操作

由于没有创建表的操作,使用insert创建一个measurement表

以下创建了一个disk表,目前有2个tag,2个fileds

insert disk,host=influxdb,ip=192.168.0.150 all=1000,free=500

我们插入2行数据后查询

> select *  from disk
name: disk
time                all  free host     ip
----                ---  ---- ----     --
1533224285135656794 1000 500  influxdb 192.168.0.150
1533224494631037108 1000 500  influxdb 192.168.0.150

系统自动为我们的point插入了时间戳,我们可以自己写入自定义的时间

> insert disk,host=influxdb,ip=192.168.0.150 all=1000,free=500 1533224494631030000
> select *  from disk
name: disk
time                all  free host     ip
----                ---  ---- ----     --
1533224285135656794 1000 500  influxdb 192.168.0.150
1533224494631030000 1000 500  influxdb 192.168.0.150
1533224494631037108 1000 500  influxdb 192.168.0.150

查询全部表

> show measurements;
name: measurements
name
----
disk

删除表

> drop measurement disk
> show measurements;

数据保存策略(Retention Policies)

influxdb不允许用户更改记录,删除记录,只能用数据保存策略来控制数据多少,主要用于指定数据保留时间,超过指定时间,就删除这部分数据。

> show retention policies on "testDB"
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        true

为testDB创建一个新的数据保存策略rp_name并设置保存时间为3周副本数量为2,并设置为默认

> create retention policy "rp_name" on "testDB" duration 3w replication 2 default
> show retention policies on "testDB"
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        false
rp_name 504h0m0s 24h0m0s            2        true

name 名称,此示例名称为 default

duration 持续时间,0代表无限制

shardGroupDuration shardGroup的存储时间,shardGroup是InfluxDB的一个基本储存结构,应该大于这个时间的数据在查询效率上应该有所降低。

replicaN 全称是REPLICATION,副本个数

default 是否是默认策略

因为default不是默认策略在查询使用他的话要显示的加上

> select *  from disk
name: disk
time                all  free host     ip
----                ---  ---- ----     --
1533225620662856389 1000 500  influxdb 192.168.0.150
1533225621422489724 1000 500  influxdb 192.168.0.150
1533225621990951345 1000 500  influxdb 192.168.0.150
1533225622510987682 1000 500  influxdb 192.168.0.150
1533225623911288068 1000 500  influxdb 192.168.0.150
> select *  from "autogen".disk

修改策略

> show retention policies on "testDB"
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        false
rp_name 504h0m0s 24h0m0s            2        true
> alter retention policy "rp_name" ON "testDB" duration 48h 
> show retention policies on "testDB"
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        false
rp_name 48h0m0s  24h0m0s            2        true

删除策略

> drop retention policy "rp_name" on "testDB"
> show retention policies on "testDB"
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        false
> ALTER RETENTION POLICY "autogen" on "testDB" default
> show retention policies on "testDB"
name    duration shardGroupDuration replicaN default
----    -------- ------------------ -------- -------
autogen 0s       168h0m0s           1        true

posted on 2018-08-03 00:13  不忘初心-铃  阅读(187)  评论(0编辑  收藏  举报