Home > Article > Operation and Maintenance > A must-read for operation and maintenance monitoring: A must-know InfluxDB usage guide that can be used at critical moments
InfluxDB is an open source sequential database developed by InfluxData. It is written in Go and focuses on high-performance query and storage of time series data. InfluxDB is widely used in scenarios such as monitoring data of storage systems and real-time data in the IoT industry. Technical features include:
Measurement: A string represents the meaning of the record. . For example, it can be monitoring data cpu_load, or measurement data average_temperature
tag group: It consists of a set of key-value pairs, which represents a series of attribute information of the record. The same measurement data does not necessarily have the same tag group, and it is Schema-free. Tag information is indexed by default.
Field group: It is also composed of a set of key-value pairs, which represents the specific value information (with a name) of the record. Definable value types in the field group include: 64-bit integer, 64-bit floating point, string and Boolean. Field information cannot be indexed.
Time stamp: It is the time attribute of the record. If the timestamp is not explicitly specified when inserting data, the timestamp stored in the database by default will be the entry time of the record.
InfluxDB supports HTTP-based data insertion and query. It also accepts connections directly based on TCP or UDP protocols.
InfluxDB allows users to define data retention policies (Retention Policies) to delete or downsample data stored for more than a specified period of time.
Enter influxDB command line
influx -precision rfc3339
InfluxDB database operation
show databases
create database shhnwangjian
drop database shhnwangjian
use shhnwangjian
In InfluxDB, there is no concept of table (table), instead it is MEASUREMENTS. The functions of MEASUREMENTS are consistent with the tables in traditional databases, so we can also call MEASUREMENTS a table in InfluxDB.
• Display all tables
SHOW MEASUREMENTS
• Create a new table
There is no explicit statement to create a new table in InfluxDB. New tables can only be created by inserting data.
insert disk_free,hostname=server01 value=442221834240i insert cpu_virtual_used_num,host=1 value=41556593150
where disk_free is the table name, hostname is the index (tag), value=xx is the record value (field), there can be multiple record values, and the system comes with additional timestamps
Or when adding data, write the timestamp yourself
insert disk_free,hostname=server01 value=442221834240i 1435362189575692182
• Delete table
drop measurement disk_free
influxDB does not provide direct deletion of data records method, but provides a data storage strategy, which is mainly used to specify the data retention time. If the specified time is exceeded, this part of the data will be deleted.
show retention policies on "db_name" show retention policies on cdhnm
create retention policy "rp_name" on "db_name" duration 3w replication 1 default create retention policy test on cdhnm duration 1h replication 1 default
rp_name: Policy name;
db_name: specific database name;
3w: Save for 3 weeks, data before 3 weeks will be deleted. influxdb has various event parameters, such as: h (hour), d (day), w (week); replication 1: number of copies, usually 1 is enough;
default: set as the default policy
alter retention policy “rp_name” on “db_name” duration 30d default alter retention policy autogen on cdhnm duration 1h default
alter retention policy autogen on cdhnm duration 0h replication 1 default
drop retention policy “rp_name” on “db_name" drop retention policy test on cdhnm
select * fromcpu_virtual_used_num
Insert data and create a table at the same time
insert disk_free,hostname=server01 value=442221834240i insert cpu_virtual_used_num,host=470b14f0-e869-43ed-a8e6-fd634258271f,hostname=server01 value=0.3 1557023160
influxDB does not provide a method to directly delete data records, but it provides a data preservation strategy, which is mainly used to specify the data retention time, exceeding the specified time , delete this part of the data. Create a new database expiration policy for at least one hour
Retention Policies create retention policy "rp_name" on "db_name" duration 3w replication 1 default retention policy duration must be at least 1h0m0s influxdb
Query tag: show tag keys from cluster_metric
Query field: show field keys from cluster_metric
When using InfluxDB, I found that because InfluxDB uses UTC time, time zone problems are often encountered when querying
InfluxDB In addition to supporting epoch_time, it also supports rfc3339_date_time_string and rfc3339_like_date_time_string.
epoch_time
Some theoretical explanations are the time that has passed since Coordinated Universal Time (Thursday, 1 January 1970). For example, what we get by using System.currentTimeMillis() in a java program is this time. Generally, it is millisecond level (ms) precision, that is, 13-bit Long type. In InfluxDB, the accuracy of timestamps can reach nanosecond level (ns), which is the 19-bit Long type.
rfc3339_date_time_string
rfc3339时间格式是ietf协会定义的一种时间格式,这个名字是因为它被定义在rfc3339中。感兴趣的同学可以自己查看上面的连接。InfluxDB中rfc3339的时间格式是这样的:
‘YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ’
其中nnnnnnnnn是可选的,如果不写则会被设置为000000000。注意,如果使用这种时间格式,需要使用单括号(’)将时间括起来。
rfc3339_like_date_time_string
因为rfc3339_date_time_string的格式确实比较反人类,所以InfluxDB也支持这种人类阅读更友好的格式:
‘YYYY-MM-DD HH:MM:SS.nnnnnnnnn’
其中HH:MM:SS.nnnnnnnnn是可选的,如果不填写会被设置为00:00:00.000000000。所以查询时可以设置到天、小时、分钟、秒等不同精度。这种时间格式同样要求被单括号括起来。
InfluxDB默认东时间是纳秒(ns),即19位时间戳。但是一般情况下时间精度不会这么高。所以如果使用秒级精度查询:
select * from cpu_virtual_used_num where time >= 1435333209s and time <= 1542964713s
如果使用毫秒级精度查询:
select * from cpu_virtual_used_num where time >= 1435333209000ms and time <= 1542964714000ms
如果需要使用北京时间(东八区),可以在SQL中使用tc关键字:
select * from cpu_virtual_used_num where time >= '2018-11-23 14:30:39' and time <= '2019-11-23 14:32:32' tz('Asia/Shanghai')
Timestamp时间列
既然是时间序列数据库,influxdb 的数据都有一列名为 time 的列,里面存储 UTC 时间戳。
Influxdb 时间转成北京时间:UTC time + 8 hours = Beijing time
influx -precision rfc3339 show retention policies on cdhnm alter retention policy autogen on cdhnm duration 1h default create retention policy test on cdhnm duration 1h replication 1 default drop retention policy test on cdhnm insert cpu_virtual_used_num,host=470b14f0-e869-43ed-a8e6-fd634258271f,hostname=server01 value=0.9 1557045292000000000 select * from cpu_virtual_used_num where time >= '2018-11-23 14:30:39' and time <= '2019-11-23 14:32:32' tz('Asia/Shanghai') delete from cpu_virtual_used_num
切换到root用户 命令:su 输入密码:123456
启动:
sudo service influxdb start
重启:
service influxdb restart
切换到普通用户:
命令:exit
The above is the detailed content of A must-read for operation and maintenance monitoring: A must-know InfluxDB usage guide that can be used at critical moments. For more information, please follow other related articles on the PHP Chinese website!