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.
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:
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
2. Create a Data Directory for Prometheus
3. Create Configuration Directories for Prometheus
4. Download the Prometheus Archive and Extract the File
To download the latest binary archive for Prometheus:
To extract the file:
5. Move the Prometheus Files to Standard Locations
Move Prometheus binary files to /usr/local/bin:
Move Prometheus configuration template to /etc directory:
Also move consoles and console_libraries to /etc/prometheus directory:
6. Create/Edit a Prometheus Configuration File
The template configurations should look similar to below:
7. Create a Prometheus systemd Service Unit File
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:
9. Reload systemd Daemon and Start the Service
Check status using systemctl status prometheus command:
10. Configure a Firewall to Open Port 9090
Once the setup is complete, you can access the Prometheus UI by logging in to http://<PrometheusHostIP>:9090
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
2. Create Prometheus Exporter Database User to Access the Database, Scrape Metrics, and Provide Grants
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:
Add the username and password of the user-created and the ScaleGrid MySQL server you want to monitor:
Set ownership permissions:
4. Create systemd Unit File
Create a new service file:
Add the following content:
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:
5. Configure MySQL Endpoint to be Scraped by Prometheus
Make changes like below to the prometheus.yml file:
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:
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.
2. Download APT Repository and Install Grafana
The command add-apt-repository isn’t a default app on Debian 9 and requires:
Install the repository for stable releases:
There is a separate repository if you want beta releases:
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:
Update your Apt repositories and install Grafana:
3. Start the Service
Enable the systemd service so that Grafana starts at boot:
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.
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.
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.
Published at DZone with permission of Swaroop Jagadeesh. See the original article here.
Opinions expressed by DZone contributors are their own.