如何通过API获取以MySQL为数据源的Grafana仪表板数据?
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:
Look for the entry withcurl -H "Authorization: Bearer YOUR_GRAFANA_API_KEY" https://your-grafana-url.com/api/datasourcestype: "mysql"and grab itsuidvalue.
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 likenow-24hor 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:
- Fetch the dashboard JSON using
GET /api/dashboards/uid/<DASHBOARD_UID>. - Locate the panel you want in the
panelsarray, then extract the query frompanels[].targets[].sql. - 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/valuesendpoint. - Rate Limiting: Be mindful of Grafana’s API rate limits to avoid being blocked.
内容的提问来源于stack exchange,提问作者Tech Girl

