一、安装
CentOS用户可以直接用yum包管理来安装最新版本的InfluxDB。
1 2 3 4 5 6 7 8 |
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 |
加如yum源后,运行下面的命令来安装和启动InfluxDB服务:
1 |
sudo yum install influxdb |
大致过程为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
[root@emr-header-1 ~]# 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] 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 [root@emr-header-1 ~]# sudo yum install influxdb Loaded plugins: fastestmirror base | 3.6 kB 00:00:00 epel | 5.3 kB 00:00:00 extras | 2.9 kB 00:00:00 influxdb | 2.5 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/3): epel/x86_64/updateinfo | 1.0 MB 00:00:00 (2/3): epel/x86_64/primary_db | 6.9 MB 00:00:00 (3/3): influxdb/7/x86_64/primary_db | 45 kB 00:00:01 Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package influxdb.x86_64 0:1.7.9-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================================================================================================================================================== Installing: influxdb x86_64 1.7.9-1 influxdb 60 M Transaction Summary ============================================================================================================================================================================================================================================================================== Install 1 Package Total download size: 60 M Installed size: 60 M Is this ok [y/d/N]: y Downloading packages: warning: /var/cache/yum/x86_64/7/influxdb/packages/influxdb-1.7.9.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 2582e0c5: NOKEY=======================================================================================================-] 586 kB/s | 60 MB 00:00:00 ETA Public key for influxdb-1.7.9.x86_64.rpm is not installed influxdb-1.7.9.x86_64.rpm | 60 MB 00:01:24 Retrieving key from https://repos.influxdata.com/influxdb.key Importing GPG key 0x2582E0C5: Userid : "InfluxDB Packaging Service <support@influxdb.com>" Fingerprint: 05ce 1508 5fc0 9d18 e99e fb22 684a 14cf 2582 e0c5 From : https://repos.influxdata.com/influxdb.key Is this ok [y/N]: y Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : influxdb-1.7.9-1.x86_64 1/1 Created symlink from /etc/systemd/system/influxd.service to /usr/lib/systemd/system/influxdb.service. Created symlink from /etc/systemd/system/multi-user.target.wants/influxdb.service to /usr/lib/systemd/system/influxdb.service. Verifying : influxdb-1.7.9-1.x86_64 1/1 Installed: influxdb.x86_64 0:1.7.9-1 Complete! [root@emr-header-1 ~]# |
可以看到我们使用的版本是1.7.9。
InfluxDB的配置文件位于/etc/influxdb/influxdb.conf中,日志位于/var/log/influxdb中,默认使用下面的网络端口(两个端口都会用到):
1、TCP端口8086用作InfluxDB的客户端和服务端的HTTP API通信;
2、TCP端口8088给备份和恢复数据的RPC服务使用;
可以通过influx命令看出:
1 2 3 |
[root@emr-header-1 influxdb]# influx Failed to connect to http://localhost:8086: Get http://localhost:8086/ping: dial tcp 127.0.0.1:8086: connect: connection refused Please check your connection settings and ensure 'influxd' is running. |
在配置文件分为几个节点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[meta] [data] [coordinator] [retention] [shard-precreation] [monitor] [http] [ifql] [logging] [subscriber] [[graphite]] [[collectd]] [[opentsdb]] [[udp]] [continuous_queries] |
其中涉及存储相关的是:
1、meta,存放数据库元数据;
2、data,存放最终存储的数据,文件以.tsm结尾;
3、wal,存放预写日志文件;
InfluxDB不会自动创建目录meta、data、wal中配置的目录(必须是SSD)。此外,由于InfluxDB启动时使用的是用户influxdb,因此需要手工创建对应目录并设置相应的权限:
1 2 |
mkdir -pv /mnt/disk2/influxdb/ chown -R influxdb:influxdb /mnt/disk2/influxdb/ |
使用如下的命令启动服务:
1 |
systemctl restart influxdb |
然后观察进程:
1 2 |
[root@emr-header-1 influxdb]# ps aux|grep influxdb influxdb 29435 81.5 0.1 352688 18284 ? Rsl 19:28 0:01 /usr/bin/influxd -config /etc/influxdb/influxdb.conf |
和监听端口:
1 2 3 4 |
[root@emr-header-1 ~]# netstat -tulnp|grep 18086 tcp 0 0 127.0.0.1:18086 0.0.0.0:* LISTEN 1309/influxd [root@emr-header-1 ~]# netstat -tulnp|grep 18088 tcp 0 0 127.0.0.1:18088 0.0.0.0:* LISTEN 1309/influxd |
启动influxdb后,发现在某些情况下可能出现influxd进程存在但使用systemctl status influxdb查看服务运行状态没有提示错误、/var/log/influxdb中的日志为空、InfluxDB的监听端口没有的情况,这说明服务没有启动成功,此时可以尝试直接用root启动:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[root@emr-header-1 influxdb]# systemctl status influxdb ● 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 Mon 2020-01-13 19:24:24 CST; 2s ago Docs: https://docs.influxdata.com/influxdb/ Main PID: 20773 (influxd) CGroup: /system.slice/influxdb.service └─20773 /usr/bin/influxd -config /etc/influxdb/influxdb.conf Jan 13 19:24:24 emr-header-1.cluster-149038 systemd[1]: Started InfluxDB is an open-source, distributed, time series database. Jan 13 19:24:24 emr-header-1.cluster-149038 systemd[1]: Starting InfluxDB is an open-source, distributed, time series database... [root@emr-header-1 influxdb]# /usr/bin/influxd -config /etc/influxdb/influxdb.conf 8888888 .d888 888 8888888b. 888888b. 888 d88P" 888 888 "Y88b 888 "88b 888 888 888 888 888 888 .88P 888 88888b. 888888 888 888 888 888 888 888 888 8888888K. 888 888 "88b 888 888 888 888 Y8bd8P' 888 888 888 "Y88b 888 888 888 888 888 888 888 X88K 888 888 888 888 888 888 888 888 888 Y88b 888 .d8""8b. 888 .d88P 888 d88P 8888888 888 888 888 888 "Y88888 888 888 8888888P" 8888888P" 2020-01-13T11:25:19.043683Z info InfluxDB starting {"log_id": "0KJfG9Gl000", "version": "1.7.9", "branch": "1.7", "commit": "23bc63d43a8dc05f53afa46e3526ebb5578f3d88"} 2020-01-13T11:25:19.043728Z info Go runtime {"log_id": "0KJfG9Gl000", "version": "go1.12.6", "maxprocs": 4} run: open server: listen: listen tcp 127.0.0.1:8088: bind: address already in use |
可以看到端口被占用的报错。
如果要配置开机自启,可以使用:
1 |
systemctl enable influxdb |
二、开启认证
使用influx命令连接到InfluxDB:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[root@emr-header-1 ~]# influx -host 'localhost' -port '18086' Connected to http://localhost:18086 version 1.7.9 InfluxDB shell version: 1.7.9 > help Usage: connect <host:port> connects to another node specified by host:port auth prompts for username and password pretty toggles pretty print for the json format chunked turns on chunked responses from server chunk size <size> sets the size of the chunked responses. Set to 0 to reset to the default chunked size use <db_name> sets current database format <format> specifies the format of the server responses: json, csv, or column precision <format> specifies the format of the timestamp: rfc3339, h, m, s, ms, u or ns consistency <level> sets write consistency level: any, one, quorum, or all history displays command history settings outputs the current settings for the shell clear clears settings such as database or retention policy. run 'clear' for help exit/quit/ctrl+d quits the influx shell show databases show database names show series show series information show measurements show measurement information show tag keys show tag key information show field keys show field key information A full list of influxql commands can be found at: https://docs.influxdata.com/influxdb/latest/query_language/spec/ > |
接下来,让我们开启登陆认证。首先创建数据库用户:
1 2 3 4 5 |
[root@emr-header-1 ~]# influx -host 'localhost' -port '18086' Connected to http://localhost:18086 version 1.7.9 InfluxDB shell version: 1.7.9 > CREATE USER "test_user" WITH PASSWORD 'test_password' WITH ALL PRIVILEGES > exit |
然后修改配置文件,在[http]节点中启用
1 |
auth-enabled = true |
配置,并使用systemctl restart influxdb重启服务。不带用户名密码进行测试将报错:
1 2 3 4 5 6 7 8 |
[root@emr-header-1 ~]# influx -host 'localhost' -port '18086' Connected to http://localhost:18086 version 1.7.9 InfluxDB shell version: 1.7.9 > show databases ERR: unable to parse authentication credentials Warning: It is possible this error is due to not setting a database. Please set a database with the command "use <database>". > |
然后在命令行中传入用户名及密码:
1 2 3 4 5 6 7 8 |
[root@emr-header-1 ~]# influx -host 'localhost' -port '18086' -username 'test_user' -password 'test_password' Connected to http://localhost:18086 version 1.7.9 InfluxDB shell version: 1.7.9 > show users user admin ---- ----- test_user true > |
三、简单使用
3.1 数据库操作
查看所有的库
1 |
show databases |
创建数据库
1 |
create database test |
删除数据库
1 |
drop database test |
3.2 measurement操作(类似于表)
想对measurement进行操作,必须先进入对应的数据库:
1 2 3 4 5 6 |
[root@emr-header-1 ~]# influx -host 'localhost' -port '18086' -username 'test_user' -password 'test_password' Connected to http://localhost:18086 version 1.7.9 InfluxDB shell version: 1.7.9 > use test Using database test > |
插入数据到measurement中,语法:insert (measurement名称),(tag名称)=(xxoo) value=(ooxx)
例如,向名为cpuinfo的measurement中插入tag名为shijiange_192.168.1.101_cpu.idle,filed为value的数据:
1 2 3 4 5 6 7 8 |
> insert cpuinfo,item=shijiange_192.168.1.101_cpu.idle value=90 > insert cpuinfo,item=shijiange_192.168.1.101_cpu.idle value=80 > insert cpuinfo,item=shijiange_192.168.1.101_cpu.idle value=70 > insert cpuinfo,item=shijiange_192.168.1.101_cpu.idle value=60 > insert cpuinfo,item=shijiange_192.168.1.101_cpu.idle value=50 > insert cpuinfo,item=shijiange_192.168.1.101_cpu.idle value=40 > insert cpuinfo,item=shijiange_192.168.1.101_cpu.idle value=10 > insert cpuinfo,item=shijiange_192.168.1.101_cpu.idle value=1 |
然后查看各种:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
> show series key --- cpuinfo,item=fuck_127.0.0.1_cpu.idle cpuinfo,item=shijiange_192.168.1.101_cpu.idle > show measurements name: measurements name ---- cpuinfo > show tag keys name: cpuinfo tagKey ------ item > show field keys name: cpuinfo fieldKey fieldType -------- --------- value float > |
查看measurement中的数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[root@emr-header-1 ~]# influx -host 'localhost' -port '18086' -username 'test_user' -password 'test_password' Connected to http://localhost:18086 version 1.7.9 InfluxDB shell version: 1.7.9 > select * from cpuinfo ERR: database name required Warning: It is possible this error is due to not setting a database. Please set a database with the command "use <database>". > use test Using database test > select * from cpuinfo name: cpuinfo time item value ---- ---- ----- 1578936964760796661 shijiange_192.168.1.101_cpu.idle 90 1578984793473551072 shijiange_192.168.1.101_cpu.idle 80 1578984796570012726 shijiange_192.168.1.101_cpu.idle 70 1578984799130356415 shijiange_192.168.1.101_cpu.idle 60 1578984800930256198 shijiange_192.168.1.101_cpu.idle 50 1578984803530282668 shijiange_192.168.1.101_cpu.idle 40 1578984806410297002 shijiange_192.168.1.101_cpu.idle 10 1578984808570229221 shijiange_192.168.1.101_cpu.idle 1 1578984871328269165 fuck_127.0.0.1_cpu.idle 20 1578984873313275218 fuck_127.0.0.1_cpu.idle 25 1578984876008298409 fuck_127.0.0.1_cpu.idle 30 1578984878153110314 fuck_127.0.0.1_cpu.idle 80 1578984879888268827 fuck_127.0.0.1_cpu.idle 90 1578984881714157120 fuck_127.0.0.1_cpu.idle 100 > |
使用命令precision rfc3339可以将时间格式化为可读形式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
> precision rfc3339 > select * from cpuinfo name: cpuinfo time item value ---- ---- ----- 2020-01-13T00:36:04.760796661Z shijiange_192.168.1.101_cpu.idle 90 2020-01-13T06:53:13.473551072Z shijiange_192.168.1.101_cpu.idle 80 2020-01-13T06:53:16.570012726Z shijiange_192.168.1.101_cpu.idle 70 2020-01-13T06:53:19.130356415Z shijiange_192.168.1.101_cpu.idle 60 2020-01-13T06:53:20.930256198Z shijiange_192.168.1.101_cpu.idle 50 2020-01-13T06:53:23.530282668Z shijiange_192.168.1.101_cpu.idle 40 2020-01-13T06:53:26.410297002Z shijiange_192.168.1.101_cpu.idle 10 2020-01-13T06:53:28.570229221Z shijiange_192.168.1.101_cpu.idle 1 2020-01-13T06:54:31.328269165Z fuck_127.0.0.1_cpu.idle 20 2020-01-13T06:54:33.313275218Z fuck_127.0.0.1_cpu.idle 25 2020-01-13T06:54:36.008298409Z fuck_127.0.0.1_cpu.idle 30 2020-01-13T06:54:38.153110314Z fuck_127.0.0.1_cpu.idle 80 2020-01-13T06:54:39.888268827Z fuck_127.0.0.1_cpu.idle 90 2020-01-13T06:54:41.71415712Z fuck_127.0.0.1_cpu.idle 100 |
删除measurement:
1 |
drop measurement cpuinfo |
四、数据保留时间设置
查看数据库的数据保留策略:
1 2 3 4 5 |
> SHOW RETENTION POLICIES ON test name duration shardGroupDuration replicaN default ---- -------- ------------------ -------- ------- autogen 0s 168h0m0s 1 true 11 24h0m0s 1h0m0s 1 false |
设置数据保留1个月:
1 2 3 4 5 6 7 |
> CREATE RETENTION POLICY retenthion_test ON test DURATION 22d REPLICATION 1 DEFAULT > SHOW RETENTION POLICIES ON test name duration shardGroupDuration replicaN default ---- -------- ------------------ -------- ------- autogen 0s 168h0m0s 1 false 11 24h0m0s 1h0m0s 1 false retenthion_test 528h0m0s 24h0m0s 1 true |
删除某个数据保留策略(注意双引号,如果标识符包含除了[A-z,0-9,_]之外的字符,或者以数字开头,又或者是InfluxQL关键字,那么它们必须使用双引号。虽然并不总是需要,但是建议为标识符加上双引号):
1 2 3 4 5 6 7 8 |
> DROP RETENTION POLICY 11 ON test ERR: error parsing query: found 11, expected identifier at line 1, char 23 > DROP RETENTION POLICY "11" ON test > SHOW RETENTION POLICIES ON test name duration shardGroupDuration replicaN default ---- -------- ------------------ -------- ------- autogen 0s 168h0m0s 1 false retenthion_test 528h0m0s 24h0m0s 1 true |
修改某个已存在的数据保留策略:
1 2 3 4 5 6 7 |
> ALTER RETENTION POLICY retenthion_test ON test DURATION 1d REPLICATION 1 DEFAULT > SHOW RETENTION POLICIES ON test name duration shardGroupDuration replicaN default ---- -------- ------------------ -------- ------- autogen 0s 168h0m0s 1 false retenthion_test 24h0m0s 24h0m0s 1 true > |
注意,如果把retention polocy中default都是fasle,那么在插入数据时会报错:
1 2 3 4 5 6 7 8 9 10 |
> DROP RETENTION POLICY "retenthion_test" ON test > SHOW RETENTION POLICIES ON test name duration shardGroupDuration replicaN default ---- -------- ------------------ -------- ------- autogen 0s 168h0m0s 1 false > insert cpuinfo,item=shijiange_47.105.99.75_cpu.idle value=80 ERR: {"error":"retention policy not found: retenthion_test"} > |
此时,需要修改下默认的保留策略:
1 2 3 4 5 |
> ALTER retention policy "autogen" on test DEFAULT > SHOW RETENTION POLICIES ON test name duration shardGroupDuration replicaN default ---- -------- ------------------ -------- ------- autogen 0s 168h0m0s 1 true |
参考文档:
1、https://jasper-zhang1.gitbooks.io/influxdb/content
2、https://helpcdn.aliyun.com/document_detail/113094.html
转载时请保留出处,违法转载追究到底:进城务工人员小梅 » Centos 7.4上InfluxDB 1.7.9的安装及简单使用(单机)