InfluxDB
Configuration
/etc/influxdb/influxdb.conf
[http]
auth-enabled = true
Management
Create a new user and database
influx
> CREATE USER admin WITH PASSWORD 'adminpass' WITH ALL PRIVILEGES
> exit
influx -username admin -password adminpass
> set password for admin = 'newpass'
>
> create database mmap_nmon with duration 180d
> create user mon with password 'thisispassword'
> grant ALL on mmap_nmon to mon
> show GRANTS for mon
Login to InfluxDB
# In the shell
influx -username admin -password thisispass
# In the InfluxDB CLI
> auth
username:
password
InfluxQL
> show databases
name: databases
name
----
_internal
nmon_reports
nmon2influxdb_log
> show users
user admin
---- -----
admin true
mon false
> use nmon_reports
Using database nmon_reports
> show measurements
name: measurements
name
----
CPU_ALL
DISKAVGRIO
DISKAVGWIO
DISKBSIZE
DISKBUSY
DISKREAD
DISKREADSERV
DISKRIO
DISKRXFER
DISKSERV
...
Retention Policy
# for current database
> show retention policies
name duration shardGroupDuration replicaN default
---- -------- ------------------ -------- -------
autogen 8760h0m0s 168h0m0s 1 true
# for specified database
> show retention policies on nmon2influxdb_log
name duration shardGroupDuration replicaN default
---- -------- ------------------ -------- -------
autogen 0s 168h0m0s 1 false
log_retention 48h0m0s 24h0m0s 1 true
# Create a policy
> CREATE RETENTION POLICY "one_day_only" ON "NOAA_water_database" DURATION 1d REPLICATION 1
# Alter the policy
> ALTER RETENTION POLICY "what_is_time" ON "NOAA_water_database" DURATION 3w SHARD DURATION 2h DEFAULT
# Delete a policy
> DROP RETENTION POLICY "what_is_time" ON "NOAA_water_database"
Verify the account
curl -G http://localhost:8086/query -u mon:thisispassword --data-urlencode "q=SHOW DATABASES"
{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"],"values":[["mmap_nmon"]]}]}]}
查詢表資訊
SHOW MEASUREMENTS --查詢當前資料庫中含有的表
SHOW FIELD KEYS --查看當前資料庫所有表的欄位
SHOW series from pay --查看key數據
SHOW TAG KEYS FROM "pay" --查看key中tag key值
SHOW TAG VALUES FROM "pay" WITH KEY = "merId" --查看key中tag 指定key值對應的值
SHOW TAG VALUES FROM cpu WITH KEY IN ("region", "host") WHERE service = 'redis'
DROP SERIES FROM WHERE ='' --刪除key
SHOW CONTINUOUS QUERIES --查看連續執行命令
SHOW QUERIES --查看最後執行命令
KILL QUERY --結束命令
SHOW RETENTION POLICIES ON mydb --查看保留數據
查詢資料
SELECT * FROM /.*/ LIMIT 1 --查詢當前資料庫下所有表的第一行記錄
select * from pay order by time desc limit 2
select * from db_name."POLICIES name".measurement_name --指定查詢資料庫下資料保留中的表資料 POLICIES name資料保留
刪除資料
delete from "query" --刪除表所有資料,則表就不存在了
drop MEASUREMENT "query" --刪除表(注意會把資料保留刪除使用delete不會)
DELETE FROM cpu
DELETE FROM cpu WHERE time < '2000-01-01T00:00:00Z'
DELETE WHERE time < '2000-01-01T00:00:00Z'
DROP DATABASE “testDB” --刪除資料庫
DROP RETENTION POLICY "dbbak" ON mydb --刪除保留資料為dbbak資料
DROP SERIES from pay where tag_key='' --刪除key中的tag
函數使用
- mean: 平均值
- sum: 總和
- min: 最小值
- max: 最大值
- count: 總個數
- difference: 差異數 (前後筆資料的比較)
- non_negative_difference: 正差異數 (前後筆資料的比較)
select * from pay order by time desc limit 2
select mean(allTime) from pay where time >= today() group by time(10m) time_zone(+8)
select * from pay time_zone(+8) limit 2
SELECT sum(allTime) FROM "pay" WHERE time > now() - 10s
select count(allTime) from pay where time > now() - 10m group by time(1s)
select difference("commit_sql") from "snapdb2" where time > now() - 1h limit 10
select non_negative_difference(*) from "snapdb2" where time > now() - 1h limit 10
select non_negative_difference(/commit_sql|rollback_sql/) from "snapdb2" where time > now() - 1h limit 10
User and Privileges
> CREATE USER "todd" WITH PASSWORD '123456'
> SHOW GRANTS for "todd"
> SET PASSWORD FOR "todd" = 'newpassword'
> GRANT READ ON "NOAA_water_database" TO "todd"
> GRANT ALL ON "NOAA_water_database" TO "todd"
> REVOKE ALL PRIVILEGES FROM "todd"
> REVOKE ALL ON "NOAA_water_database" FROM "todd"
> REVOKE WRITE ON "NOAA_water_database" FROM "todd"
> DROP USER "todd"
Covert timestamp to normal datetime
influx -precision rfc3339
# Or, in CLI
> precision rfc3339
Convert InfluxData to csv format
# With CLI
# There is also useful -precision option to set format of timestamp.
influx -database 'database_name' -execute "SELECT * FROM table_name" -format csv > test.csv
# With HTTP-API
# Samples:
# "q=SELECT * FROM \"mymeasurement\" where time > now() - 130d"
# "q=SELECT * FROM \"mymeasurement\" where (time < now() - 130d) and (time > now() - 260d)"
curl -G 'http://localhost:8086/query' --data-urlencode "db=mydb" --data-urlencode "q=SELECT * FROM \"mymeasurement\" " -H "Accept: application/csv" > mytargetcsv.csv
Services
systemctl start influxdb
systemctl enable influxdb
Learning