一、简介
Influxdb是一个时序数据库(Time Series Database),适合保存按照时间顺序打点的数据,例如监控信息、日志信息、统计信息等。
官网:https://www.influxdata.com/
二、安装、启动
Mac上安装
brew install influxdb
启动和停止
brew services start influxdb
brew services stop influxdb
上面是后台启动,或者直接前台启动,可以看到输出到console的日志:
influxd
三、基本概念
Influxdb和MySQL很像,不管是db、table的概念,还是SQL语句查询,都很相像。
- 数据库 - database
- 表 - measurement
- 列 - tag
- 值 - value
四、客户端连接
连接
- 连接到本地服务
➜ influx
Connected to http://localhost:8086 version v1.6.3
InfluxDB shell version: v1.6.3
> exit
- 连接到远端服务器的 influxdb 服务
influx -database 'db_name' -host '198.1.1.1' -port '8086'
Connected to http://198.1.1.1:8086 version 1.5.4
InfluxDB shell version: v1.6.3
> exit
五、基本增删查命令
增
> create database db_test
> show databases
name: databases
name
----
_internal
db_test
> use db_test
Using database db_test
> insert measurement_test,tag_a="abc",tag_b="def" count1=123,count2=456,count3=789
> insert measurement_test,tag_a="abc",tag_b="def" count1=123,count2=456,count3=789
> insert measurement_test,tag_a="cba",tag_b="def" count1=123,count2=456,count3=789
> insert measurement_test,tag_a="cba",tag_b="def" count1=123,count2=456,count3=789 1565640883000004648
最后这个insert,可以指定时间。
> show measurements
name: measurements
name
----
measurement_test
>
查
> show measurements
name: measurements
name
----
measurement_test
> select * from measurement_test
name: measurement_test
time count1 count2 count3 tag_a tag_b
---- ------ ------ ------ ----- -----
1547618434743359000 123 456 789 "abc" "def"
1547618524727590000 123 456 789 "abc" "def"
1547619061163184000 123 456 789 "cba" "def"
> select sum(count1) from measurement_test group by tag_a
name: measurement_test
tags: tag_a="abc"
time sum
---- ---
0 246
name: measurement_test
tags: tag_a="cba"
time sum
---- ---
0 123
删
> delete from measurement_test where tag_a="cba"
> drop measurement measurement_test
> show measurements
> drop database db_test
> show databases
name: databases
name
----
_internal
改
不支持改
SELECT, DELETE, SHOW, CREATE, DROP, EXPLAIN, GRANT, REVOKE, ALTER, SET, KILL
注:
- 不支持修改,只支持 select、delete、show、create、drop、explain、grant、revoke、alter、set、kill 这几个关键字;
- 聚合的时候,不需要在 select 里加上要聚合的字段,直接放到 group by 里即可;(不同版本不一样,后来遇到过一个坑,就是版本更新了,必须要在select里加上聚合的字段。好习惯还是有道理的)
六、特别注意
> insert students,stuid=s123 score=22;
ERR: {"error":"unable to parse 'students,stuid=s123 score=22;': invalid number"}
这是为啥呢?
是因为后面的分号!!!不能有分号!!!