{"id":209,"date":"2024-05-22T01:13:07","date_gmt":"2024-05-22T01:13:07","guid":{"rendered":"https:\/\/www.rajeshkumar.xyz\/blog\/?p=209"},"modified":"2024-05-22T04:12:13","modified_gmt":"2024-05-22T04:12:13","slug":"how-to-connect-grafana-to-a-remote-mysql-database","status":"publish","type":"post","link":"https:\/\/www.rajeshkumar.xyz\/blog\/how-to-connect-grafana-to-a-remote-mysql-database\/","title":{"rendered":"How to Connect Grafana to a Remote MySQL Database"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Grafana is one of the most widely-used interactive data visualization tools on the market. It\u2019s open-source, powerful, highly configurable and free to use. I\u2019ve walked you through the process of&nbsp;<a href=\"https:\/\/www.techrepublic.com\/index.php\/videos\/how-to-install-the-grafana-enterprise-edition-on-ubuntu-server-20-04\/?mobile=true\">installing Grafana on Ubuntu Server 20.04<\/a>, and this time around, I\u2019ll help you connect that newly installed platform to a MySQL database, so you can visualize that data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-youll-need\">What you\u2019ll need<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To make this work, you\u2019re going to need a running instance of Grafana, a running instance of MySQL and a user with&nbsp;sudo privileges.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For this tutorial, I am going to assume your instance of&nbsp;MySQL is on a remote Linux server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-configure-mysql-for-remote-connection\">How to configure MySQL for remote connection<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The first thing we must do is configure the MySQL server to allow remote connections. On top of that, we\u2019re going to create a specific user that has specific permissions for the database we\u2019ll view on Grafana.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Configure Mariadb for remote access<\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ systemctl status mysql\n$ systemctl stop mysql\n$ systemctl status mysql\n$ more my.cnf\n$ cd conf.d\/\n$ vi mysql.cnf\ncd ..\n$ grep -R bind-address .\n$ grep -R 50-server.cnf .\n$ vi .\/mariadb.conf.d\/50-server.cnf\n$ systemctl start mysql\n$ systemctl status mysql<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Configure MySQL for remote access<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First, log into your MySQL server and open the MySQL configuration file with:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nano \/etc\/mysql\/mysql.conf.d\/mysqld.cnf<\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>In that file, look for the line:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">bind-address = 127.0.0.1<\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>Change that to:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">bind-address = 0.0.0.0<\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>Save and close the file, then restart the MySQL service with:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart mysql<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create a new user with specific permissions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we\u2019ve 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:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Log in to the MySQL console with:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo mysql -u root -p<\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>Make sure you know which database you\u2019re going to be using. You can list them out with:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">show databases;<\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>Create the new user with:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE USER 'grafanareader' IDENTIFIED BY 'PWORD';<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Where PWORD is a strong\/unique password for the new user.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>Now, we can grant that new user the SELECT permission for the database with:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">GRANT SELECT ON DB.* TO 'grafanareader';<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Where DB is the name of the database to be read by Grafana.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li>Flush the privileges and exit from the MySQL console with:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">flush privileges;\nexit<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Your database server is now ready.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-connect-grafana-to-mysql\">How to connect Grafana to MySQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 (<strong>Figure A<\/strong>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Figure A<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_4163814\"><img decoding=\"async\" src=\"https:\/\/assets.techrepublic.com\/uploads\/2023\/09\/tr_09182023-how-to-connect-grafana-to-a-remote-mysql-database-figure_a-770x304.jpg\" alt=\"Screencapture of Grafana interface with Add new connection option and the Options button on highlight.\" class=\"wp-image-4163814\"\/><figcaption class=\"wp-element-caption\">Add a new connection.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the next window, type mysql in the search bar, and hit Enter, or scroll down, and click MySQL from the listing (<strong>Figure B<\/strong>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Figure B<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_4163813\"><img decoding=\"async\" src=\"https:\/\/assets.techrepublic.com\/uploads\/2023\/09\/tr_09182023-how-to-connect-grafana-to-a-remote-mysql-database-figure_b-770x304.jpg\" alt=\"The MySQL Grafana configuration selection page.\" class=\"wp-image-4163813\"\/><figcaption class=\"wp-element-caption\">The MySQL Grafana configuration selection page.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">On the next page, click on the blue Add new data source button in the upper right (<strong>Figure C<\/strong>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Figure C<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_4163812\"><img decoding=\"async\" src=\"https:\/\/assets.techrepublic.com\/uploads\/2023\/09\/tr_09182023-how-to-connect-grafana-to-a-remote-mysql-database-figure_c-770x304.jpg\" alt=\"Prelude to adding a new MySQL connection, with Add new data source button on highlight.\" class=\"wp-image-4163812\"\/><figcaption class=\"wp-element-caption\">Prelude to adding a new MySQL connection.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You will then be presented with the necessary configuration options for a MySQL data connection. Here\u2019s what you must fill out:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Host:<\/strong>&nbsp;The IP address or domain name of the MySQL hosting server, along with the port used for the database server. Note that if you are setting up a connection to localhost:3306, which is the default value specified, you must click into the field and type it in, or else the connection will not work.<\/li>\n\n\n\n<li><strong>Database:<\/strong>&nbsp;The database to be used as a source.<\/li>\n\n\n\n<li><strong>User:<\/strong>&nbsp;grafanareader.<\/li>\n\n\n\n<li><strong>Password:<\/strong>&nbsp;PWORD used to create grafanareader in the MySQL console.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Configure those options, and click Save &amp; Test. You should eventually see Database Connection OK (<strong>Figure D<\/strong>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Figure D<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_4163811\"><img decoding=\"async\" src=\"https:\/\/assets.techrepublic.com\/uploads\/2023\/09\/tr_09182023-how-to-connect-grafana-to-a-remote-mysql-database-figure_d-770x716.jpg\" alt=\"The completed connection setup in Grafana.\" class=\"wp-image-4163811\"\/><figcaption class=\"wp-element-caption\">The completed connection setup.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations! You now have Grafana connected to a remote MySQL server. In our next piece in the series, we\u2019ll create a new dashboard to view some of the data from the source.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you have successfully connected to your MySQL data source<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grafana is one of the most widely-used interactive data visualization tools on the market. It\u2019s open-source, powerful, highly configurable and&#8230; <\/p>\n","protected":false},"author":1,"featured_media":4163814,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"series":[],"class_list":["post-209","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":3,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":215,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/posts\/209\/revisions\/215"}],"wp:attachment":[{"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/tags?post=209"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.rajeshkumar.xyz\/blog\/wp-json\/wp\/v2\/series?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}