|
@@ -12,6 +12,8 @@ weight = 7
|
|
|
# Using MySQL in Grafana
|
|
# Using MySQL in Grafana
|
|
|
|
|
|
|
|
> Only available in Grafana v4.3+.
|
|
> Only available in Grafana v4.3+.
|
|
|
|
|
+>
|
|
|
|
|
+> Starting from Grafana v5.1 you can name the time column *time* in addition to earlier supported *time_sec*. Usage of *time_sec* will eventually be deprecated.
|
|
|
|
|
|
|
|
Grafana ships with a built-in MySQL data source plugin that allow you to query any visualize
|
|
Grafana ships with a built-in MySQL data source plugin that allow you to query any visualize
|
|
|
data from a MySQL compatible database.
|
|
data from a MySQL compatible database.
|
|
@@ -23,6 +25,17 @@ data from a MySQL compatible database.
|
|
|
3. Click the `+ Add data source` button in the top header.
|
|
3. Click the `+ Add data source` button in the top header.
|
|
|
4. Select *MySQL* from the *Type* dropdown.
|
|
4. Select *MySQL* from the *Type* dropdown.
|
|
|
|
|
|
|
|
|
|
+### Data source options
|
|
|
|
|
+
|
|
|
|
|
+Name | Description
|
|
|
|
|
+------------ | -------------
|
|
|
|
|
+*Name* | The data source name. This is how you refer to the data source in panels & queries.
|
|
|
|
|
+*Default* | Default data source means that it will be pre-selected for new panels.
|
|
|
|
|
+*Host* | The IP address/hostname and optional port of your MySQL instance.
|
|
|
|
|
+*Database* | Name of your MySQL database.
|
|
|
|
|
+*User* | Database user's login/username
|
|
|
|
|
+*Password* | Database user's password
|
|
|
|
|
+
|
|
|
### Database User Permissions (Important!)
|
|
### Database User Permissions (Important!)
|
|
|
|
|
|
|
|
The database user you specify when you add the data source should only be granted SELECT permissions on
|
|
The database user you specify when you add the data source should only be granted SELECT permissions on
|
|
@@ -46,10 +59,11 @@ To simplify syntax and to allow for dynamic parts, like date range filters, the
|
|
|
Macro example | Description
|
|
Macro example | Description
|
|
|
------------ | -------------
|
|
------------ | -------------
|
|
|
*$__time(dateColumn)* | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time_sec`. For example, *UNIX_TIMESTAMP(dateColumn) as time_sec*
|
|
*$__time(dateColumn)* | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time_sec`. For example, *UNIX_TIMESTAMP(dateColumn) as time_sec*
|
|
|
|
|
+*$__timeEpoch(dateColumn)* | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time_sec`. For example, *UNIX_TIMESTAMP(dateColumn) as time_sec*
|
|
|
*$__timeFilter(dateColumn)* | Will be replaced by a time range filter using the specified column name. For example, *dateColumn > FROM_UNIXTIME(1494410783) AND dateColumn < FROM_UNIXTIME(1494497183)*
|
|
*$__timeFilter(dateColumn)* | Will be replaced by a time range filter using the specified column name. For example, *dateColumn > FROM_UNIXTIME(1494410783) AND dateColumn < FROM_UNIXTIME(1494497183)*
|
|
|
*$__timeFrom()* | Will be replaced by the start of the currently active time selection. For example, *FROM_UNIXTIME(1494410783)*
|
|
*$__timeFrom()* | Will be replaced by the start of the currently active time selection. For example, *FROM_UNIXTIME(1494410783)*
|
|
|
*$__timeTo()* | Will be replaced by the end of the currently active time selection. For example, *FROM_UNIXTIME(1494497183)*
|
|
*$__timeTo()* | Will be replaced by the end of the currently active time selection. For example, *FROM_UNIXTIME(1494497183)*
|
|
|
-*$__timeGroup(dateColumn,'5m')* | Will be replaced by an expression usable in GROUP BY clause. For example, *cast(cast(UNIX_TIMESTAMP(dateColumn)/(300) as signed)*300 as signed) as time_sec,*
|
|
|
|
|
|
|
+*$__timeGroup(dateColumn,'5m')* | Will be replaced by an expression usable in GROUP BY clause. For example, *cast(cast(UNIX_TIMESTAMP(dateColumn)/(300) as signed)*300 as signed),*
|
|
|
*$__timeGroup(dateColumn,'5m',0)* | Same as above but with a fill parameter so all null values will be converted to the fill value (all null values would be set to zero using this example).
|
|
*$__timeGroup(dateColumn,'5m',0)* | Same as above but with a fill parameter so all null values will be converted to the fill value (all null values would be set to zero using this example).
|
|
|
*$__unixEpochFilter(dateColumn)* | Will be replaced by a time range filter using the specified column name with times represented as unix timestamp. For example, *dateColumn > 1494410783 AND dateColumn < 1494497183*
|
|
*$__unixEpochFilter(dateColumn)* | Will be replaced by a time range filter using the specified column name with times represented as unix timestamp. For example, *dateColumn > 1494410783 AND dateColumn < 1494497183*
|
|
|
*$__unixEpochFrom()* | Will be replaced by the start of the currently active time selection as unix timestamp. For example, *1494410783*
|
|
*$__unixEpochFrom()* | Will be replaced by the start of the currently active time selection as unix timestamp. For example, *1494410783*
|
|
@@ -85,52 +99,50 @@ The resulting table panel:
|
|
|
|
|
|
|
|

|
|

|
|
|
|
|
|
|
|
-### Time series queries
|
|
|
|
|
-
|
|
|
|
|
-If you set `Format as` to `Time series`, for use in Graph panel for example, then there are some requirements for
|
|
|
|
|
-what your query returns.
|
|
|
|
|
|
|
+## Time series queries
|
|
|
|
|
|
|
|
-- Must be a column named `time_sec` representing a unix epoch in seconds.
|
|
|
|
|
-- Must be a column named `value` representing the time series value.
|
|
|
|
|
-- Must be a column named `metric` representing the time series name.
|
|
|
|
|
|
|
+If you set `Format as` to `Time series`, for use in Graph panel for example, then the query must return a column named `time` that returns either a sql datetime or any numeric datatype representing unix epoch.
|
|
|
|
|
+Any column except `time` and `metric` is treated as a value column.
|
|
|
|
|
+You may return a column named `metric` that is used as metric name for the value column.
|
|
|
|
|
|
|
|
-Example:
|
|
|
|
|
|
|
+**Example with `metric` column:**
|
|
|
|
|
|
|
|
```sql
|
|
```sql
|
|
|
SELECT
|
|
SELECT
|
|
|
- min(UNIX_TIMESTAMP(time_date_time)) as time_sec,
|
|
|
|
|
- max(value_double) as value,
|
|
|
|
|
- metric1 as metric
|
|
|
|
|
|
|
+ $__timeGroup(time_date_time,'5m'),
|
|
|
|
|
+ min(value_double),
|
|
|
|
|
+ 'min' as metric
|
|
|
FROM test_data
|
|
FROM test_data
|
|
|
-WHERE $__timeFilter(time_date_time)
|
|
|
|
|
-GROUP BY metric1, UNIX_TIMESTAMP(time_date_time) DIV 300
|
|
|
|
|
-ORDER BY time_sec asc
|
|
|
|
|
|
|
+WHERE $__timeFilter(time_date_time)
|
|
|
|
|
+GROUP BY time
|
|
|
|
|
+ORDER BY time
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-Example with $__timeGroup macro:
|
|
|
|
|
|
|
+**Example using the fill parameter in the $__timeGroup macro to convert null values to be zero instead:**
|
|
|
|
|
|
|
|
```sql
|
|
```sql
|
|
|
SELECT
|
|
SELECT
|
|
|
- $__timeGroup(time_date_time,'5m') as time_sec,
|
|
|
|
|
- min(value_double) as value,
|
|
|
|
|
- metric_name as metric
|
|
|
|
|
|
|
+ $__timeGroup(createdAt,'5m',0),
|
|
|
|
|
+ sum(value_double) as value,
|
|
|
|
|
+ measurement
|
|
|
FROM test_data
|
|
FROM test_data
|
|
|
-WHERE $__timeFilter(time_date_time)
|
|
|
|
|
-GROUP BY 1, metric_name
|
|
|
|
|
-ORDER BY 1
|
|
|
|
|
|
|
+WHERE
|
|
|
|
|
+ $__timeFilter(createdAt)
|
|
|
|
|
+GROUP BY time, measurement
|
|
|
|
|
+ORDER BY time
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
-Example using the fill parameter in the $__timeGroup macro to convert null values to be zero instead:
|
|
|
|
|
|
|
+**Example with multiple columns:**
|
|
|
|
|
|
|
|
```sql
|
|
```sql
|
|
|
-SELECT
|
|
|
|
|
- $__timeGroup(atimestamp,'24h',0) as time_sec,
|
|
|
|
|
- avg(afloat) as value,
|
|
|
|
|
- avarchar as metric
|
|
|
|
|
-FROM testdata.grafana_metrics
|
|
|
|
|
-WHERE $__timeFilter(atimestamp)
|
|
|
|
|
-GROUP BY 1, avarchar
|
|
|
|
|
-ORDER BY 1
|
|
|
|
|
|
|
+SELECT
|
|
|
|
|
+ $__timeGroup(time_date_time,'5m'),
|
|
|
|
|
+ min(value_double) as min_value,
|
|
|
|
|
+ max(value_double) as max_value
|
|
|
|
|
+FROM test_data
|
|
|
|
|
+WHERE $__timeFilter(time_date_time)
|
|
|
|
|
+GROUP BY time
|
|
|
|
|
+ORDER BY time
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
Currently, there is no support for a dynamic group by time based on time range & panel width.
|
|
Currently, there is no support for a dynamic group by time based on time range & panel width.
|
|
@@ -194,7 +206,7 @@ There are two syntaxes:
|
|
|
|
|
|
|
|
```sql
|
|
```sql
|
|
|
SELECT
|
|
SELECT
|
|
|
- UNIX_TIMESTAMP(atimestamp) as time_sec,
|
|
|
|
|
|
|
+ UNIX_TIMESTAMP(atimestamp) as time,
|
|
|
aint as value,
|
|
aint as value,
|
|
|
avarchar as metric
|
|
avarchar as metric
|
|
|
FROM my_table
|
|
FROM my_table
|
|
@@ -206,7 +218,7 @@ ORDER BY atimestamp ASC
|
|
|
|
|
|
|
|
```sql
|
|
```sql
|
|
|
SELECT
|
|
SELECT
|
|
|
- UNIX_TIMESTAMP(atimestamp) as time_sec,
|
|
|
|
|
|
|
+ UNIX_TIMESTAMP(atimestamp) as time,
|
|
|
aint as value,
|
|
aint as value,
|
|
|
avarchar as metric
|
|
avarchar as metric
|
|
|
FROM my_table
|
|
FROM my_table
|
|
@@ -216,23 +228,37 @@ ORDER BY atimestamp ASC
|
|
|
|
|
|
|
|
## Annotations
|
|
## Annotations
|
|
|
|
|
|
|
|
-[Annotations]({{< relref "reference/annotations.md" >}}) allows you to overlay rich event information on top of graphs. You add annotation queries via the Dashboard menu / Annotations view.
|
|
|
|
|
|
|
+[Annotations]({{< relref "reference/annotations.md" >}}) allow you to overlay rich event information on top of graphs. You add annotation queries via the Dashboard menu / Annotations view.
|
|
|
|
|
|
|
|
-An example query:
|
|
|
|
|
|
|
+**Example query using time column with epoch values:**
|
|
|
|
|
|
|
|
```sql
|
|
```sql
|
|
|
SELECT
|
|
SELECT
|
|
|
- UNIX_TIMESTAMP(atimestamp) as time_sec,
|
|
|
|
|
- value as text,
|
|
|
|
|
|
|
+ epoch_time as time,
|
|
|
|
|
+ metric1 as text,
|
|
|
CONCAT(tag1, ',', tag2) as tags
|
|
CONCAT(tag1, ',', tag2) as tags
|
|
|
-FROM my_table
|
|
|
|
|
-WHERE $__timeFilter(atimestamp)
|
|
|
|
|
-ORDER BY atimestamp ASC
|
|
|
|
|
|
|
+FROM
|
|
|
|
|
+ public.test_data
|
|
|
|
|
+WHERE
|
|
|
|
|
+ $__unixEpochFilter(epoch_time)
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+**Example query using time column of native sql date/time data type:**
|
|
|
|
|
+
|
|
|
|
|
+```sql
|
|
|
|
|
+SELECT
|
|
|
|
|
+ native_date_time as time,
|
|
|
|
|
+ metric1 as text,
|
|
|
|
|
+ CONCAT(tag1, ',', tag2) as tags
|
|
|
|
|
+FROM
|
|
|
|
|
+ public.test_data
|
|
|
|
|
+WHERE
|
|
|
|
|
+ $__timeFilter(native_date_time)
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
Name | Description
|
|
Name | Description
|
|
|
------------ | -------------
|
|
------------ | -------------
|
|
|
-time_sec | The name of the date/time field.
|
|
|
|
|
|
|
+time | The name of the date/time field. Could be a column with a native sql date/time data type or epoch value.
|
|
|
text | Event description field.
|
|
text | Event description field.
|
|
|
tags | Optional field name to use for event tags as a comma separated string.
|
|
tags | Optional field name to use for event tags as a comma separated string.
|
|
|
|
|
|