# Prometheus 将数据远程写入 InfluxDB 存储

## **Prometheus 将数据远程写入 InfluxDB 存储** <a href="#prometheus-e5-b0-86-e6-95-b0-e6-8d-ae-e8-bf-9c-e7-a8-8b-e5-86-99-e5-85-a5-influxdb-e5-a-d-98-e5-82-a" id="prometheus-e5-b0-86-e6-95-b0-e6-8d-ae-e8-bf-9c-e7-a8-8b-e5-86-99-e5-85-a5-influxdb-e5-a-d-98-e5-82-a"></a>

* InfluxDB 1.x 提供了直接写入数据的 http 接口，Prometheus 可以直接写入，但是 InfluxDB 2.x 删除了这个接口。
* InfluxDB 2.x 引入了新的组件 Telegraf，必须借助 Telegraf 才可以将 Prometheus 的数据写入到 InfluxDB 2.x 中。

### **InfluxDB** 2.0 使用 Prometheus 架构图 <a href="#influxdb-2.0-e4-bd-bf-e7-94-a8-prometheus-e6-9e-b6-e6-9e-84-e5-9b-be" id="influxdb-2.0-e4-bd-bf-e7-94-a8-prometheus-e6-9e-b6-e6-9e-84-e5-9b-be"></a>

<figure><img src="/files/Vr53yzNbu8g8nhn0sf8Z" alt=""><figcaption></figcaption></figure>

### **InfluxDB** 2.0 不使用 Prometheus 架构图 <a href="#influxdb-2.0-e4-b8-8d-e4-bd-bf-e7-94-a8-prometheus-e6-9e-b6-e6-9e-84-e5-9b-be" id="influxdb-2.0-e4-b8-8d-e4-bd-bf-e7-94-a8-prometheus-e6-9e-b6-e6-9e-84-e5-9b-be"></a>

<figure><img src="/files/9X9ibyXukxxDk1TObFlJ" alt=""><figcaption></figcaption></figure>

### **InfluxDB** 2.0 与 **Telegraf 如何结合配置** <a href="#influxdb-2.0-e4-b8-8e-telegraf-e5-a6-82-e4-bd-95-e7-bb-93-e5-90-88-e9-85-8d-e7-bd-ae" id="influxdb-2.0-e4-b8-8e-telegraf-e5-a6-82-e4-bd-95-e7-bb-93-e5-90-88-e9-85-8d-e7-bd-ae"></a>

> 参考： <https://blog.csdn.net/catoop/article/details/127533069>

## 实操 InfluxDB 1.x 版本 <a href="#e5-ae-9e-e6-93-8d-influxdb-1.x-e7-89-88-e6-9c-ac" id="e5-ae-9e-e6-93-8d-influxdb-1.x-e7-89-88-e6-9c-ac"></a>

```yml
version: '3'

services:
  influxdb:
    image: 'influxdb:1.8'
    container_name: influxdb
    network_mode: host
    volumes:
      - './influxdb:/var/lib/influxdb'
    #ports:
    #  - '8086:8086'

  mysql:
    image: mysql:8.0
    restart: always
    container_name: mysql
    network_mode: host
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: grafana
      MYSQL_USER: grafana
      MYSQL_PASSWORD: grafana
      MYSQL_ROOT_HOST: '%'
      MYSQL_DEFAULT_AUTHENTICATION_PLUGIN: mysql_native_password  # 设置使用 MySQL 5.7 的加密算法
    volumes:
      - ./mysql:/var/lib/mysql
    #ports:
    #  - "3306:3306

  prometheus:
    image: prom/prometheus
    container_name: prometheus
    network_mode: host
    environment:
      TZ: Asia/Shanghai
    #ports:
    #  - "9090:9090"
    volumes:
      #- ./prometheus/k8s.token:/etc/prometheus/k8s.token 
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'

  grafana:
    image: grafana/grafana
    #image: grafana/grafana:8.5.27
    container_name: grafana
    network_mode: host
    volumes:
      - ./grafana/grafana.ini:/etc/grafana/grafana.ini
    depends_on:
      - mysql
    #ports:
    #  - "53000:3000"
```

```bash
[root@worker1 aaa]# cat prometheus/prometheus.yml 
global:
  scrape_interval: 15s

remote_write:
  - url: "http://<influxdb>:8086/api/v1/prom/write?db=prometheus"

remote_read:
  - url: "http://<influxdb>:8086/api/v1/prom/read?db=prometheus"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'test'
    static_configs:
      - targets: ['8.8.8.8:9100']
```

```bash
[root@worker1 aaa]# cat grafana/grafana.ini
......略
[database]
type = mysql
host = you_mysql_adderss:3306
name = grafana
user = grafana
password = grafana

[paths]
data = mysql
```

> <http://xxxx:3000>
>
> admin/admin
>
> 测试: 启动，收集一点数据，再卸载容器，再启动容器，查看刚刚收集的数据是否存在，存在就说明持久化成功


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/prometheus/prometheus-jiang-shu-ju-yuan-cheng-xie-ru-influxdb-cun-chu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
