InfluxDB : データベース 基本操作2026/02/20 |
|
InfluxDB のデータベースの基本操作です。 |
|
| [1] | データベースを作成する。 |
|
root@dlp:~ # influx -username admin -password adminpassword Connected to http://localhost:8086 version 1.8.10 InfluxDB shell version: 1.8.10 # [test_database] 作成 > create database test_database > show databases name: databases name ---- _internal test_database > show users user admin ---- ----- admin true serverworld true freebsd false # [serverworld] ユーザーに [test_database] の全権限を付与 > grant all on "test_database" to "serverworld" # [freebsd] ユーザーに [test_database] の [read] 権限を付与 > grant read on "test_database" to "freebsd" > show grants for "serverworld" database privilege -------- --------- test_database ALL PRIVILEGES > exit |
| [2] | データベースにデータを投入する。 |
# InfluxDB のデータ形式
# ⇒ 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] はオプショナル
# [timestamp] は省略可 ⇒ 省略した場合は現在の UNIX Time (ナノ秒単位) が使用される
# UNIX Time (ナノ秒) は [date] コマンドで確認可 ⇒ $ date +%s%N
# [test_database] に接続 root@dlp:~ # influx -username serverworld -password userpassword -database test_database Connected to http://localhost:8086 version 1.8.10 InfluxDB shell version: 1.8.10 # [cpu] メジャメントに [idle] 値をセットしてデータ投入 > insert cpu idle=99.50 > insert cpu idle=99.00 > insert cpu idle=99.30 > select * from cpu name: cpu time idle ---- ---- 1771544675151406748 99.5 1771544678502566718 99 1771544681999516425 99.3 # [weather] メジャメントに [location] タグと [temperature] 値をセットしてデータ投入 > 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 ---- -------- ----------- 1771544690272734127 hiroshima 20 1771544693656446188 hiroshima 22 1771544696953417982 osaka 18 1771544700305665815 osaka 19 # [WHERE] で条件を指定して表示 > select * from weather where "location" = 'hiroshima' and temperature <= 20 name: weather time location temperature ---- -------- ----------- 1771544690272734127 hiroshima 20 # [timestamp] を RFC3339 で表示 > precision rfc3339 > select * from weather name: weather time location temperature ---- -------- ----------- 2026-02-19T23:44:50.272734127Z hiroshima 20 2026-02-19T23:44:53.656446188Z hiroshima 22 2026-02-19T23:44:56.953417982Z osaka 18 2026-02-19T23:45:00.305665815Z osaka 19 > exit |
| [3] | データを削除する。 |
|
# RFC3339 を指定して接続 root@dlp:~ # influx -username serverworld -password userpassword -database test_database -precision rfc3339 Connected to http://localhost:8086 version 1.8.10 InfluxDB shell version: 1.8.10 > select * from weather name: weather time location temperature ---- -------- ----------- 2026-02-19T23:44:50.272734127Z hiroshima 20 2026-02-19T23:44:53.656446188Z hiroshima 22 2026-02-19T23:44:56.953417982Z osaka 18 2026-02-19T23:45:00.305665815Z osaka 19 # [timestamp] が [2026-02-19 23:44:51] より以前のデータを削除 > delete from weather where time <= '2026-02-19 23:44:51' > select * from weather name: weather time location temperature ---- -------- ----------- 2026-02-19T23:44:53.656446188Z hiroshima 22 2026-02-19T23:44:56.953417982Z osaka 18 2026-02-19T23:45:00.305665815Z osaka 19 # [location = hiroshima] のデータを削除 > drop series from "weather" where "location" = 'hiroshima' # * delete *** ⇒ Index から系列を削除しない, Time Interval による条件指定可 # * drop *** ⇒ Index から系列を削除, Time Interval による条件指定不可 # [measurement] を削除する場合は以下 > drop measurement "cpu" > show measurements name: measurements name ---- weather # データベースを削除する場合は以下 > drop database "test_database" > show databases name: databases name ---- _internal > exit |
| Sponsored Link |
|
|