InfluxDB安装及入门

发布时间 2023-08-31 17:55:14作者: 云计算工程师

1.influxdb 简介

InfluxDB是一个用于存储和分析时间序列数据的开源数据库
主要特性有:
    内置HTTP接口,使用方便
    数据可以打标记,这样查询可以很灵活
    类SQL的查询语句
    安装管理很简单,并且读写数据很高效
    能够实时查询,数据在写入时被索引后就能够被立即查出
注意: InfluxDB使用服务器本地时间给数据加时间戳,而且是UTC时区的。并使用NTP来同步服务器之间的时间,如果服务器的时钟没有通过NTP同步,那么写入InfluxDB的数据的时间戳就可能不准确。
InfluxDB网络端口说明:
    TCP端口8086用作InfluxDB的客户端和服务端的http api通信
    TCP端口8088给备份和恢复数据的RPC服务使用

2.influxdb 安装.sh

下载地址: https://portal.influxdata.com/downloads/
使用包管理器来安装
Ubuntu添加InfluxDB的仓库
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/lsb-release
echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
Debian添加Influxdb仓库
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/os-release
test $VERSION_ID = "7" && echo "deb https://repos.influxdata.com/debian wheezy stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
test $VERSION_ID = "8" && echo "deb https://repos.influxdata.com/debian jessie stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
安装并启动Influxdb
sudo apt-get update && sudo apt-get install influxdb
sudo service influxdb start
或者 sudo systemctl start influxdb
RedHat & CentOS配置InfluxDB仓库
cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo
[influxdb]
name = InfluxDB Repository - RHEL \$releasever
baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdb.key
EOF
安装并启动InfluxDB
sudo yum install influxdb
sudo service influxdb start
或者 sudo systemctl start influxdb
# 查看influxdb运行时配置
influxd config

3.InfluxDB 命令参数说明

influx
    -host 连接到远程主机
    -port 远程主机端口
    -socket 'Unix domain socket' 连接unix套接字
    -database 连接到指定数据库
    -password 密码
    -username 用户名
    -ssl 使用https请求
    -execute 'command' 执行命令并退出
    -format 'json|csv|column' 格式制定了服务器响应的格式
    -precision 指定时间戳的格式精度
        rfs3339(YYYY-MM-DDTHH:MM:SS.nnnnnnnZ),h(hours),m(minutes),s,ms,u,ns
    -consistency 'any|one|quorum|all' 设置写一致性级别
    -pretty 打开美化json打印
    -import 导入备份的数据库文件
    -pps 导入允许每秒多少个点,默认情况下,它是零,不会限制进口
    -path import的文件路径
    -compressed 如果导入文件被压缩,则设置为true

  

4.InfluxDB使用

# 连接本地InfluxDB实例
[root@centos8 ~]# influx --precision rfc3339
Connected to http://localhost:8086 version 1.8.10
InfluxDB shell version: 1.8.10
>
创建数据库并插入数据
> create database test_db # 创建数据库
> show databases;
name: databases
name
----
_internal
test_db
> use test_db # 选择数据库,以下操作均在test_db上
> INSERT test_measurement,host=serverA,region=us_west value=0.64 # 插入单条数据
> SELECT "host", "region", "value" FROM "test_measurement" # 查询数据
name: test_measurement
time                           host    region  value
----                           ----    ------  -----
2023-08-31T07:51:28.269471825Z serverA us_west 0.64