You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API获取以MySQL为数据源的Grafana仪表板数据?

How to Fetch MySQL Data from Grafana via API (Similar to Prometheus Instant Queries)

Great question! While Grafana doesn’t have a dedicated "MySQL records" endpoint, it offers a generic Datasource Query API that lets you send queries directly to your connected MySQL datasource—just like how you used Prometheus’s Instant Queries API. Here’s how to make it work:

1. Get Your MySQL Datasource UID

First, you need the unique identifier (UID) of your MySQL datasource in Grafana. You can get this two ways:

  • Via Grafana UI: Go to Configuration > Data Sources, select your MySQL datasource. The UID will be visible in the page URL (e.g., /datasources/edit/abc123) or displayed prominently on the datasource settings page.
  • Via API: Use the datasources list endpoint to fetch all connected datasources and filter for your MySQL one:
    curl -H "Authorization: Bearer YOUR_GRAFANA_API_KEY" https://your-grafana-url.com/api/datasources
    
    Look for the entry with type: "mysql" and grab its uid value.

2. Send a Query to the Datasource API

Use the /api/datasources/uid/<YOUR_MYSQL_UID>/query endpoint to execute your MySQL query through Grafana. This acts as a proxy between your application and the MySQL database, using the credentials Grafana already has stored.

Here’s an example curl command:

curl -X POST -H "Authorization: Bearer YOUR_GRAFANA_API_KEY" \
  -H "Content-Type: application/json" \
  https://your-grafana-url.com/api/datasources/uid/mysql-ds-xyz/query \
  -d '{
    "queries": [
      {
        "refId": "A",
        "sql": "SELECT timestamp, metric_value FROM your_metrics_table WHERE timestamp >= NOW() - INTERVAL 24 HOUR",
        "format": "table"
      }
    ],
    "from": "now-24h",
    "to": "now"
  }'

Key Parameters:

  • refId: A unique identifier for your query (useful if sending multiple queries in one request).
  • sql: Your actual MySQL query (the same one you use in your Grafana panels).
  • format: Choose "table" for row-based data or "time_series" if you need data formatted for time series charts.
  • from/to: Optional time range parameters (aligns with Grafana’s time picker; you can use relative values like now-24h or absolute timestamps).

3. Parse the Response

The API returns a structured JSON response with your MySQL data. Here’s a simplified example:

{
  "results": {
    "A": {
      "frames": [
        {
          "schema": {
            "fields": [
              {"name": "timestamp", "type": "time"},
              {"name": "metric_value", "type": "number"}
            ]
          },
          "data": {
            "values": [
              [1690000000000, 1690003600000],
              [45.2, 52.8]
            ]
          }
        }
      ]
    }
  }
}

The values array contains rows of data, with each sub-array corresponding to a field in your query.

Alternative: Reuse Dashboard Panel Queries

If you want to pull the exact queries already configured in your Grafana dashboard panels:

  1. Fetch the dashboard JSON using GET /api/dashboards/uid/<DASHBOARD_UID>.
  2. Locate the panel you want in the panels array, then extract the query from panels[].targets[].sql.
  3. Plug that query into the datasource API request above. This is great if you want to avoid duplicating query logic.

Important Notes

  • API Key Permissions: Create an API key in Grafana (Configuration > API Keys) with a role like Editor or Admin—Viewer roles typically don’t have permission to execute datasource queries.
  • Variable Substitution: If your panel uses Grafana variables (e.g., $time_range, $metric), you’ll need to substitute those values in your query before sending it, or resolve them using Grafana’s /api/templating/values endpoint.
  • Rate Limiting: Be mindful of Grafana’s API rate limits to avoid being blocked.

内容的提问来源于stack exchange,提问作者Tech Girl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 14:32:31