溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

InfluxDB的安裝及基本使用方法

發(fā)布時(shí)間:2021-07-05 17:37:02 來(lái)源:億速云 閱讀:308 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“InfluxDB的安裝及基本使用方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“InfluxDB的安裝及基本使用方法”吧!

InfluxDB的簡(jiǎn)介

InfluxDB 是用Go語(yǔ)言編寫(xiě)的一個(gè)開(kāi)源分布式時(shí)序、事件和指標(biāo)數(shù)據(jù)庫(kù),無(wú)需外部依賴(lài)。

功能特點(diǎn)

  • 基于時(shí)間序列,支持與時(shí)間有關(guān)的相關(guān)函數(shù)(如最大,最小,求和等)

  • 可度量性:你可以實(shí)時(shí)對(duì)大量數(shù)據(jù)進(jìn)行計(jì)算

  • 基于事件:它支持任意的事件數(shù)據(jù)基于事件:它支持任意的事件數(shù)據(jù)

注:自帶管理界面在1.2版之后,將不再提供。

oint屬性 傳統(tǒng)數(shù)據(jù)庫(kù)中的概念 time(時(shí)間戳) 每個(gè)數(shù)據(jù)記錄時(shí)間,是數(shù)據(jù)庫(kù)中的主索引(會(huì)自動(dòng)生成) fields(字段、數(shù)據(jù)) 各種記錄值(沒(méi)有索引的屬性)也就是記錄的值:溫度, 濕度 tags(標(biāo)簽) 各種有索引的屬性:地區(qū),海拔 注意

在influxdb中,字段必須存在。因?yàn)樽侄问菦](méi)有索引的。如果使用字段作為查詢(xún)條件,會(huì)掃描符合查詢(xún)條件的所有字段值,性能不及tag。類(lèi)比一下,fields相當(dāng)于SQL的沒(méi)有索引的列。 tags是可選的,但是強(qiáng)烈建議你用上它,因?yàn)閠ag是有索引的,tags相當(dāng)于SQL中的有索引的列。tag value只能是string類(lèi)型。 series 相當(dāng)于是 InfluxDB 中一些數(shù)據(jù)的集合,在同一個(gè) database 中,retention policy、measurement、tag sets 完全相同的數(shù)據(jù)同屬于一個(gè) series,同一個(gè) series 的數(shù)據(jù)在物理上會(huì)按照時(shí)間順序排列存儲(chǔ)在一起。

select * from students name: students time score stuid value


1542848518465067760 89 s123 1542850528630385278 79 s123 1542850533581732431 69 s123 1542850536266169940 39 s123 1542850676477097687 99 s123 1542874869654197110 s124 100 1542874898710687064 s125 60

show series from students key


students,stuid=s123 students,stuid=s124 students,stuid=s125 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 shard shard 和 retention policy 相關(guān)聯(lián)。每一個(gè)存儲(chǔ)策略下會(huì)存在許多 shard,每一個(gè) shard 存儲(chǔ)一個(gè)指定時(shí)間段內(nèi)的數(shù)據(jù),并且不重復(fù); 例如 7點(diǎn)-8點(diǎn) 的數(shù)據(jù)落入 shard0 中,8點(diǎn)-9點(diǎn)的數(shù)據(jù)則落入 shard1 中。 每一個(gè) shard 都對(duì)應(yīng)一個(gè)底層的 tsm 存儲(chǔ)引擎,有獨(dú)立的 cache、wal、tsm file。

組件 TSM 存儲(chǔ)引擎主要由幾個(gè)部分組成: cache、wal、tsm file、compactor。

Cache:cache 相當(dāng)于是 LSM Tree 中的 memtabl。插入數(shù)據(jù)時(shí),實(shí)際上是同時(shí)往 cache 與 wal 中寫(xiě)入數(shù)據(jù),可以認(rèn)為 cache 是 wal 文件中的數(shù)據(jù)在內(nèi)存中的緩存。當(dāng) InfluxDB 啟動(dòng)時(shí),會(huì)遍歷所有的 wal 文件,重新構(gòu)造 cache,這樣即使系統(tǒng)出現(xiàn)故障,也不會(huì)導(dǎo)致數(shù)據(jù)的丟失。 cache 中的數(shù)據(jù)并不是無(wú)限增長(zhǎng)的,有一個(gè) maxSize 參數(shù)用于控制當(dāng) cache 中的數(shù)據(jù)占用多少內(nèi)存后就會(huì)將數(shù)據(jù)寫(xiě)入 tsm 文件。如果不配置的話(huà),默認(rèn)上限為 25MB,每當(dāng) cache 中的數(shù)據(jù)達(dá)到閥值后,會(huì)將當(dāng)前的 cache 進(jìn)行一次快照,之后清空當(dāng)前 cache 中的內(nèi)容,再創(chuàng)建一個(gè)新的 wal 文件用于寫(xiě)入,剩下的 wal 文件最后會(huì)被刪除,快照中的數(shù)據(jù)會(huì)經(jīng)過(guò)排序?qū)懭胍粋€(gè)新的 tsm 文件中。

WAL:wal 文件的內(nèi)容與內(nèi)存中的 cache 相同,其作用就是為了持久化數(shù)據(jù),當(dāng)系統(tǒng)崩潰后可以通過(guò) wal 文件恢復(fù)還沒(méi)有寫(xiě)入到 tsm 文件中的數(shù)據(jù)。 TSM File:?jiǎn)蝹€(gè) tsm file 大小最大為 2GB,用于存放數(shù)據(jù)。 Compactor:compactor 組件在后臺(tái)持續(xù)運(yùn)行,每隔 1 秒會(huì)檢查一次是否有需要壓縮合并的數(shù)據(jù)。 主要進(jìn)行兩種操作 一種是 cache 中的數(shù)據(jù)大小達(dá)到閥值后,進(jìn)行快照,之后轉(zhuǎn)存到一個(gè)新的 tsm 文件中。 另外一種就是合并當(dāng)前的 tsm 文件,將多個(gè)小的 tsm 文件合并成一個(gè),使每一個(gè)文件盡量達(dá)到單個(gè)文件的最大大小,減少文件的數(shù)量,并且一些數(shù)據(jù)的刪除操作也是在這個(gè)時(shí)候完成。

安裝 環(huán)境: CentOS7.0_x64 InfluxDB版本:1.7.0

基礎(chǔ)環(huán)境 yum install go 1 InfluxDB的安裝 wget https://dl.influxdata.com/influxdb/releases/influxdb-1.7.0.x86_64.rpm rpm -ivh influxdb-1.2.0.x86_64.rpm 1 2 安裝后產(chǎn)生的InfluxDB相關(guān)文件講解 /usr/bin下文件 文件名 文件解析 influxd influxdb服務(wù)器 influx influxdb命令行客戶(hù)端 influx_inspect 查看工具 influx_stress 壓力測(cè)試工具 influx_tsm 數(shù)據(jù)庫(kù)轉(zhuǎn)換工具(將數(shù)據(jù)庫(kù)從b1或bz1格式轉(zhuǎn)換為tsm1格式) /var/lib/influxdb下文件夾 文件夾 文件夾解析 data 存放最終存儲(chǔ)的數(shù)據(jù),文件以**.tsm**結(jié)尾 meta 存放數(shù)據(jù)庫(kù)元數(shù)據(jù) wal 存放預(yù)寫(xiě)日志文件 /etc/influxdb下文件 文件 文件解析 influxdb.conf influxdb數(shù)據(jù)庫(kù)配置文件 配置文件參數(shù)詳解:InfluxDB influxdbc.conf配置文件詳解

啟動(dòng)服務(wù) 操作服務(wù)的相關(guān)命令 服務(wù)式啟動(dòng)命令

service influxdb start 1 服務(wù)式的其他命令 停止服務(wù) service influxdb stop 1 重啟服務(wù) service influxdb restart 1 嘗試重啟服務(wù) service influxdb try-restart 1 重新加載服務(wù) service influxdb reload 1 強(qiáng)制重新加載服務(wù) service influxdb force-reload 1 查看服務(wù)狀態(tài) service influxdb status 1 以非服務(wù)方式啟動(dòng)

cd /usr/bin ./influxd 1 2 服務(wù)啟動(dòng)查看是否正常 通過(guò)查看服務(wù)對(duì)應(yīng)進(jìn)程

[root@localhost influxdb]# ps aux | grep influx influxdb 7293 0.3 2.4 635640 46516 ? Ssl 18:43 0:01 /usr/bin/influxd -config /etc/influxdb/influxdb.conf root 7412 0.0 0.0 112656 964 pts/0 S+ 18:49 0:00 grep --color=auto influx 1 2 3 通過(guò)service influxdb status命令,查看服務(wù)狀態(tài)

[root@localhost influxdb]# service influxdb status Redirecting to /bin/systemctl status influxdb.service ● influxdb.service - InfluxDB is an open-source, distributed, time series database Loaded: loaded (/usr/lib/systemd/system/influxdb.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2018-11-21 18:43:06 PST; 1h 1min ago Docs: https://docs.influxdata.com/influxdb/ Main PID: 7293 (influxd) CGroup: /system.slice/influxdb.service └─7293 /usr/bin/influxd -config /etc/influxdb/influxdb.conf

Nov 21 19:32:48 localhost.localdomain influxd[7293]: [httpd] ::1 - - [21/Nov/2018:19:32:48 -0800] "POST /query?chunked=true&db=&epoch=ns&q=usr+testdn HTTP/1.1" 400 148 "-" "InfluxDBS...29d6319a 172 Nov 21 19:32:50 localhost.localdomain influxd[7293]: [httpd] ::1 - - [21/Nov/2018:19:32:50 -0800] "POST /query?chunked=true&db=&epoch=ns&q=usr+testdb HTTP/1.1" 400 148 "-" "InfluxDBS...29d6319a 119 Nov 21 19:33:00 localhost.localdomain influxd[7293]: ts=2018-11-22T03:33:00.628996Z lvl=info msg="Executing query" log_id=0BvKdN2W000 service=query query="SHOW DATABASES" Nov 21 19:33:00 localhost.localdomain influxd[7293]: [httpd] ::1 - - [21/Nov/2018:19:33:00 -0800] "POST /query?db=&epoch=ns&q=SHOW+DATABASES HTTP/1.1" 200 116 "-" "InfluxDBShell/1.7....29d6319a 608 Nov 21 19:33:10 localhost.localdomain influxd[7293]: ts=2018-11-22T03:33:10.917329Z lvl=info msg="Executing query" log_id=0BvKdN2W000 service=query query="SHOW MEASUREMENTS ON testdb" Nov 21 19:33:10 localhost.localdomain influxd[7293]: [httpd] ::1 - - [21/Nov/2018:19:33:10 -0800] "POST /query?chunked=true&db=testdb&epoch=ns&q=show+measurements HTTP/1.1" 200 108 "...29d6319a 939 Nov 21 19:43:07 localhost.localdomain influxd[7293]: ts=2018-11-22T03:43:07.447858Z lvl=info msg="Retention policy deletion check (start)" log_id=0BvKdN2W000 service=retention trace_..._event=start Nov 21 19:43:07 localhost.localdomain influxd[7293]: ts=2018-11-22T03:43:07.448965Z lvl=info msg="Retention policy deletion check (end)" log_id=0BvKdN2W000 service=retention trace_id...psed=1.161ms Nov 21 19:43:49 localhost.localdomain influxd[7293]: [httpd] ::1 - - [21/Nov/2018:19:43:49 -0800] "POST /query?chunked=true&db=testdb&epoch=ns&q=qiut HTTP/1.1" 400 149 "-" "InfluxDBS...29d6319a 189 Nov 21 19:43:58 localhost.localdomain influxd[7293]: [httpd] ::1 - - [21/Nov/2018:19:43:58 -0800] "POST /query?chunked=true&db=testdb&epoch=ns&q=quir HTTP/1.1" 400 149 "-" "InfluxDBS...c29d6319a 95 Hint: Some lines were ellipsized, use -l to show in full. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 服務(wù)啟動(dòng)后相關(guān)端口 8086:HTTP API的端口 8088:備份恢復(fù)時(shí)使用,默認(rèn)值為8088 InfluxDB 客戶(hù)端命令行方式操作 InfluxDB數(shù)據(jù)庫(kù)操作

客戶(hù)端命令行方式操作

[root@localhost influxdb]# influx Connected to http://localhost:8086 version 1.7.0 InfluxDB shell version: 1.7.0 Enter an InfluxQL query

1 2 3 4 5 顯示數(shù)據(jù)庫(kù)

show databases name: databases name


_internal 1 2 3 4 5 新建數(shù)據(jù)庫(kù)

create database testdb show databases name: databases name


_internal testdb 1 2 3 4 5 6 7 刪除數(shù)據(jù)庫(kù)

drop database testdb show databases name: databases name


_internal 1 2 3 4 5 6 使用數(shù)據(jù)庫(kù)

create database testdb use testdb Using database testdb 1 2 3 InfluxDB 數(shù)據(jù)表操作 在InfluxDB當(dāng)中,并沒(méi)有表(table)這個(gè)概念,取而代之的是measurement,measurement的功能與傳統(tǒng)數(shù)據(jù)庫(kù)中的表一致,因此我們也可以將measurement稱(chēng)為InfluxDB中的表。

顯示所有表

show measurement 1 新建表 InfluxDB中沒(méi)有顯示的創(chuàng)建表的語(yǔ)句,只能通過(guò)insert數(shù)據(jù)的房還是來(lái)建立新表。 其中 disk_free 就是表名,hostname 是索引(tag),value=xx 是記錄值(field),記錄值可以有多個(gè),系統(tǒng)自帶追加時(shí)間戳。

insert disk_free,hostname=server01 value=442221834240i select * from disk_free name: disk_free time hostname value


1435362189575692180 server01 442221834240 1 2 3 4 5 6 或者添加數(shù)據(jù)時(shí),自己寫(xiě)入時(shí)間戳(寫(xiě)入相同時(shí)間戳、相同tags,對(duì)原有數(shù)據(jù)進(jìn)行update操作)

insert disk_free,hostname=server01 value=442221834240i 1435362189575692182 select * from disk_free name: disk_free time hostname value


1435362189575692180 server01 442221834240 1435362189575692182 server01 442221834240 1 2 3 4 5 6 7 刪除表

drop measurement disk_free 1 數(shù)據(jù)保存策略(Retention Policies) InfluxDB 是沒(méi)有提供直接刪除數(shù)據(jù)記錄的方法,但是提供數(shù)據(jù)保存策略,主要用于指定數(shù)據(jù)保留時(shí)間,超過(guò)指定時(shí)間,就刪除這部分?jǐn)?shù)據(jù)。

查看當(dāng)前數(shù)據(jù)庫(kù)Retention Policies

name:名稱(chēng),此示例名稱(chēng)為 default。 duration:持續(xù)時(shí)間,0代表無(wú)限制。 shardGroupDuration:shardGroup的存儲(chǔ)時(shí)間,shardGroup是InfluxDB的一個(gè)基本儲(chǔ)存結(jié)構(gòu),應(yīng)該大于這個(gè)時(shí)間的數(shù)據(jù)在查詢(xún)效率上應(yīng)該有所降低。 replicaN:全稱(chēng)是replication,副本個(gè)數(shù)。 default:是否是默認(rèn)策略。

show retention policies on testdb name duration shardGroupDuration replicaN default


autogen 0s 168h0m0s 1 true 1 2 3 4 創(chuàng)建新的Retention Policies

rp_name:策略名。 db_name:具體的數(shù)據(jù)庫(kù)名。 3w:保存3周,3周之前的數(shù)據(jù)將被刪除,influxdb 具備各種事件參數(shù),持續(xù)時(shí)間必須至少為1小時(shí);比如:h(小時(shí))、d(天)、w(星期)。 replication 1:副本個(gè)數(shù),一般為1即可。 default:設(shè)置為默認(rèn)策略。 create retention policy "rp_name" on "db_name" duration 3w replication 1 default 1 修改Retention Policies

alter retention policy "rp_name" on "db_name" duration 30d default 1 刪除Retention Policies

drop retention policy "rp_name" on "db_name" 1 連續(xù)查詢(xún)(Continuous Queries) InfluxDB 的連續(xù)查詢(xún)是在數(shù)據(jù)庫(kù)中自動(dòng)定時(shí)啟動(dòng)的一組語(yǔ)句,語(yǔ)句中必須包含 select 關(guān)鍵字 和 group by time() 關(guān)鍵字。 InfluxDB 會(huì)將查詢(xún)結(jié)果放在指定的數(shù)據(jù)表中。 目的:使用連續(xù)查詢(xún)是最優(yōu)的降低采樣率的方式,連續(xù)查詢(xún)和存儲(chǔ)策略搭配使用將會(huì)大大降低 InfluxDB 的系統(tǒng)占用量。而且使用連續(xù)查詢(xún)后,數(shù)據(jù)會(huì)存放到指定的數(shù)據(jù)表中,這樣就為以后統(tǒng)計(jì)不同精度的數(shù)據(jù)提供了方便。

新建連續(xù)查詢(xún)

CREATE CONTINUOUS QUERY <cq_name> ON <database_name> [RESAMPLE [EVERY <interval>] [FOR <interval>]] BEGIN SELECT <function>(<stuff>)[,<function>(<stuff>)] INTO <different_measurement> FROM <current_measurement> [WHERE <stuff>] GROUP BY time(<interval>)[,<stuff>] END 1 2 3 4 5 樣例 CREATE CONTINUOUS QUERY wj_30m ON testdb BEGIN SELECT mean(connected_clients), MEDIAN(connected_clients), MAX(connected_clients), MIN(connected_clients) INTO redis_clients_30m FROM redis_clients GROUP BY ip,port,time(30m) END 1 在 testdb 庫(kù)中新建了一個(gè)名為 wj_30m 的連續(xù)查詢(xún),每三十分鐘取一個(gè) connected_clients 字段的平均值、中位值、最大值、最小值 redis_clients_30m 表中。使用的數(shù)據(jù)保留策略都是default。 不同 database 樣例: CREATE CONTINUOUS QUERY wj_30m ON testdb_30 BEGIN SELECT mean(connected_clients), MEDIAN(connected_clients), MAX(connected_clients), MIN(connected_clients) INTO testdb_30.autogen.redis_clients_30m FROM testdb.autogen.redis_clients GROUP BY ip,port,time(30m) END 1 顯示所有已存在的連續(xù)查詢(xún)

show continuous queries 1 刪除Continuous Queries

到此,相信大家對(duì)“InfluxDB的安裝及基本使用方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI