DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Keep Your Application Secrets Secret
  • Building an Enterprise CDC Solution
  • Monitor Apache Kafka with Telegraf and Grafana in Azure
  • Grafana and Prometheus Setup With Strimzi, a.k.a. Kafka on Kubernetes.

Trending

  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • Advancing Your Software Engineering Career in 2025
  1. DZone
  2. Coding
  3. Languages
  4. How to Monitor MySQL Deployments With Prometheus and Grafana at ScaleGrid

How to Monitor MySQL Deployments With Prometheus and Grafana at ScaleGrid

In this article, see how to monitor MySQL deployments with Prometheus and Grafana at ScaleGrid.

By 
Swaroop Jagadeesh user avatar
Swaroop Jagadeesh
·
Updated Jul. 06, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
23.6K Views

Join the DZone community and get the full member experience.

Join For Free

Monitoring your MySQL database performance in real-time helps you immediately identify problems and other factors that could be causing issues now or in the future. It's also a good way to determine which components of the database can be enhanced or optimized to increase your efficiency and performance. This is usually done through monitoring software and tools either built-in to the database management software or installed from third-party providers.

Prometheus is an open-source software application used for event monitoring and alerting. It can be used along with a visualization tool like Grafana to easily create and edit dashboards, query, visualize, alert on, and understand your metrics. ScaleGrid provides full admin access to your MySQL deployments — this makes it easier to integrate the existing MySQL ecosystem of tools with your ScaleGrid MySQL deployments on AWS or Azure. Prometheus works well for recording any purely numeric time series, and also offers support for multi-dimensional data collection and querying. Grafana can be used with it to build dashboards that help visualize this data in a way that is easy to interpret and utilize.

These tools will provide additional insight into your metrics, usage patterns, and datasets along with your ScaleGrid MySQL monitoring, query analysis, and alerts. In this post, we discuss how you can set up and use Prometheus and Grafana with your ScaleGrid MySQL deployments for advanced database monitoring and alerting.

You might also be interested in:   Deploying and Running MySQL on Kubernetes With Statefulsets and Kops

How to Set Up Your MySQL Monitoring

Let’s walk through the steps involved in installing and configuring the Prometheus server to store and display the metrics, an exporter (MySQL Exporter in this case) to collect the metrics and relay them to the Prometheus server, and Grafana to create dashboards. The MySQL Exporter tool can be installed locally on a MySQL server or centrally on the Prometheus server. In the use case below, we will explain how to set up and start using Prometheus, MySQL Exporter, and Grafana from a central host running on Ubuntu to monitor multiple MySQL servers. You can also review step-by-step instructions in our Prometheus and Grafana for MySQL help doc.

The block diagram below shows the setup of a master-slave-quorum MySQL deployment that includes two data-bearing nodes (master and slave) and one voting member (quorum) using the MySQL Exporter, Prometheus host, and Grafana:

Prometheus block diagram - host for MySQL master slave quorum deployment with Grafana

Installing and Configuring the Prometheus Server

Prometheus is the tool we will be using to centralize and store your MySQL metrics. It scrapes the metrics from one or several exporters at regular intervals and displays it on its UI. Below are the steps to install and configure Prometheus on a central Ubuntu host. For more details, you can refer to this article.

1. Create a Prometheus System Group and User

Shell
 




xxxxxxxxxx
1


 
1
$sudo groupadd --system prometheus
2
$sudo useradd -s /sbin/nologin --system -g prometheus prometheus



2. Create a Data Directory for Prometheus

Shell
 




xxxxxxxxxx
1


 
1
$sudo mkdir /var/lib/prometheus



3. Create Configuration Directories for Prometheus

Shell
 




xxxxxxxxxx
1


 
1
$for i in rules rules.d files_sd; do sudo mkdir -p /etc/prometheus/${i}; done



4. Download the Prometheus Archive and Extract the File

To download the latest binary archive for Prometheus:

Shell
 


x
 
1
$mkdir -p /tmp/prometheus && cd /tmp/prometheus
2
$curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest \
3
| grep browser_download_url \
4
| grep linux-amd64 \
5
| cut -d '"' -f 4 \
6
| wget -qi -


To extract the file:

Shell
 




xxxxxxxxxx
1


 
1
$tar xvf prometheus*.tar.gz
2
$cd prometheus*/



5. Move the Prometheus Files to Standard Locations

Move Prometheus binary files to /usr/local/bin:

Shell
 




xxxxxxxxxx
1


 
1
$sudo mv prometheus promtool /usr/local/bin/



Move Prometheus configuration template to /etc directory:

Shell
 




xxxxxxxxxx
1


 
1
$sudo mv prometheus.yml  /etc/prometheus/prometheus.yml



Also move consoles and console_libraries to /etc/prometheus directory:

Shell
 




xxxxxxxxxx
1


 
1
$sudo mv consoles/ console_libraries/ /etc/prometheus/



6. Create/Edit a Prometheus Configuration File

Shell
 




xxxxxxxxxx
1


 
1
$sudo vim /etc/prometheus/prometheus.yml



The template configurations should look similar to below:

Plain Text
 




xxxxxxxxxx
1
29


 
1
 #my global config
2
 global:
3
 scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
4
 evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
5
 #scrape_timeout is set to the global default (10s).
6

          
7
 #Alertmanager configuration
8
 alerting:
9
 alertmanagers:
10
 - static_configs:
11
 - targets:
12
 #- alertmanager:9093
13

          
14
 #Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
15
 rule_files:
16
 #- "first_rules.yml"
17
 #- "second_rules.yml"
18

          
19
 #A scrape configuration containing exactly one endpoint to scrape:
20
 #Here it's Prometheus itself.
21
 scrape_configs:
22
 #The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
23
 - job_name: 'prometheus'
24

          
25
 #metrics_path defaults to '/metrics'
26
 #scheme defaults to 'http'.
27

          
28
 static_configs:
29
 - targets: ['localhost:9090']



7. Create a Prometheus systemd Service Unit File

Shell
 




xxxxxxxxxx
1
27


 
1
$cat /etc/systemd/system/prometheus.service 
2

          
3
 [Unit]
4
 Description=Prometheus
5
 Documentation=https://prometheus.io/docs/introduction/overview/
6
 Wants=network-online.target
7
 After=network-online.target
8

          
9
 [Service]
10
 Type=simple
11
 Environment="GOMAXPROCS=1"
12
 User=prometheus
13
 Group=prometheus
14
 ExecReload=/bin/kill -HUP $MAINPID
15
 ExecStart=/usr/local/bin/prometheus \
16
 --config.file=/etc/prometheus/prometheus.yml \
17
 --storage.tsdb.path=/var/lib/prometheus \
18
 --web.console.templates=/etc/prometheus/consoles \
19
 --web.console.libraries=/etc/prometheus/console_libraries \
20
 --web.listen-address=0.0.0.0:9090 \
21
 --web.external-url=
22

          
23
 SyslogIdentifier=prometheus
24
 Restart=always
25

          
26
 [Install]
27
 WantedBy=multi-user.target



Remember to edit the line: Environment="GOMAXPROCS=1 by replacing 1 with the number of **vcpus** on your server.

8. Change Directory Permissions

Change the ownership of these directories to Prometheus user and group:

Shell
 




xxxxxxxxxx
1


 
1
$for i in rules rules.d files_sd; do sudo chown -R prometheus:prometheus /etc/prometheus/${i}; done
2
$for i in rules rules.d files_sd; do sudo chmod -R 775 /etc/prometheus/${i}; done
3
$sudo chown -R prometheus:prometheus /var/lib/prometheus/



9. Reload systemd Daemon and Start the Service

Shell
 




xxxxxxxxxx
1


 
1
$sudo systemctl daemon-reload
2
$sudo systemctl start prometheus
3
$sudo systemctl enable prometheus



Check status using systemctl status prometheus command:

Installing & Configuring the Prometheus Server for MySQL - Reload systemd Daemon & Start the Service


10. Configure a Firewall to Open Port 9090

Shell
 




xxxxxxxxxx
1


 
1
$sudo firewall-cmd --add-port=9090/tcp --permanent
2
$sudo firewall-cmd --reload



Once the setup is complete, you can access the Prometheus UI by logging in to http://<PrometheusHostIP>:9090

Installing & Configuring the Prometheus Server for MySQL - Access Prometheus UI

Installing and Configuring MySQL Prometheus Exporter

Prometheus requires an exporter for collecting MySQL server metrics. This exporter can be run centrally on the Prometheus server, or on the database server. For further reading, refer to the Prometheus documentation.

Follow the below steps to install and set up MySQL Prometheus Exporter on the central Prometheus host. For more details, refer to this article.

1. Download and Install Prometheus MySQL Exporter

Shell
 




xxxxxxxxxx
1


 
1
$curl -s https://api.github.com/repos/prometheus/mysqld_exporter/releases/latest   | grep browser_download_url   | grep linux-amd64 | cut -d '"' -f 4   | wget -qi -
2
$tar xvf mysqld_exporter*.tar.gz
3
$sudo mv  mysqld_exporter-*.linux-amd64/mysqld_exporter /usr/local/bin/
4
$sudo chmod +x /usr/local/bin/mysqld_exporter



2. Create Prometheus Exporter Database User to Access the Database, Scrape Metrics, and  Provide Grants

SQL
 




xxxxxxxxxx
1


 
1
CREATE USER 'mysqld_exporter'@'<PrometheusHostIP>' IDENTIFIED BY 'StrongPassword' WITH MAX_USER_CONNECTIONS 2;
2
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'<PrometheusHostIP>';
3
FLUSH PRIVILEGES;
4
EXIT



WITH MAX_USER_CONNECTIONS 2 is used to set a max connection limit for the user to avoid overloading the server with monitoring scrapes under heavy load.

3. Configure the Database Credentials

Edit the config file of the exporter:

Shell
 




xxxxxxxxxx
1


 
1
$sudo vim /etc/.mysqld_exporter.cnf



Add the username and password of the user-created and the ScaleGrid MySQL server you want to monitor:

Shell
 




xxxxxxxxxx
1


 
1
$sudo vim /etc/.mysqld_exporter.cnf
2

          
3
[client]
4
user=mysqld_exporter
5
password=StrongPassword
6
host=SG-mysqltestcluster-123456.servers.mongodirector.com



Set ownership permissions:

Shell
 




xxxxxxxxxx
1


 
1
$sudo chown root:prometheus /etc/.mysqld_exporter.cnf



4. Create systemd Unit File

Create a new service file:

Shell
 




xxxxxxxxxx
1


 
1
$sudo vim /etc/systemd/system/mysql_exporter.service



Add the following content:

Properties files
 




xxxxxxxxxx
1
31


 
1
 [Unit]
2
 Description=Prometheus MySQL Exporter
3
 After=network.target
4
 User=prometheus
5
 Group=prometheus
6

          
7
 [Service]
8
 Type=simple
9
 Restart=always
10
 ExecStart=/usr/local/bin/mysqld_exporter \
11
 --config.my-cnf /etc/.mysqld_exporter.cnf \
12
 --collect.global_status \
13
 --collect.info_schema.innodb_metrics \
14
 --collect.auto_increment.columns \
15
 --collect.info_schema.processlist \
16
 --collect.binlog_size \
17
 --collect.info_schema.tablestats \
18
 --collect.global_variables \
19
 --collect.info_schema.query_response_time \
20
 --collect.info_schema.userstats \
21
 --collect.info_schema.tables \
22
 --collect.perf_schema.tablelocks \
23
 --collect.perf_schema.file_events \
24
 --collect.perf_schema.eventswaits \
25
 --collect.perf_schema.indexiowaits \
26
 --collect.perf_schema.tableiowaits \
27
 --collect.slave_status \
28
 --web.listen-address=0.0.0.0:9104
29
 
30
 [Install]
31
 WantedBy=multi-user.target



web.listen-address=0.0.0.0:9104 specifies that the server is listening on port 9104. If your server has a public and private network, you may need to replace 0.0.0.0:9104 with private IP, for example – 192.168.4.5:9104.

When done, reload systemd and start mysql_exporter service:

Shell
 




xxxxxxxxxx
1


 
1
$sudo systemctl daemon-reload
2
$sudo systemctl enable mysql_exporter
3
$sudo systemctl start mysql_exporter



5. Configure MySQL Endpoint to be Scraped by Prometheus

Make changes like below to the prometheus.yml file:

Java
 




xxxxxxxxxx
1


 
1
scrape_configs:
2
- job_name: mysql_server1
3
static_configs:
4
- targets: ['localhost:9104']
5
labels:
6
alias: db1


Note: If the exporter is not running on the same host as Prometheus, provide the IP address of the server instead of localhost. 9104 refers to the port Prometheus listens to, as specified in the previous step.

Monitoring Multiple MySQL Hosts From a Central Prometheus Host

Multiple MySQL servers can be monitored from a central server. This can be achieved by having a separate exporter service for each server. Make sure to create .mysqld_exporter.cnf and mysql_exporter.service (with unique port numbers assigned to the --web.listen-address flag) files for each service as mentioned in steps 3 and 4 above. Add targets to the prometheus.yml file as mentioned in step 5 above. Job names should be unique for each target. For example:

Java
 




xxxxxxxxxx
1
16


 
1
scrape_configs:
2
- job_name: mysql_server1
3
static_configs:
4
- targets: ['localhost:9104']
5
labels:
6
alias: db1
7
- job_name: mysql_server2
8
static_configs:
9
- targets: ['localhost:9105']
10
labels:
11
alias: db2
12
- job_name: mysql_server3
13
static_configs:
14
- targets: ['localhost:9106']
15
labels:
16
alias: db3


Note: Prometheus Server should be able to reach the targets over the network. Ensure that your network/firewall configurations have been modified accordingly.

Installing Grafana and Creating Dashboards

Grafana uses Prometheus as a data source, allowing you to create dashboards to better visualize and understand your metrics. It provides a great way to gain insight into your time series data.

Follow the below steps to install Grafana on your central Prometheus host.

1. Download the Latest Grafana Version

Go to the Download Grafana page to download the latest version.

Shell
 




xxxxxxxxxx
1


 
1
$wget <debian package url>
2
$sudo apt-get install -y adduser libfontconfig1
3
$sudo dpkg -i grafana_<version>_amd64.deb



2. Download APT Repository and Install Grafana

The command add-apt-repository isn’t a default app on Debian 9 and requires:

Shell
 




xxxxxxxxxx
1


 
1
$apt-get install -y software-properties-common



Install the repository for stable releases:

Shell
 




xxxxxxxxxx
1


 
1
$sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"



There is a separate repository if you want beta releases:

Shell
 




xxxxxxxxxx
1


 
1
$sudo add-apt-repository "deb https://packages.grafana.com/oss/deb beta main"



Use the above line even if you are on Ubuntu or another Debian version. Then add our gpg key. This allows you to install signed packages:

Shell
 




xxxxxxxxxx
1


 
1
$wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -



Update your Apt repositories and install Grafana:

Shell
 




xxxxxxxxxx
1


 
1
$sudo apt-get update
2
$sudo apt-get install grafana



3. Start the Service

Shell
 




xxxxxxxxxx
1


 
1
$systemctl daemon-reload
2
$systemctl start grafana-server
3
$systemctl status grafana-server



Enable the systemd service so that Grafana starts at boot:

Shell
 




xxxxxxxxxx
1


 
1
$sudo systemctl enable grafana-server.service



To run Grafana, open your browser and go to *http://<PrometheusHostIP>:3000/*. 3000 is the http port that Grafana listens to, by default.

4. Adding a Data Source

When installed, login to the admin dashboard and add a data source by navigating to Configuration > Data Sources.

Name: Prometheus Type: Prometheus URL: http://localhost:9090

Note: If Prometheus server is not running on the same host as Grafana, provide the IP address of the server instead of localhost.

Installing Grafana & Creating Dashboards for MySQL - Add a Data Source


You are now all set to create and customize dashboards for your MySQL monitoring. You can create a new dashboard by clicking on the link on the right side of the dashboard picker. Once the dashboard is created, you can add panels choosing the metrics to be displayed, star the dashboard, save, and share it. For detailed instructions, you can refer to Grafana's Getting Started documentation.

Here’s an example of a Grafana dashboard created for a MySQL deployment at ScaleGrid: Grafana MySQL Dashboard - MySQL Table Locks, Temporary Objects, Sorts and Select Types


The above Grafana dashboard displays MySQL Table Locks, MySQL Temporary Objects, MySQL Sorts, and MySQL Select Types metrics visualized in the charts, and the below Grafana dashboard displays MySQL Basic Command Counters and MySQL Top Command Counters Hourly.

Grafana MySQL Dashboard - MySQL Basic Command Counters and Top Command Counters Hourly


Further Reading

Deploying a Spring Boot App With MySQL on OpenShift

Measuring MySQL Performance in Kubernetes

MySQL Grafana shell Monitor (synchronization)

Published at DZone with permission of Swaroop Jagadeesh. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Keep Your Application Secrets Secret
  • Building an Enterprise CDC Solution
  • Monitor Apache Kafka with Telegraf and Grafana in Azure
  • Grafana and Prometheus Setup With Strimzi, a.k.a. Kafka on Kubernetes.

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!