MySQL Data in Grafana

In my recent study, I explored that Grafana not only helps users visualize performance metrics but also helps users to query information from a number of data sources; for example -MySQL. This encouraged me to further research and evaluate the tool and its connectivity with MySQL. It was worth an experiment and hence sharing the lessons in this blog.

Pre-requisites: Grafana and MySQL are installed and configured

Scenario: Display data from MySQL in Grafana. MySQL comprises of two tables (‘employee’ and ‘pay’).These tables have employee data and their respective reward points collected per month.  We will extract data from both these tables and display the rewards granted per month in Grafana. As an example, we will leverage following query to get the data –

“SELECT DISTINCT name,pay FROM employee,rewards”

The above query returns unique records of employees and their reward points.

Steps followed-

Step 1 -> Login into Grafana

Grafana home screen

Step 2 -> Click on DATA SOURCES to add MySQL database as the source of information.

Adding Data Source in Grafana

Search for the MYSQL DB plugin and click on SELECT button as shown below-

Searching for MySQL data source in Grafana

Step 3 ->  Now, we will configure the settings for this data source.

Configuring MySQL in Grafana

Test the configuration –

Saving and testing MySQL configuration in Grafana

If the connection is successful, you will receive the following notification-

MySQL data source and configuration test confirmation

Step 4 -> Click on CREATE NEW DASHBOARD to fetch data from the configured database. Select the option to create a dashboard and a NEW PANEL as displayed below-

Creating New Dashboard and Adding Panel in Grafana

There can be multiple panels on a single dashboard. Each panel needs to be configured. The following steps 4.1 till 4.3 describe the settings that are to be configured.

Step 4.1 –> Define SETTINGS (visible on right window pane)- Give a name to this new panel/section and describe it. For our example, lets name this section as “Reward Points Per Month”.

Panel Settings in Grafana

Step 4.2 -> Select ‘VISUALIZATION” (visible on right window pane, below the Settings option)– Now expand this menu to select the type of data display. Let us select “TABLE ” for our scenario-

Selecting Table as Visualization form in Grafana

Step 4.3 -> Define the QUERY (visible at the bottom window pane) – embed the query and execute to view the results as shown below –

Defining SQL Query in Grafana

Step 5 -> Once the configurations are done, SAVE the dashboard. Lets name it ‘Reward Points’-

Saving Grafana Dashboard

This dashboard now displays the following panel that shows DATA from MySQL tables–

Viewing Grafana Dashboard

That’s it.. we have our first dashboard in Grafana that displays data from MySQL. And in fact this is an easy example to get started for beginners. You can try this out and work with different SQLs based on the requirement.

Extending the scenario – Let us try adding another panel on this dashboard, this time we will add the panel and select “GAUGE” visualization setting. Let us assume we need to display the total rewards granted so far. You can either write the query as we did earlier or use the Query Builder to design the SQL. As shown below, just insert a new panel on the same dashboard, mention the query, select the visualization form as Gauge and then save and apply this new panel.

Adding Gauge Visualization in Grafana for MySQL Data

The updated dashboard will now display both the panels –

Gauge and Table Visualization forms in Grafana

Summary

Grafana definitely is a user friendly tool and offers a number of options to display data. While the key focus majorly is on time series data like displaying performance metrics (which we will cover in our next blog) but it also offers capability to display data either in raw form or in an aggregated form.

Are you too using Grafana? Do share in your experiences and learnings.

References-

Differences between Grafana vs. Kibana vs. Tableau – well known DVT tools!!