{"id":238,"date":"2018-12-11T14:09:00","date_gmt":"2018-12-11T14:09:00","guid":{"rendered":""},"modified":"2025-11-24T15:58:05","modified_gmt":"2025-11-24T10:28:05","slug":"setting-up-mysql-monitoring-with-prometheus","status":"publish","type":"post","link":"https:\/\/opstree.com\/blog\/2018\/12\/11\/setting-up-mysql-monitoring-with-prometheus\/","title":{"rendered":"Setting up MySQL Monitoring with Prometheus"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">One thing that I love about Prometheus is that it has\u00a0a multitude of Integration with different services, both officially supported and the third party supported.<br \/>\nLet&#8217;s see how can we monitor MySQL with Prometheus.Those who are the starter or new to Prometheus can refer to our this blog.MySQL is a popular opensource relational database system, which exposed a large number of metrics for monitoring but not in Prometheus format. For capturing that data in Prometheus format we use\u00a0<b><a href=\"https:\/\/github.com\/prometheus\/mysqld_exporter\" target=\"_blank\" rel=\"noopener\">mysqld_exporter<\/a>.<\/b><br \/>\n<b><br \/>\n<\/b> I am assuming that you have already setup MySQL Server.<\/p>\n<h3 style=\"text-align: left;\">Configuration changes in MySQL<\/h3>\n<div>For setting up MySQL monitoring, we need a user with reading access on all databases which we can achieve by an existing user also but the good practice is that we should always create a new user in the database\u00a0for new service.<\/div>\n<div><\/div>\n<div>\n<pre><span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">CREATE<\/span> <span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">USER<\/span> '<span class=\"pl-en\" style=\"box-sizing: border-box; color: #6f42c1;\">mysqld_exporter<\/span>'@<span class=\"pl-s\" style=\"box-sizing: border-box; color: #032f62;\"><span class=\"pl-pds\" style=\"box-sizing: border-box;\">'<\/span>localhost<span class=\"pl-pds\" style=\"box-sizing: border-box;\">'<\/span><\/span> IDENTIFIED BY <span class=\"pl-s\" style=\"box-sizing: border-box; color: #032f62;\"><span class=\"pl-pds\" style=\"box-sizing: border-box;\">'password<\/span><span class=\"pl-pds\" style=\"box-sizing: border-box;\">'<\/span><\/span> WITH MAX_USER_CONNECTIONS <span class=\"pl-c1\" style=\"box-sizing: border-box; color: #005cc5;\">3<\/span>;<\/pre>\n<\/div>\n<div><\/div>\n<div>After\u00a0creating a user we simply have to provide permission to that user.<\/div>\n<div>\n<pre><span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">GRANT<\/span> PROCESS, REPLICATION CLIENT, <span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">SELECT<\/span> <span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">ON<\/span> <span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">*<\/span>.<span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">*<\/span> TO <span class=\"pl-s\" style=\"box-sizing: border-box; color: #032f62;\"><span class=\"pl-pds\" style=\"box-sizing: border-box;\">'<\/span>mysqld_exporter<span class=\"pl-pds\" style=\"box-sizing: border-box;\">'<\/span><\/span>@<span class=\"pl-s\" style=\"box-sizing: border-box; color: #032f62;\"><span class=\"pl-pds\" style=\"box-sizing: border-box;\">'<\/span>localhost<span class=\"pl-pds\" style=\"box-sizing: border-box;\">'<\/span><\/span>;<\/pre>\n<\/div>\n<div><\/div>\n<h3 style=\"text-align: left;\">Setting up MySQL Exporter<\/h3>\n<div>Download the\u00a0<b><a href=\"https:\/\/github.com\/prometheus\/mysqld_exporter\" target=\"_blank\" rel=\"noopener\">mysqld_exporter<\/a>\u00a0<\/b>from GitHub. We are downloading the 0.11.0 version as per latest release now, change the version in future if you want to download the latest version.<\/div>\n<div><b>\u00a0<\/b><\/div>\n<div>\n<pre><span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">wget <\/span>https:\/\/github.com\/prometheus\/mysqld_exporter\/releases\/download\/v0.11.0\/mysqld_exporter-0.11.0.linux-amd64.tar.gz<\/pre>\n<\/div>\n<div><b>\u00a0<\/b><\/div>\n<div>Then simply extract the tar file and move the binary file at the appropriate location.<\/div>\n<div>\n<pre><span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">tar -xvf <\/span>mysqld_exporter-0.11.0.linux-amd64.tar.gz<\/pre>\n<\/div>\n<div><\/div>\n<div>\n<pre><span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">mv <\/span>mysqld_exporter \/usr\/bin\/<\/pre>\n<\/div>\n<div><\/div>\n<div>\n<p>Although we can execute the binary simply,\u00a0but the best practice is to create service for every Third Party binary application. Also, we are assuming that <b>systemd\u00a0<\/b>is already installed in your system. If you are using init then you have to create init service for the exporter.<\/p>\n<\/div>\n<div>\n<pre><span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">useradd <\/span>mysqld_exporter\r\n<span style=\"color: #d73a49; font-size: 13.6px;\">vim<\/span> <span style=\"font-size: 13.6px;\">\/etc\/systemd\/system\/mysqld_exporter.service<\/span><\/pre>\n<\/div>\n<div><\/div>\n<div>\n<pre style=\"background-color: #f6f8fa; border-radius: 3px; box-sizing: border-box; line-height: 1.45; overflow-wrap: normal; overflow: auto; padding: 16px; word-break: normal;\"><span style=\"font-size: 13.6px;\"><span style=\"color: #cc0000;\">[Unit]<\/span><span style=\"color: #24292e;\">\r\nDescription=MySQL Exporter Service\r\nWants=network.target\r\nAfter=network.target\r\n\r\n<\/span><span style=\"color: #cc0000;\">[Service]<\/span>\r\n<\/span><span style=\"font-size: 13.6px;\">User=mysqld_exporter\r\nGroup=mysqld_exporter\r\nEnvironment=\"DATA_SOURCE_NAME=mysqld_exporter:password@unix(\/var\/run\/mysqd\/mysqld.sock)\"\r\nType=simple\r\nExecStart=\/usr\/bin\/mysqld_exporter\r\nRestart=always<\/span>\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n<\/pre>\n<\/div>\n<div><\/div>\n<div>You may need to adjust the socket location of Unix according to your environment<\/div>\n<div><\/div>\n<div>If you go and visit the <a href=\"#\">http:\/\/localhost.com:9104\/metrics<\/a>, you will be able to see them.<\/div>\n<div><\/div>\n<h3 style=\"text-align: left;\">Prometheus Configurations<\/h3>\n<div>For scrapping metrics from mysqld_exporter in Prometheus we have to make some configuration changes in Prometheus, the changes are not fancy, we just have to add another job for mysqld_exporter, like this:-<\/div>\n<div><\/div>\n<div><\/div>\n<div>\n<pre><span class=\"pl-k\" style=\"box-sizing: border-box; color: #d73a49;\">vim <\/span>\/etc\/prometheus\/prometheus.yml<\/pre>\n<\/div>\n<div><\/div>\n<div>\n<pre style=\"background-color: #f6f8fa; border-radius: 3px; box-sizing: border-box; line-height: 1.45; overflow-wrap: normal; overflow: auto; padding: 16px; word-break: normal;\"><span style=\"font-size: 13.6px;\">  - job_name: 'mysqld_exporter'\r\n    static_configs:\r\n      - targets:\r\n          - :9104<\/span><\/pre>\n<\/div>\n<div><\/div>\n<div>\n<p>After the configuration changes, we just have to restart the Prometheus server.<\/p>\n<pre style=\"background-color: #f6f8fa; border-radius: 3px; box-sizing: border-box; font-size: 13.6px; line-height: 1.45; overflow-wrap: normal; overflow: auto; padding: 16px; word-break: normal;\"><span style=\"color: #d73a49;\">systemctl restart prometheus<\/span><\/pre>\n<p>Then, if you go to the Prometheus server you can find the MySQL\u00a0metrics there like this:-<\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2018\/12\/4f9f6-mysql.png\"><img loading=\"lazy\" decoding=\"async\" style=\"cursor: move;\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2018\/12\/4f9f6-mysql.png?w=300\" width=\"640\" height=\"356\" border=\"0\" \/><\/a><\/div>\n<\/div>\n<div>So In this blog, we have covered MySQL configuration changes for Prometheus, mysqld_exporter setup and Prometheus configuration changes.<\/div>\n<div>In the next part, we will discuss how to create a visually impressive dashboard in Grafana for better visualization of MySQL metrics. See you soon&#8230; \ud83d\ude42<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>One thing that I love about Prometheus is that it has\u00a0a multitude of Integration with different services, both officially supported and the third party supported. Let&#8217;s see how can we monitor MySQL with Prometheus.Those who are the starter or new to Prometheus can refer to our this blog.MySQL is a popular opensource relational database system, &hellip; <a href=\"https:\/\/opstree.com\/blog\/2018\/12\/11\/setting-up-mysql-monitoring-with-prometheus\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setting up MySQL Monitoring with Prometheus&#8221;<\/span><\/a><\/p>\n","protected":false},"author":89038429,"featured_media":29900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[723407,768739293,768739289,768739287],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/11\/DevSecOps-1.jpg","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfDBOm-3Q","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/238"}],"collection":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/users\/89038429"}],"replies":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":5,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":30052,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/238\/revisions\/30052"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media\/29900"}],"wp:attachment":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}