This topic shows you how to add a MySQL data source and create a dashboard in Grafana.
Step 1: Log on to Grafana
-
Log on to the Managed Service for Grafana console. In the left-side navigation pane, click Workspace Management.
-
On the Workspace Management page, find the workspace that you want to manage and click the URL in the URL column to go to Grafana.
NoteYou can log on to Grafana with the administrator account of Grafana and the password that you configured when you created the workspace. You can also click Sign in with Alibaba Cloud to log on to Grafana with the current Alibaba Cloud account.
Step 2: Add a MySQL data source
Grafana 9.0.x
-
In the left-side navigation pane, choose
> Data sources. -
On the Data sources tab, click Add data source. In the search box, search for and select MySQL.
-
On the Settings tab, configure the following parameters.
Parameter
Description
Name
The name of the data source.
Host
The address of the MySQL database in the format
[address]:[port]. For more information about how to obtain the address, see View and manage instance connection addresses and ports.Database
The name of the MySQL database.
User
The MySQL database username.
ImportantFor security, use a read-only account with limited permissions. Using a high-privilege account can expose your server to significant security risks.
Password
The MySQL database password.
Configure other parameters as needed. For more information, see the official Grafana documentation.
-
Click Save & test.
If the
Database Connection OKmessage is displayed, the MySQL data source has been added successfully.Note-
If an error occurs after you click Save & test, check whether the MySQL host address, database name, username, and password are correct.
-
If all the information is correct and your MySQL database is an ApsaraDB RDS for MySQL instance, you must configure a whitelist. For more information, see Set an IP whitelist.
-
If you connect using a public IP address, go to the Workspace Information page in the Managed Service for Grafana console. In the Basic Information section, obtain the Public Endpoint and add it to the whitelist of the ApsaraDB RDS for MySQL instance.
-
If you connect using a private IP address, go to the Whitelists and Security Groups page in the Managed Service for Grafana console. On the Private network whitelist settings tab, find the VPC and add its CIDR block (for example, 192.168.0.0/16) to the whitelist of the ApsaraDB RDS for MySQL instance. Also, ensure that the security group for the data channel allows traffic on the MySQL port.
-
-
Grafana 10.0.x
-
On the Grafana homepage, click the
icon. -
In the left-side navigation pane, choose .
-
On the Data Source tab, click + Add new data source. In the search box, search for and select MySQL.
-
On the Settings tab, configure the following parameters.
Parameter
Description
Name
The name of the data source.
Host
The address of the MySQL database in the format
[address]:[port]. For more information about how to obtain the address, see View and manage instance connection addresses and ports.Database
The name of the MySQL database.
User
The MySQL database username.
ImportantFor security, use a read-only account with limited permissions. Using a high-privilege account can expose your server to significant security risks.
Password
The MySQL database password.
Configure other parameters as needed. For more information, see the official Grafana documentation.
-
Click Save & test.
If the
Database Connection OKmessage is displayed, the MySQL data source has been added successfully.Note-
If an error occurs after you click Save & test, check whether the MySQL host address, database name, username, and password are correct.
-
If all the information is correct and your MySQL database is an ApsaraDB RDS for MySQL instance, you must configure a whitelist. For more information, see Set an IP whitelist.
-
If you connect using a public IP address, go to the Workspace Information page in the Managed Service for Grafana console. In the Basic Information section, obtain the Public Endpoint and add it to the whitelist of the ApsaraDB RDS for MySQL instance.
-
If you connect using a private IP address, go to the Whitelists and Security Groups page in the Managed Service for Grafana console. On the Private network whitelist settings tab, find the VPC and add its CIDR block (for example, 192.168.0.0/16) to the whitelist of the ApsaraDB RDS for MySQL instance. Also, ensure that the security group for the data channel allows traffic on the MySQL port.
-
-
Step 3: Create a dashboard
This section uses sample MySQL data to demonstrate how to create table and time series panels for a dashboard.
SELECT * FROM grafana_test.table_5
|
ID |
Place |
User |
Operate |
Operate time |
Count |
|
1 |
Beihai |
Zhu |
Browse |
2024-01-05 19:07:58 |
26 |
|
2 |
Putian |
Su |
Browse |
2024-02-08 20:58:33 |
17 |
|
3 |
Pingdingshan |
Cao |
Browse |
2024-01-09 09:28:59 |
63 |
|
4 |
Shuozhou |
Ren |
Browse |
2024-03-28 03:17:06 |
19 |
|
5 |
Dazhou |
Xu |
Browse |
2024-03-16 18:50:00 |
24 |
|
6 |
Fangchenggang |
Jiang |
Browse |
2024-04-14 02:15:21 |
42 |
|
7 |
Benxi |
Luo |
Browse |
2024-03-27 19:51:12 |
81 |
|
8 |
Yuncheng |
Du |
Browse |
2024-03-29 15:07:18 |
6 |
|
9 |
Yangjiang |
Shao |
Browse |
2024-01-14 23:23:23 |
91 |
|
10 |
Suining |
Sheng |
Browse |
2024-04-09 00:43:47 |
6 |
Create a table panel
Grafana 9.0.x
-
In the left-side navigation pane, choose
> Dashboards. -
On the Dashboards page, click New Dashboard.
-
On the New Dashboard page, click Add a new panel.
-
On the Query tab of the Edit Panel page, select your MySQL data source from the Data source drop-down list.
-
Click the
icon on the right side of the A section, enter the following query, and then set Format As to Table.SELECT `operate`, `place`, `ID`, `operate_time`, `count`, `user` FROM grafana_test.table_5 -
Click Apply.
-
In the upper-right corner, click the
icon. Then, specify the name of the dashboard and the directory in which the dashboard resides. -
Click Save.
Grafana 10.0.x
-
On the Grafana homepage, click the
icon. -
In the left-side navigation pane, click Dashboards. On the page that appears, select New dashboard from the New drop-down list.
-
On the New dashboard page, click + Add visualization.
-
In the Select data source panel, select your MySQL data source.
-
In the A section, set Format to Table. Then, click Code on the right and enter the following query.
SELECT `operate`, `place`, `ID`, `operate_time`, `count`, `user` FROM grafana_test.table_5 -
Click Apply.
-
In the upper-right corner, click the
icon. Then, specify the name of the dashboard and the directory in which the dashboard resides. -
Click Save.
Create a time series chart
Grafana 9.0.x
-
In an existing dashboard, click the
icon in the upper-right corner, and then click Add a new panel. -
On the right side of the Edit Panel page, in the Visualization area, set the chart type to Time series.
-
On the Edit Panel tab, in the Query section, select your MySQL data source from the Data source drop-down list.
-
In the A section, configure the query parameters.
Parameter
Description
FROM
Select the name of the table in your database.
Time column
Select the time column from the table, such as operate_time.
Metric column
Select the metric column from the table. You can also select none, which indicates that no specific column is used as the metric (or name) for data points.
SELECT
Specify the data you want to display.
WHERE
Select
$__timeFilteras the time filter.GROUP BY
Select
time($__interval,none). This groups and aggregates data by a dynamically calculated time interval.Format As
Select Time series as the format.
-
(Optional) In the Graph styles section on the right, you can set Style to Bars, set Bar alignment to
, and modify the value of Line width to display the data as a bar chart.For example, set Line width to
3and Fill opacity to100. -
After you complete the settings, click Apply in the upper-right corner.
Grafana 10.0.x
-
In an existing dashboard, click Add in the upper-right corner and select Visualization from the drop-down list.
-
On the right side of the Edit Panel page, in the Visualization area, set the chart type to Time series.
-
On the Edit Panel tab, in the Query section, select your MySQL data source.
-
In the A section, set Format to Table, configure the query parameters as described in the following table, and then click Run query.
Parameter
Description
Dataset
Select the name of your database.
Table
Select the name of the table.
Column
Select the columns that you want to query. You can also select an asterisk (*) to query all columns.
In the visualization type drop-down list in the upper-right corner, select Time series. In the query section, select MySQL from the Data source drop-down list.
NoteIf you want to query multiple specific columns but not all of them, click the
icon next to the Column field to add more columns. -
(Optional) On the right side of the A section, click Code to customize the chart by using an SQL query.
SELECT `operate_time` as time, SUM(`count`) as value, `operate` as metric FROM grafana_test.table_5 GROUP BY `operate`,`operate_time` ORDER BY `operate_time` ASCAdd
LIMIT 50to the end of the SQL query to limit the number of rows returned, and set Format to Time series. -
(Optional) In the Graph styles section on the right, you can set Style to Bars, set Bar alignment to
, and modify the value of Line width to display the data as a bar chart.For example, set Bar alignment to center, set both Line width and Fill opacity to
4, and set Gradient mode to None. -
After you complete the settings, click Apply in the upper-right corner.
icon in the upper-right corner, and then click Add a new panel.
, and modify the value of Line width to display the data as a bar chart.