InfluxDB安装及使用

    技术2026-08-12  2

    1 安装

    1.1 Tar包安装

      (1)获取tar包

    wget https://dl.influxdata.com/influxdb/releases/influxdb-1.8.0_linux_amd64.tar.gz

      (2)解压tar包   tar xvfz influxdb-1.8.0_linux_amd64.tar.gz

    $ su - tigk $ tar xvfz /opt/package/influxdb-1.8.0_linux_amd64.tar.gz -C /home/tigk/.local/ $ cd /home/tigk/.local $ mv influxdb-1.8.0-1/ influxdb

      (3)修改配置文件

      可执行文件路径为:{influxdb目录}/usr/bin   安装InfluxDB之后,在/usr/bin下会有如下几个文件:

    influxd influxdb服务器 influx influxdb命令行客户端 influx_inspect 查看工具 influx_stress 压力测试工具 influx_tsm 数据库转换工具(将数据库从b1或bz1格式转换为tsm1格式)

      默认配置文件所在路径:{influxdb目录}/etc/influxdb/influxdb.conf

    [tigk@fbi-local-02 .local]$ mkdir /data/tigk/influxdb/conf [tigk@fbi-local-02 .local]$ cp /home/tigk/.local/influxdb/etc/influxdb/influxdb.conf /data/tigk/influxdb/conf/ [tigk@fbi-local-02 .local]$ cd /data/tigk/influxdb/conf/influxdb.conf [tigk@fbi-local-02 influxdb]$ mkdir meta [tigk@fbi-local-02 influxdb]$ mkdir data [tigk@fbi-local-02 influxdb]$ mkdir wal

      然后进行修改配置文件

       (4)配置文件说明

    # reporting-disabled = false # 该选项用于上报influxdb的使用信息给InfluxData公司,默认值为false # bind-address = "127.0.0.1:8088" # 备份恢复时使用,默认值为8088 ### [meta] [meta] dir = "/data/tigk/influxdb/meta" # meta数据存放目录 # retention-autocreate = true # 用于控制默认存储策略,数据库创建时,会自动生成autogen的存储策略,默认值:true # logging-enabled = true # 是否开启meta日志,默认值:true ### [data] [data] dir = "/data/tigk/influxdb/data" # 最终数据(TSM文件)存储目录 wal-dir = "/data/tigk/influxdb/wal" # 预写日志存储目录 # wal-fsync-delay = "0s" #在同步写入之前等待的总时间,默认0s # index-version = "inmem" #用于新碎片的切分索引的类型。 # trace-logging-enabled = false #跟踪日志记录在tsm引擎周围提供了更详细的输出 # query-log-enabled = true # 是否开启tsm引擎查询日志,默认值: true # Valid size suffixes are k, m, or g (case insensitive, 1024 = 1k). # cache-max-memory-size = "1g" # 用于限定shard最大值,大于该值时会拒绝写入,默认值:1000MB,单位:byte # Valid size suffixes are k, m, or g (case insensitive, 1024 = 1k). # cache-snapshot-memory-size = "25m" # 用于设置快照大小,大于该值时数据会刷新到tsm文件,默认值:25MB,单位:byte # cache-snapshot-write-cold-duration = "10m" # tsm引擎 snapshot写盘延迟,默认值:10Minute # compact-full-write-cold-duration = "4h" # tsm文件在压缩前可以存储的最大时间,默认值:4Hour # max-concurrent-compactions = 0 #压缩并发的最大数量,默认设置为0表示runtime.GOMAXPROCS(0)*50% ,否则以设置的非零值为准 # Valid size suffixes are k, m, or g (case insensitive, 1024 = 1k). # max-index-log-file-size = "1m" #限制索引日志文件大小 # max-series-per-database = 1000000 # 限制数据库的级数,该值为0时取消限制,默认值:1000000 # max-values-per-tag = 100000 # 一个tag最大的value数,0取消限制,默认值:100000 # tsm-use-madv-willneed = false #如果为true,mmap的建议值MADV_WILLNEED会被提供给内核 ### [coordinator] [coordinator] # write-timeout = "10s" # 写操作超时时间,默认值: 10s # max-concurrent-queries = 0 # 最大并发查询数,0无限制,默认值: 0 # query-timeout = "0s" # 查询操作超时时间,0无限制,默认值:0s # log-queries-after = "0s" # 慢查询超时时间,0无限制,默认值:0s # max-select-point = 0 # SELECT语句可以处理的最大点数(points),0无限制,默认值:0 # max-select-series = 0 # SELECT语句可以处理的最大级数(series),0无限制,默认值:0 # max-select-buckets = 0 # SELECT语句可以处理的最大"GROUP BY time()"的时间周期,0无限制,默认值:0 ### [retention] [retention] # enabled = true # 是否启用该模块,默认值 : true # check-interval = "30m" # 检查时间间隔,默认值 :"30m" ### [shard-precreation] [shard-precreation] # enabled = true # 是否启用该模块,默认值 : true # check-interval = "10m" # 检查时间间隔,默认值 :"10m" # advance-period = "30m" # 预创建分区的最大提前时间,默认值 :"30m" [monitor] # store-enabled = true # 是否启用该模块,默认值 :true # store-database = "_internal" # 默认数据库:"_internal" # store-interval = "10s" # 统计间隔,默认值:"10s" ### [http] [http] # enabled = true # 是否启用该模块,默认值 :true # bind-address = ":8086" # 绑定地址,默认值 :":8086" # auth-enabled = false # 是否开启认证,默认值:false # realm = "InfluxDB" # 配置JWT realm,默认值: "InfluxDB" # log-enabled = true # 是否开启日志,默认值:true # suppress-write-log = false #在启用日志时是否抑制HTTP写请求日志 # access-log-path = "" #当启用HTTP请求日志时,该选项指定了路径。如influxd不能访问指定的路径,它将记录一个错误并将请求日志写入stderr # write-tracing = false # 是否开启写操作日志,如果置成true,每一次写操作都会打日志,默认值:false # pprof-enabled = true # 是否开启pprof,默认值:true # debug-pprof-enabled = false # 是否开启pprof,默认值:true # https-enabled = false # 是否开启https ,默认值 :false # https-certificate = "/etc/ssl/influxdb.pem" # 设置https证书路径,默认值:"/etc/ssl/influxdb.pem" # https-private-key = ""# 设置https私钥,无默认值 # shared-secret = "" # 用于JWT签名的共享密钥,无默认值 # max-row-limit = 0 # 配置查询返回最大行数,0无限制,默认值:0 # max-connection-limit = 0 # 配置最大连接数,0无限制,默认值:0 # unix-socket-enabled = false # 是否使用unix-socket,默认值:false # bind-socket = "/var/run/influxdb.sock" # unix-socket路径,默认值:"/var/run/influxdb.sock" # max-body-size = 25000000 #客户端请求主体的最大值,以字节为单位。0无限制,默认值0 # max-concurrent-write-limit = 0 #并发处理的最大写入次数,0无限制,默认值0 # max-enqueued-write-limit = 0 #排队等待处理的最大数量,0无限制,默认值0 # enqueued-write-timeout = 0 #在队列中等待处理的最长时间,0或者setting max-concurrent-write-limit=0无限制,默认值0 ### [ifql] [ifql] # enabled = true# 是否启用该模块,默认值 :true # log-enabled = true# 是否开启日志,默认值:true # bind-address = ":8082" #ifql RPC服务使用的绑定地址默认是8082 ### [logging] [logging] # format = "auto" #日志格式,默认是自动 # level = "info" #日志级别默认info # suppress-logo = false #当程序启动时,会抑制打印出来的logo输出 ### [subscriber] [subscriber] # enabled = true # 是否启用该模块,默认值 :true # http-timeout = "30s" # http超时时间,默认值:"30s" # insecure-skip-verify = false # 是否允许不安全的证书 # ca-certs = "" # 设置CA证书 # write-concurrency = 40 #设置并发数目,默认值:40 # write-buffer-size = 1000 # 设置buffer大小,默认值:1000 ### [[graphite]] [[graphite]] # enabled = false# 是否启用该模块,默认值 :false # database = "graphite"# 数据库名称,默认值:"graphite" # retention-policy = "" # 存储策略,无默认值 # bind-address = ":2003"# 绑定地址,默认值:":2003" # protocol = "tcp"# 协议,默认值:"tcp" # consistency-level = "one" # 一致性级别,默认值:"one # batch-size = 5000 # 批量size,默认值:5000 # batch-pending = 10# 配置在内存中等待的batch数,默认值:10 # batch-timeout = "1s" # 超时时间,默认值:"1s" # udp-read-buffer = 0# udp读取buffer的大小,0表示使用操作系统提供的值,如果超过操作系统的默认配置则会出错。 该配置的默认值:0 # separator = "." # 多个measurement间的连接符,默认值: "." # tags = ["region=us-east", "zone=1c"] #将被添加到所有指标的默认标签。这些可以在模板级别上覆盖或者从指标中提取的标签 # templates = [ # "*.app env.service.resource.measurement", # # Default template # "server.*", # ] ### [collectd] [[collectd]] # enabled = false# 是否启用该模块,默认值 :false # bind-address = ":25826" # 绑定地址,默认值: ":25826" # database = "collectd"# 数据库名称,默认值:"collectd" # retention-policy = "" # 存储策略,无默认值 # typesdb = "/usr/local/share/collectd" # 路径,默认值:"/usr/share/collectd/types.db" # security-level = "none" #安全级别 # auth-file = "/etc/collectd/auth_file" # batch-size = 5000 #从缓存中批量获取数据的量,默认值:5000 # batch-pending = 10 #可能在内存中等待的批次的数量,默认值:10 # batch-timeout = "10s" #即使没有达到缓冲区的限制,至少要刷新一下,默认值:"10s" # read-buffer = 0 #udp读取buffer的大小,0表示使用操作系统提供的值,如果超过操作系统的默认配置则会出错。默认值:0 # parse-multivalue-plugin = "split" #两种处理方式split和join,split会分到不同的表中,join会将记录作为一个单独的记录处理。默认是split ### [opentsdb] [[opentsdb]] # enabled = false# 是否启用该模块,默认值 :false # bind-address = ":4242"# 绑定地址,默认值:":4242" # database = "opentsdb" # 默认数据库:"opentsdb" # retention-policy = ""# 存储策略,无默认值 # consistency-level = "one" # 一致性级别,默认值:"one" # tls-enabled = false # 是否开启tls,默认值:false # certificate= "/etc/ssl/influxdb.pem" # 证书路径,默认值:"/etc/ssl/influxdb.pem" # log-point-errors = true# 出错时是否记录日志,默认值:true # batch-size = 1000 #从缓存中批量获取数据的量,默认值:1000 # batch-pending = 5 #可能在内存中等待的批次的数量,默认值:5 # batch-timeout = "1s" #即使没有达到缓冲区的限制,至少要刷新一下,默认值:"1s" ### [[udp]] [[udp]] # enabled = false# 是否启用该模块,默认值 :false # bind-address = ":8089" # 绑定地址,默认值:":8089" # database = "udp"# 数据库名称,默认值:"udp" # retention-policy = "" # 存储策略,无默认值 # precision = "" #接收点的时间点的精度("" or "n", "u", "ms", "s", "m", "h") # batch-size = 5000#从缓存中批量获取数据的量,默认值:5000 # batch-pending = 10 #可能在内存中等待的批次的数量,默认值:10 # batch-timeout = "1s" #即使没有达到缓冲区的限制,至少要刷新一下,默认值:"1s" # read-buffer = 0# udp读取buffer的大小,0表示使用操作系统提供的值,如果超过操作系统的默认配置则会出错。 该配置的默认值:0  ### [continuous_queries] [continuous_queries] # enabled = true# 是否启用该模块,默认值 :true # log-enabled = true# 是否开启日志,默认值:true # query-stats-enabled = false #控制查询是否被记录到自我监控数据存储中 # run-interval = "1s" # 时间间隔,默认值:"1s" ### [tls] [tls] # ciphers = [ # "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305", # "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", # ] # min-version = "tls1.2" # max-version = "tls1.2"

      (5)启动

       cd {influxdb目录}

    $ cd /home/tigk/.local/influxdb/usr/bin/ $ ./influxd -config /data/tigk/influxdb/conf/influxdb.conf

      (6)启动失败

    $ netstat -tunlp|grep 8086 (No info could be read for "-p": geteuid()=1005 but you should be root.) tcp 0 0 10.0.165.2:8086 0.0.0.0:* LISTEN -

      修改端口 bind-address = “:8085”

      (7)后台启动服务

    $ nohup ./influxd -config /data/tigk/influxdb/conf/influxdb.conf &

      (8)启动influxdb控制台,第一次启动会较慢,耐心等待

    $ ./influx -port 8085 Connected to http://localhost:8085 version 1.8.0 InfluxDB shell version: 1.8.0 >

      (9)创建一个tigk用户,作为管理用户

    > create user "tigk" with password 'tigk' with all privileges > show users user admin ---- ----- tigk true

    2 使用

    2.1 常用命令

      (1)命令展示influx –help

    $ ./influx –help unknown arguments: –help Usage of influx: -version Display the version and exit. -path-prefix 'url path' Path that follows the host and port -host 'host name' Host to connect to. -port 'port #' Port to connect to. -socket 'unix domain socket' Unix socket to connect to. -database 'database name' Database to connect to the server. -password 'password' Password to connect to the server. Leaving blank will prompt for password (--password ''). -username 'username' Username to connect to the server. -ssl Use https for requests. -unsafeSsl Set this when connecting to the cluster using https and not use SSL verification. -execute 'command' Execute command and quit. -type 'influxql|flux' Type specifies the query language for executing commands or when invoking the REPL. -format 'json|csv|column' Format specifies the format of the server responses: json, csv, or column. -precision 'rfc3339|h|m|s|ms|u|ns' Precision specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns. -consistency 'any|one|quorum|all' Set write consistency level: any, one, quorum, or all -pretty Turns on pretty print for the json format. -import Import a previous database export from file -pps How many points per second the import will allow. By default it is zero and will not throttle importing. -path Path to file to import -compressed Set to true if the import file is compressed Examples: # Use influx in a non-interactive mode to query the database "metrics" and pretty print json: $ influx -database 'metrics' -execute 'select * from cpu' -format 'json' -pretty # Connect to a specific database on startup and set database context: $ influx -database 'metrics' -host 'localhost' -port '8086'

      (2)Influx命令使用   Influx是连接influxdb的一个CLI程序,在本地成功安装influxdb后,influxdb启动,influx命令就可以直接使用。Influxdb默认在8086端口运行。Influx cli默认使用localhost:8086连接。可以使用-host和-port选项,连接到其他机器或者端口。

    $ ./influx -port 8085 Connected to http://localhost:8085 version 1.8.0 InfluxDB shell version: 1.8.0 >

      (3)Influx-sql使用举例   Influx命令连接数据库后,可以使用类sql语句查询数据

    命令解释CREATE DATABASE database_name建立数据库SHOW DATABASES查询所有数据库USE database_name使用database_name 库INSERT cpu,host=host_name,region=cn value=66插数据入cpu measurement,标签host=host_name,region=cn, field是value=66SELECT “host”, “region”, “value” FROM “cpu”从cpu measurement查询数据SELECT * FROM “cpu”从cpu measurement查询所有数据

      (4)Influxdb的数据格式   Influxdb是一个时序数据库,其保存metric的在某一系列时间点的状态。这些点状态包含时间戳,一个所谓的“measurement”名称,至少一个称为filed的键值对,以及0到多个标签(如 host=bigdata01)。   在influxdb中可以把measurement看为关系数据库中的table,tag和field是其中的列。其中Tag都加了索引、field不加。和关系数据库不同的是,不用预先定义schema,没有value的点不会被保存。其格式如下:

    <measurement>[,<tag-key>=<tag-value>...] <field-key>=<field-value>[,<field2-key>=<field2-value>...] [unix-nano-timestamp]

      举例:

    cpu,host=bigdata01,region=cn value=66 payment,device=mobile,product=Notepad,method=credit billed=88,licenses=2i 1434067469156329323
    Processed: 0.011, SQL: 10