influxdb使用說明及安裝部署
influxdb是目前比較流行的時間序列數據庫
什么是時間序列數據庫,最簡單的定義就是數據格式里包含Timestamp字段的數據,比如某一時間環境的溫度,CPU的使用率等。但是,有什么數據不包含Timestamp呢?幾乎所有的數據其實都可以打上一個Timestamp字段。時間序列數據的更重要的一個屬性是如何去查詢它,包括數據的過濾,計算等等。
Influxdb安裝
RedHat & CentOS
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.0.2.x86_64.rpm sudo yum localinstall influxdb-1.0.2.x86_64.rpm
influxdb基本操作
database:數據庫;
measurement:數據庫中的表;
points:表里面的一行數據。
influxDB中獨有的一些概念
Point由時間戳(time)、數據(field)和標簽(tags)組成。
time:每條數據記錄的時間,也是數據庫自動生成的主索引;
fields:各種記錄的值;
tags:各種有索引的屬性。
#創建數據庫 create database "db_name" #顯示所有的數據庫 show databases #刪除數據庫 drop database "db_name" #使用數據庫 use db_name #顯示該數據庫中所有的表 show measurements #創建表,直接在插入數據的時候指定表名 insert test,host=127.0.0.1,monitor_name=test count=1 #test 代表創建表 #刪除表 drop measurement "measurement_name"
增
use testDb insert test,host=127.0.0.1,monitor_name=test count=1 #通過命令行 curl -i -XPOST 'http://127.0.0.1:8086/write?db=testDb' --data-binary 'test,host=127.0.0.1,monitor_name=test count=1' #通過http接口
其中:
-
test:表名;
-
host=127.0.0.1,monitor_name=test:tag;
-
count=1:field
查
show databases #查詢數據庫 show measurements #查詢measurement表 select * from test order by time desc #查詢test表中的數據 curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=metrics" --data-urlencode "q=select * from test order by time desc" #通過http接口查詢test表中的數據
備份
備份數據庫: [root@localhost /]# mkdir influx_backup [root@localhost /]# cd ./influx_backup [root@localhost /]# influxd backup -database xxxdb-retention autogen ./ 可選參數: -retention <retention policy name> -shard <shard ID> -since <date> 遠程備份:(未測試) $ influxd backup -database mydatabase -host 10.0.0.1:8088 /tmp/mysnapshot
恢復
注意:進入backup目錄 語法: influxd restore [ -metadir | -datadir ] <path-to-meta-or-data-directory> <path-to-backup> influxd restore -database xxxxxdb -datadir /ect/var/lib/influxdb/data /home/influxdb_backup_Construction/ 注意:influxdb的data路徑和備份文件的路徑 例如: [root@localhost influxdb_backup_Construction]# //元數據導入 [root@localhost influxdb_backup_Construction]# influxd restore -metadir /var/lib/influxdb/meta ./ Using metastore snapshot: meta.00 //數據庫導入 [root@localhost influxdb_backup_Construction]# influxd restore -database Construction -datadir /var/lib/influxdb/data ./ 導入。。。。 //給與權限 [root@localhost influxdb_backup_Construction]# chown -R influxdb:influxdb /var/lib/influxdb //重啟服務 [root@localhost influxdb_backup_Construction]# service influxdb restart //結束
備份恢復的步驟
backup a.備份元數據 influxd backup /home/environ_backup b.備份數據庫 influxd backup -database environ /home/environ_backup restore a.停止服務 執行:service influxdb stop b.進入元數據和數據存放目錄 cd /home/environ_backup c.恢復元數據 influxd restore -metadir /var/lib/influxdb/meta ./ d.恢復指定數據庫influxd restore -database environ -datadir /var/lib/influxdb/data ./ e.重新授權 chown -R influxdb:influxdb /var/lib/influxdb f.重啟服務 service influxdb restart
關於influxdb數據庫高可用架構描述
架構圖

配置示例
# 配置文件 /opt/influx-proxy/proxy.json { "circles": [ { "name": "circle-1", "backends": [ { "name": "influxdb-1-1", "url": "http://192.168.1.11:8086", "username": "root", "password": "123456", "auth_secure": false }, { "name": "influxdb-1-2", "url": "http://192.168.1.11:8087", "username": "root", "password": "123456", "auth_secure": false } ] }, { "name": "circle-2", "backends": [ { "name": "influxdb-2-1", "url": "http://192.168.1.12:8086", "username": "root", "password": "123456", "auth_secure": false }, { "name": "influxdb-2-2", "url": "http://192.168.1.12:8087", "username": "root", "password": "123456", "auth_secure": false } ] } ], "listen_addr": ":7076", "db_list": [], "data_dir": "/mnt/data/influx-pr/data", "tlog_dir": "/mnt/data/influx-pr/log", "hash_key": "idx", "flush_size": 10000, "flush_time": 1, "check_interval": 1, "rewrite_interval": 10, "conn_pool_size": 20, "write_timeout": 10, "idle_timeout": 10, "username": "root", "password": "123456", "auth_secure": false, "write_tracing": false, "query_tracing": false, "https_enabled": false, "https_cert": "", "https_key": "" }
# 配置文件 /etc/influxdb/influxdb.conf.1 reporting-disabled = true # 禁用報告,默認為 false bind-address = ":8088" [meta] dir = "/mnt/data/influxdb/meta" # 元信息目錄 [data] dir = "/mnt/data/influxdb/data" # 數據目錄 wal-dir = "/mnt/data/influxdb/wal" # 預寫目錄 wal-fsync-delay = "10ms" # SSD 設置為 0s,非 SSD 推薦設置為 0ms-100ms index-version = "tsi1" # tsi1 磁盤索引,inmem 內存索引需要大量內存 query-log-enabled = true # 查詢的日志,默認是 true [coordinator] write-timeout = "20s" # 寫入請求超時時間,默認為 10s [http] enabled = true bind-address = ":8086" auth-enabled = true log-enabled = true # http 請求日志,默認是 true [logging] level = "info" # 日志等級,error、warn、info(默認)、debug # 系統服務文件 /etc/systemd/system/influxd-cluster@.service [Unit] Description=influx-cluster After=network.target [Service] Type=simple ExecStart=/usr/bin/influxd -config /etc/influxdb/influxdb.conf.%i Restart=on-failure [Install] WantedBy=multi-user.target
Influx-proxy 版本安裝
# 下載influx-proxy(服務器1) cd /opt git clone https://github.com/chengshiwen/influx-proxy.git # 安裝go環境(服務器1) tar -zxvf go1.15.linux-amd64.tar.gz -C /usr/local echo "export PATH=\$PATH:/usr/local/go/bin" >> /etc/profile echo "export GOPROXY=https://goproxy.io" >> /etc/profile source /etc/profile # 編譯influx-proxy(服務器1) cd influx-proxy go bulid #編譯生成influx-proxy執行程序 ./influx-proxy -config ./proxy.json #啟動influx-proxy的go程序 # 查看版本 influx-proxy -version # 查看集群狀態 curl http://127.0.0.1:7076/health -u root:123456 拓展: # 設置用戶、密碼(兩台服務器4個實例都需要設置)(服務器1)(服務器2) influx -port 8086 create user 'username' with password 'password' with all privileges exit influx -port 8087 create user 'username' with password 'password' with all privileges exit