Grafana needs data to be useful. Phil Hajjar walks you through the process of using a MySQL database as a source for data visualization in Grafana.
Grafana is one of the most widely-used interactive data visualization tools on the market. It’s open-source, powerful, highly configurable and free to use. I’ve walked you through the process of installing Grafana on Ubuntu Server 20.04, and this time around, I’ll help you connect that newly installed platform to a MySQL database, so you can visualize that data.
Grafana allows you to connect to numerous data sources, such as Google Sheets, Amazon Timestream, Elasticsearch and many databases. Because MySQL is such a popular database, I thought it would be a great data source to use as an illustration. This will also refresh your memory on how to set MySQL up for remote connections.
Jump to:
To make this work, you’re going to need a running instance of Grafana, a running instance of MySQL and a user with sudo privileges.
For this tutorial, I am going to assume your instance of MySQL is on a remote Linux server.
The first thing we must do is configure the MySQL server to allow remote connections. On top of that, we’re going to create a specific user that has specific permissions for the database we’ll view on Grafana.
SEE: Explore these open-source and Linux terms you should know.
Going through the process below will help ensure MySQL is indeed configured correctly. In fact, if the MySQL server is not running on the same server as Grafana, it is critical that you follow the steps below first, as a stock MySQL installation usually is not configured to allow for remote connections.
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 127.0.0.1
bind-address = 0.0.0.0
sudo systemctl restart mysql
Now that we’ve set up our MySQL server to allow remote connections, we need to create a new user and give it the correct permissions. To do this:
sudo mysql -u root -p
show databases;
CREATE USER 'grafanareader' IDENTIFIED BY 'PWORD';
Where PWORD is a strong/unique password for the new user.
GRANT SELECT ON DB.* TO 'grafanareader';
Where DB is the name of the database to be read by Grafana.
flush privileges;
exit
Your database server is now ready.
Log into your Grafana instance, and click the hamburger (3 horizontal lines) icon in the left sidebar. From the resulting pop-up, click Add new connection (Figure A).
Figure A

In the next window, type mysql in the search bar, and hit Enter, or scroll down, and click MySQL from the listing (Figure B).
Figure B

On the next page, click on the blue Add new data source button in the upper right (Figure C).
Figure C

You will then be presented with the necessary configuration options for a MySQL data connection. Here’s what you must fill out:
You might also have to enable Skip TLS Verify. And you may want to consider specifying a name for this connection, especially if you need to have multiple MySQL connections from Grafana.
Configure those options, and click Save & Test. You should eventually see Database Connection OK (Figure D).
Figure D

Congratulations! You now have Grafana connected to a remote MySQL server. In our next piece in the series, we’ll create a new dashboard to view some of the data from the source.
Now that you have successfully connected to your MySQL data source, it’s time to take the next step by adding a dashboard.
SEE: Not satisfied with Grafana? Consider these cost-effective data visualization tools.
Phil is usually seen making things work together that shouldn’t be, but need to be. He describes himself as a marriage counselor for software and other technology systems. He appropriated this moniker way back in college as he first experimented with making disparate software work together back then, and he continues doing so in his over 20 years of professional IT experience now. When Phil isn’t making disagreeable software work in a cooperative fashion, he’s teaching things like Python and Linux Administration, as well as dabbling in new technology fields like cybersecurity and virtualization. He’s always looking for emerging technology trends to dabble in.