Ubuntu 24.04
Sponsored Link

InfluxDB : Basic Database Management2024/06/10

 
This is InfluxDB Basic Database Management example.
[1] Create Database.
root@dlp:~#
influx -username admin -password adminpassword

Connected to http://localhost:8086 version 1.6.7~rc0
InfluxDB shell version: 1.6.7~rc0

# create [test_database] database
> create database test_database 

> show databases 
name: databases
name
----
_internal
test_database

> show users 
user        admin
----        -----
admin       true
ubuntu      false
serverworld true

# add all privileges to [serverworld] user on [test_database] database
> grant all on "test_database" to "serverworld" 

# add [read] privilege to [ubuntu] user on [test_database] database
> grant read on "test_database" to "ubuntu" 

> show grants for "serverworld" 
database      privilege
--------      ---------
test_database ALL PRIVILEGES

> exit
[2] Insert data to database.
# InfluxDB data series
# ⇒ measurement,tag_set field_set timestamp
# insert <measurement>[,<tag_key>=<tag_value>[,<tag_key>=<tag_value>]] <field_key>=<field_value>[,<field_key>=<field_value>] [<timestamp>]
#
# [tag_set] is optional
# [timestamp] is optional ⇒ if [timestamp] is not specified, it is used current UNIX Time (nanosecond)
# possible to see UNIX Time (nanosecond) with [date] command ⇒ $ date +%s%N

# connect to [test_database]

root@dlp:~#
influx -username serverworld -password userpassword -database test_database

Connected to http://localhost:8086 version 1.6.7~rc0
InfluxDB shell version: 1.6.7~rc0

# insert data to [cpu] measurement
> insert cpu idle=99.50
> insert cpu idle=99.00
> insert cpu idle=99.30
> select * from cpu
name: cpu
time                idle
----                ----
1717979435076549773 99.5
1717979438851866665 99
1717979442636415770 99.3

# insert data to [weather] measurement
> insert weather,location=hiroshima temperature=20
> insert weather,location=hiroshima temperature=22
> insert weather,location=osaka temperature=18
> insert weather,location=osaka temperature=19
> select * from weather
name: weather
time                location  temperature
----                --------  -----------
1717979484297990791 hiroshima 20
1717979487530291919 hiroshima 22
1717979491410818595 osaka     18
1717979494771556563 osaka     19

# possible to search data with [WHERE]
> select * from weather where "location" = 'hiroshima' and temperature <= 20
name: weather
time                location  temperature
----                --------  -----------
1717979484297990791 hiroshima 20

# show [timestamp] with RFC3339 style
> precision rfc3339
> select * from weather
name: weather
time                           location  temperature
----                           --------  -----------
2024-06-10T00:31:24.297990791Z hiroshima 20
2024-06-10T00:31:27.530291919Z hiroshima 22
2024-06-10T00:31:31.410818595Z osaka     18
2024-06-10T00:31:34.771556563Z osaka     19

> exit
[3] Delete data.
# connect with RFC3339 style

root@dlp:~#
influx -username serverworld -password userpassword -database test_database -precision rfc3339

Connected to http://localhost:8086 version 1.6.7~rc0
InfluxDB shell version: 1.6.7~rc0

> select * from weather
name: weather
time                           location  temperature
----                           --------  -----------
2024-06-10T00:31:24.297990791Z hiroshima 20
2024-06-10T00:31:27.530291919Z hiroshima 22
2024-06-10T00:31:31.410818595Z osaka     18
2024-06-10T00:31:34.771556563Z osaka     19

# delete data that [timestamp] are older than [2024-06-10 00:31:25]
> delete from weather where time <= '2024-06-10 00:31:25'
> select * from weather
name: weather
time                           location  temperature
----                           --------  -----------
2024-06-10T00:31:27.530291919Z hiroshima 22
2024-06-10T00:31:31.410818595Z osaka     18
2024-06-10T00:31:34.771556563Z osaka     19

# delete data that tags are [location = hiroshima]
> drop series from "weather" where "location" = 'hiroshima'

# * delete *** ⇒ not drop series from Index, possible to search with Time Interval
# * drop *** ⇒ drop series from Index, impossible to search Time Interval

# delete [measurement]
> drop measurement "cpu"
> show measurements
name: measurements
name
----
weather

# delete database
> drop database "test_database"
> show databases
name: databases
name
----
_internal

> exit
Matched Content