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