{"id":3312,"date":"2023-05-31T11:26:12","date_gmt":"2024-07-03T04:07:45","guid":{"rendered":""},"modified":"2025-02-02T00:46:20","modified_gmt":"2025-02-01T23:46:20","slug":"how-to-monitor-mysql-or-mariadb-with-prometheus-and-grafana","status":"publish","type":"post","link":"https:\/\/netcloud24.com\/knowledgebase\/how-to-monitor-mysql-or-mariadb-with-prometheus-and-grafana\/","title":{"rendered":"How to Monitor MySQL or MariaDB with Prometheus and Grafana"},"content":{"rendered":"<p>\u00a0<\/p>\n<\/p>\n<h1>\u00a0<\/h1>\n<h2>Introduction<\/h2>\n<p>Prometheus is a powerful monitoring system and time series database, and Grafana is a popular analytics and monitoring solution. This guide will show you how to set up monitoring for MySQL or MariaDB databases using these two tools.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>A server with MySQL or MariaDB installed.<\/li>\n<li>Root access or a user with <code>sudo<\/code> privileges.<\/li>\n<li>Prometheus installed on your system.<\/li>\n<li>Grafana installed on your system.<\/li>\n<\/ul>\n<h2>Step 1: Install Dependencies<\/h2>\n<p>Before starting, you need to ensure that the necessary dependencies are installed. For monitoring MySQL\/MariaDB, you will need the <strong>mysqld_exporter<\/strong>:<\/p>\n<pre><code>sudo apt install -y prometheus-mysqld-exporter<\/code><\/pre>\n<h2>Step 2: Configure mysqld_exporter<\/h2>\n<p>Create a new user in MySQL\/MariaDB for the exporter:<\/p>\n<pre><code>mysql -u root -p\r\nCREATE USER 'exporter'@'localhost' IDENTIFIED BY 'your_password';\r\nGRANT SELECT ON *.* TO 'exporter'@'localhost'; -- Grant necessary permissions\r\nFLUSH PRIVILEGES;\r\nEXIT;<\/code><\/pre>\n<p>Replace <code>your_password<\/code> with a secure password.<\/p>\n<h2>Step 3: Create mysqld_exporter Configuration<\/h2>\n<p>Create a configuration file for the mysqld_exporter:<\/p>\n<pre><code>sudo nano \/etc\/default\/prometheus-mysqld-exporter<\/code><\/pre>\n<p>Add the following lines to configure the MySQL connection:<\/p>\n<pre><code>DATA_SOURCE_NAME=\"exporter:your_password@tcp(127.0.0.1:3306)\/\" # Ensure you replace 'your_password'<\/code><\/pre>\n<p>Save and exit the editor.<\/p>\n<h2>Step 4: Start mysqld_exporter<\/h2>\n<p>Enable and start the mysqld_exporter service:<\/p>\n<pre><code>sudo systemctl enable prometheus-mysqld-exporter\r\nsudo systemctl start prometheus-mysqld-exporter<\/code><\/pre>\n<p>The exporter should now be running on port 9104.<\/p>\n<h2>Step 5: Configure Prometheus<\/h2>\n<p>Next, you need to configure Prometheus to scrape the MySQL\/MariaDB metrics. Open the Prometheus configuration file:<\/p>\n<pre><code>sudo nano \/etc\/prometheus\/prometheus.yml<\/code><\/pre>\n<p>Add the following job configuration under the <code>scrape_configs<\/code> section:<\/p>\n<pre><code>- job_name: 'mysql'\r\n    static_configs:\r\n      - targets: ['localhost:9104']<\/code><\/pre>\n<p>Save and exit the editor. Then, restart Prometheus:<\/p>\n<pre><code>sudo systemctl restart prometheus<\/code><\/pre>\n<h2>Step 6: Configure Grafana<\/h2>\n<p>Open your Grafana dashboard in a web browser, typically at <code>http:\/\/your_server_ip:3000<\/code>. Log in using the default credentials (username: <code>admin<\/code>, password: <code>admin<\/code>). You will be prompted to change the password.<\/p>\n<p>Add Prometheus as a data source:<\/p>\n<ul>\n<li>Click on &#8220;Configuration&#8221; (gear icon) &gt; &#8220;Data Sources&#8221;.<\/li>\n<li>Click on &#8220;Add Data Source&#8221; and select &#8220;Prometheus&#8221;.<\/li>\n<li>Set the URL to <code>http:\/\/localhost:9090<\/code> (or your Prometheus server address).<\/li>\n<li>Click &#8220;Save &amp; Test&#8221; to ensure it is working.<\/li>\n<\/ul>\n<h2>Step 7: Create a Dashboard<\/h2>\n<p>Now you can create a Grafana dashboard to visualize the MySQL metrics:<\/p>\n<ul>\n<li>Click on the &#8220;+&#8221; sign on the left sidebar, then select &#8220;Dashboard&#8221;.<\/li>\n<li>Click on &#8220;Add a new panel&#8221;.<\/li>\n<li>Select a query such as <code>mysql_global_status_threads_connected<\/code> to visualize the number of connected threads.<\/li>\n<li>Customize the visualization as needed and click &#8220;Apply&#8221;.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>You have successfully set up monitoring for MySQL or MariaDB using Prometheus and Grafana. You can now visualize and monitor your database performance in real-time.<\/p>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"https:\/\/prometheus.io\/\" target=\"_blank\" rel=\"follow\">Prometheus Official Website<\/a><\/li>\n<li><a href=\"https:\/\/grafana.com\/\" target=\"_blank\" rel=\"follow\">Grafana Official Website<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/percona\/mysqld_exporter\" target=\"_blank\" rel=\"follow\">mysqld_exporter GitHub Repository<\/a><\/li>\n<\/ul>\n<div class=\"post-author-box\" style=\"border-top:1px solid #ddd;margin-top:20px;padding-top:15px;\">\n<p><strong>Author:<\/strong> \u0141ukasz Bodziony<\/p>\n<p><strong>Website:<\/strong> <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a><\/p>\n<p><em>\u0141ukasz Bodziony is the CEO and founder of <a href=\"https:\/\/netcloud24.com\" target=\"_blank\" rel=\"dofollow\">NETCLOUD24<\/a>, a global VPS hosting brand proudly originating from Poland. With extensive experience in cloud computing, virtualization, and server management, he delivers high-performance <strong>Windows VPS<\/strong> and <strong>Remote Desktop Services (RDS)<\/strong> solutions to clients across Europe, North America, and beyond.<\/em><\/p>\n<p><em>His expertise covers a wide range of technologies, including <strong>Microsoft Azure<\/strong>, <strong>Proxmox VE<\/strong>, <strong>Amazon Web Services (AWS)<\/strong>, and numerous other virtualization and cloud platforms.<\/em><\/p>\n<p><em>Beyond running his hosting business, \u0141ukasz also provides <strong>professional paid server configuration and optimization services<\/strong> for companies and individuals. Outside of work, he is dedicated to caring for his children and building a secure future for them.<\/em><\/p>\n<p><em>If you are interested in working with him or need expert assistance with your hosting, cloud environment, or server setup, feel free to reach out via <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a>.<\/em><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 Introduction Prometheus is a powerful monitoring system and time series database, and Grafana is a popular analytics and monitoring solution. This guide will show you how\u2026<\/p>\n","protected":false},"author":1,"featured_media":3421,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[],"tags":[14,12,11,23,20,21,22,17,7,8,6,10,18,19,15,24,16,5,13,9],"class_list":["post-3312","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","tag-cheapvps","tag-cloudvps","tag-hostingvps","tag-rds","tag-rdscal","tag-remotedesktop","tag-remotedesktopvps","tag-servervps","tag-ukvps","tag-virtualserver","tag-vpshosting","tag-vpsserver","tag-vpssolutions","tag-vpswindows","tag-vpswithwindows","tag-windowsrds","tag-windowsserver","tag-windowsvps","tag-windowsvpshosting","tag-windowsvpsuk"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/3312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=3312"}],"version-history":[{"count":0,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/3312\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media\/3421"}],"wp:attachment":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=3312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=3312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=3312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}