You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.0 KiB

4 weeks ago
### 参考链接
* https://docs.influxdata.com/influxdb/v1.8/introduction/install/
* https://docs.influxdata.com/influxdb/v1.8/introduction/getting-started/
* [Spring Boot中使用时序数据库InfluxDB](http://www.manongjc.com/detail/60-zdzcqmqvsacgbql.html)
### 1、安装InfluxDB
参考链接
* https://docs.influxdata.com/influxdb/v1.8/introduction/install/
* [InfluxData TICK栈 1.X 在Windows下安装笔记](https://blog.csdn.net/m0_55628112/article/details/114238785)
#### 1.1 CentOS安装InfluxDB
* 更新配置文件:
```shell
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/influxdata-archive_compat.key
EOF
```
* 安装
```shell
sudo yum install influxdb
sudo systemctl enable influxdb
sudo systemctl start influxdb
```
* 配置
1) Create at least one admin
```shell
CREATE USER admin WITH PASSWORD 'root' WITH ALL PRIVILEGES
```
2) Enable authentication in your configuration file by setting the auth-enabled option to true in the [http] section:
```ini
[http]
enabled = true
bind-address = ":8086"
auth-enabled = true # Set to true
log-enabled = true
write-tracing = false
pprof-enabled = true
pprof-auth-enabled = true
debug-pprof-enabled = false
ping-auth-enabled = true
https-enabled = true
https-certificate = "/etc/ssl/influxdb.pem"
```
3. Restart InfluxDB.
### 2. 常用命令
#### 2.1 用户管理
```shell
# Create admin user
CREATE USER admin WITH PASSWORD '<password>' WITH ALL PRIVILEGES
# GRANT administrative privileges to an existing user
GRANT ALL PRIVILEGES TO <username>
# REVOKE administrative privileges from an admin user
REVOKE ALL PRIVILEGES FROM <username>
# SHOW all existing users and their admin status
SHOW USERS
# CREATE a new non-admin user
CREATE USER <username> WITH PASSWORD '<password>'
# GRANT READ, WRITE or ALL database privileges to an existing user
GRANT [READ,WRITE,ALL] ON <database_name> TO <username>
# REVOKE READ, WRITE, or ALL database privileges from an existing user
REVOKE [READ,WRITE,ALL] ON <database_name> FROM <username>
# SHOW GRANTS FOR <user_name>
SHOW GRANTS FOR <user_name>
# Reset a user’s password
SET PASSWORD FOR <username> = '<password>'
# DROP a user
DROP USER <username>
```
#### 2.2 数据库相关
```shell
CREATE DATABASE mydb
SHOW DATABASES
USE mydb
SHOW MEASUREMENTS
# 查看InfluxDB中的保留策略
SHOW RETENTION POLICIES ON <database_name>
# 删除InfluxDB中的保留策略
DROP RETENTION POLICY "<policy_name>" ON "<database_name>"
# 查看表结构
show field keys from measurement;
show tag keys from measurement;
```
### 3. springboot集成步骤
#### 3.1 添加Maven依赖
```xml
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
</dependency>
```
注意:这里因为Spring Boot 2.x版本的parent中有维护InfluxDB的SDK版本,所以不需要手工指明版本信息。
#### 3.2 配置influxdb连接信息
```yaml
spring:
influx:
url: http://127.0.0.1:8086
user: admin
password: root
```
#### 3.3 增加保留策略
##### 3.3.1 MQTT日志保留一年
要将MQTT日志数据保存为1年,可以使用以下步骤:
1. 首先,需要在InfluxDB中创建一个新的保留策略。可以使用以下命令在InfluxDB的CLI中创建一个名为"one_year"的保留策略:
```
CREATE RETENTION POLICY "one_year" ON "gas_alarm" DURATION 55w REPLICATION 1 DEFAULT
```
这将在"gas_alarm"的measurement上创建一个名为"one_year"的保留策略,保留时间为1年。
2. 然后,在写入数据时,将保留策略设置为"one_year"。可以使用以下代码将数据写入到"gas_alarm" measurement中,并将保留策略设置为"one_year":
```
influxDB.write("gas_alarm", "one_year", point);
```
这样,数据将以"one_year"保留策略保存在InfluxDB中,并且会在1年后被自动删除。