Connect a self-hosted or an AWS EC2 MySQL database to PMM¶
PMM Client collects metrics from MySQL, Percona Server for MySQL, Percona XtraDB Cluster, and MariaDB. (Amazon RDS is also supported and explained in a separate section.)
Summary
Before you start¶
Check that:
- PMM Server is installed and running with a known IP address accessible from the client node.
- PMM Client is installed and the nodes are registered with PMM Server.
- You have superuser (root) access on the client host.
Create a database account for PMM¶
It is good practice to use a non-superuser account to connect PMM Client to the monitored database instance. This example creates a database user with name pmm
, password pass
, and the necessary permissions.
CREATE USER 'pmm'@'127.0.0.1' IDENTIFIED BY 'pass' WITH MAX_USER_CONNECTIONS 10;
GRANT SELECT, PROCESS, REPLICATION CLIENT, RELOAD ON *.* TO 'pmm'@'localhost';
CREATE USER 'pmm'@'localhost' IDENTIFIED BY 'pass' WITH MAX_USER_CONNECTIONS 10;
GRANT SELECT, PROCESS, REPLICATION CLIENT, RELOAD, BACKUP_ADMIN ON *.* TO 'pmm'@'localhost';
Choose and configure a source¶
Decide which source of metrics to use, and configure your database server for it. The choices are Slow query log and Performance Schema.
While you can use both at the same time we recommend using only one–there is some overlap in the data reported, and each incurs a small performance penalty. The choice depends on the version and variant of your MySQL instance, and how much detail you want to see.
Here are the benefits and drawbacks of Slow query log and Performance Schema metrics sources.
Benefits | Drawbacks | |
---|---|---|
Slow query log | 1. More detail. 2. Lower resource impact (with query sampling feature in Percona Server for MySQL). |
1. PMM Client must be on same host as database server or have access to slow query log. 2. Log files grow and must be actively managed. |
Performance Schema | 1. Faster parsing. 2. Enabled by default on newer versions of MySQL. |
1. Less detail. |
Data source recommendations¶
Database server | Versions | Recommended source |
---|---|---|
MySQL | 5.1-5.5 | Slow query log |
MySQL | 5.6+ | Performance Schema |
MariaDB | 10.0+ | Performance Schema |
Percona Server for MySQL | 5.7, 8.0 | Slow query log |
Percona XtraDB Cluster | 5.6, 5.7, 8.0 | Slow query log |
Slow query log¶
This section covers how to configure a MySQL-based database server to use the slow query log as a source of metrics.
Applicable versions¶
Server | Versions |
---|---|
MySQL | 5.1-5.5 |
MariaDB | 10.1.2+ |
Percona Server for MySQL | 5.7.10+, 8.0.12+ |
Percona XtraDB Cluster | 5.6, 5.7, 8.0 |
The slow query log records the details of queries that take more than a certain amount of time to complete. With the database server configured to write this information to a file rather than a table, PMM Client parses the file and sends aggregated data to PMM Server via the Query Analytics part of pmm-agent.
Settings¶
Variable | Value | Description |
---|---|---|
slow_query_log |
ON | Enables the slow query log. |
log_output |
'FILE' |
Ensures the log is sent to a file. (This is the default on MariaDB.) |
long_query_time |
0 | The slow query threshold in seconds. In heavily-loaded applications, many quick queries can affect performance more than a few slow ones. Setting this value to 0 ensures all queries are captured. |
log_slow_admin_statements |
ON | Includes the logging of slow administrative statements. |
log_slow_slave_statements |
ON | Enables logging for queries that have taken more than long_query_time seconds to execute on the replica. |
Examples
-
Configuration file.
slow_query_log=ON log_output=FILE long_query_time=0 log_slow_admin_statements=ON log_slow_slave_statements=ON
-
Session.
SET GLOBAL slow_query_log = 1; SET GLOBAL log_output = 'FILE'; SET GLOBAL long_query_time = 0; SET GLOBAL log_slow_admin_statements = 1; SET GLOBAL log_slow_slave_statements = 1;
Slow query log – extended¶
Some MySQL-based database servers support extended slow query log variables.
Applicable versions¶
Server | Versions |
---|---|
Percona Server for MySQL | 5.7.10+, 8.0.12+ |
Percona XtraDB Cluster | 5.6, 5.7, 8.0 |
MariaDB | 10.0 |
Settings¶
Variable | Value | Description |
---|---|---|
log_slow_rate_limit |
100 | Defines the rate of queries captured by the slow query log. A good rule of thumb is 100 queries logged per second. For example, if your Percona Server instance processes 10,000 queries per second, you should set log_slow_rate_limit to 100 and capture every 100th query for the slow query log. Depending on the amount of traffic, logging could become aggressive and resource consuming. This variable throttles the level of intensity of the data capture without compromising information. |
log_slow_rate_type |
‘query’ | Set so that it applies to queries, rather than sessions. |
slow_query_log_always_write_time |
1 | Specifies which queries should ignore sampling. With query sampling this ensures that queries with longer execution time will always be captured by the slow query log, avoiding the possibility that infrequent slow queries might not get captured at all. |
log_slow_verbosity |
‘full’ | Ensures that all information about each captured query is stored in the slow query log. |
slow_query_log_use_global_control |
‘all’ | Configure the slow query log during runtime and apply these settings to existing connections. (By default, slow query log settings apply only to new sessions.) |
Examples
-
Configuration file (Percona Server for MySQL, Percona XtraDB Cluster).
log_slow_rate_limit=100 log_slow_rate_type='query' slow_query_log_always_write_time=1 log_slow_verbosity='full' slow_query_log_use_global_control='all'
-
Configuration file (MariaDB).
log_slow_rate_limit=100
-
Session (Percona Server for MySQL, Percona XtraDB Cluster).
SET GLOBAL log_slow_rate_limit = 100; SET GLOBAL log_slow_rate_type = 'query'; SET GLOBAL slow_query_log_always_write_time = 1; SET GLOBAL log_slow_verbosity = 'full'; SET GLOBAL slow_query_log_use_global_control = 'all';
Slow query log rotation¶
Slow query log files can grow quickly and must be managed.
When adding a service with the command line use the pmm-admin
option --size-slow-logs
to set at what size the slow query log file is rotated. (The size is specified as a number with a suffix. See pmm-admin add mysql
.)
When the limit is reached, PMM Client will:
- remove the previous
.old
slow log file, - rename the current file by adding the suffix
.old
, - execute the MySQL command
FLUSH LOGS
.
Only one .old
file is kept. Older ones are deleted.
You can manage log rotation yourself, for example, with logrotate
. If you do, you can disable PMM Client’s log rotation by providing a negative value to --size-slow-logs
option when adding a service with pmm-admin add
.
Performance Schema¶
This section covers how to configure a MySQL-based database server to use Performance Schema as a source of metrics.
Applicable versions¶
Server | Versions |
---|---|
Percona Server for MySQL | 5.6, 5.7, 8.0 |
Percona XtraDB Cluster | 5.6, 5.7, 8.0 |
MariaDB | 10.3+ |
PMM’s MySQL Performance Schema Details dashboard charts the various performance_schema
metrics.
To use Performance Schema, set these variables.
Variable | Value | Description |
---|---|---|
performance_schema |
ON |
Enables Performance Schema metrics. This is the default in MySQL 5.6.6 and higher. |
performance-schema-instrument |
'statement/%=ON' |
Configures Performance Schema instruments. |
performance-schema-consumer-statements-digest |
ON |
Configures the statements-digest consumer. |
innodb_monitor_enable |
all | Enables InnoDB metrics counters. |
Examples
-
Configuration file.
performance_schema=ON performance-schema-instrument='statement/%=ON' performance-schema-consumer-statements-digest=ON innodb_monitor_enable=all
-
Session.
(
performance_schema
cannot be set in a session and must be set at server start-up.)UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%statements%'; SET GLOBAL innodb_monitor_enable = all;
MariaDB 10.5.7 or lower¶
There is no Explain or Example data shown by default in Query Analytics when monitoring MariaDB instances version 10.5.7 or lower. A workaround is to set this variable.
Variable | Value | Description |
---|---|---|
performance_schema.setup_instruments |
'statement/%' |
List of instrumented object classes. |
-
Session
UPDATE performance_schema.setup_instruments SET ENABLED = 'YES', TIMED = 'YES' WHERE NAME LIKE 'statement/%'; UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%statements%';
-
Transactions
MariaDB doesn’t implement queries history for transactions. All queries executed within a transaction won’t have query examples since PMM relies on the
performance_schema.events_statements_history
to grab the query example but that table won’t have any query executed as part of a transaction.This behavior is because MariaDB doesn’t implement these consumers:
events_transactions_current events_transactions_history events_transactions_history_long
Query response time¶
Query time distribution is a chart in the Details tab of Query Analytics showing the proportion of query time spent on various activities. It is enabled with the query_response_time_stats
variable and associated plugins.
Applicable versions¶
Server | Versions |
---|---|
Percona Server for MySQL | 5.7 (not Percona Server for MySQL 8.0.) |
MariaDB | 10.0.4 |
Set this variable to see query time distribution charts.
Variable | Value | Description |
---|---|---|
query_response_time_stats |
ON | Report query response time distributions. (Requires plugin installation. See below.) |
-
Configuration file.
query_response_time_stats=ON
You must also install the plugins.
-
Session
- Check that
/usr/lib/mysql/plugin/query_response_time.so
exists. - Install the plugins and activate.
INSTALL PLUGIN QUERY_RESPONSE_TIME_AUDIT SONAME 'query_response_time.so'; INSTALL PLUGIN QUERY_RESPONSE_TIME SONAME 'query_response_time.so'; SET GLOBAL query_response_time_stats = ON;
INSTALL PLUGIN QUERY_RESPONSE_TIME_AUDIT SONAME 'query_response_time.so'; INSTALL PLUGIN QUERY_RESPONSE_TIME SONAME 'query_response_time.so'; INSTALL PLUGIN QUERY_RESPONSE_TIME_READ SONAME 'query_response_time.so'; INSTALL PLUGIN QUERY_RESPONSE_TIME_WRITE SONAME 'query_response_time.so'; SET GLOBAL query_response_time_stats = ON;
- Check that
Tablestats¶
Some table metrics are automatically disabled when the number of tables exceeds a default limit of 1000 tables. This prevents PMM Client from affecting the performance of your database server.
The limit can be changed when adding a service on the command line with the two pmm-admin
options:
pmm-admin option |
Description |
---|---|
--disable-tablestats |
Disables tablestats collection when the default limit is reached. |
--disable-tablestats-limit=N |
Sets the number of tables (N ) for which tablestats collection is disabled. 0 means no limit. A negative number means tablestats is completely disabled (for any number of tables). |
User statistics¶
Applicable versions¶
User activity, individual table and index access details are shown on the MySQL User Details dashboard when the userstat
variable is set.
Server | Versions |
---|---|
Percona Server for MySQL | 5.6, 5.7, 8.0 |
Percona XtraDB Cluster | 5.6, 5.7, 8.0 |
MariaDB | 5.2.0+ |
Examples
-
Configuration file.
userstat=ON
-
Session.
SET GLOBAL userstat = ON;
Add service¶
There are two ways to install PMM Client for monitoring your MySQL database:
- Local installation: Installs PMM Client directly on the database node, collecting both database and OS/host metrics. This option enables more effective comparison and problem identification.
- Remote instance: Use when local installation isn’t possible. This method doesn’t provide OS/Node metrics in PMM.
Install PMM Client locally¶
Add the MySQL server as a service using one of the following example commands. Upon successful addition, PMM Client will display “MySQL Service added” along with the service’s ID and name.
-
Select PMM Configuration > PMM Inventory > Add Service > MySQL.
-
Enter or select values for the fields.
-
Click Add service.
If your MySQL instance is configured to use TLS, click on the Use TLS for database connections check box and fill in your TLS certificates and key.
On the command line¶
Add the database server as a service using one of these example commands. If successful, PMM Client will print MySQL Service added
with the service’s ID and name. Use the --environment
and -custom-labels
options to set tags for the service to help identify them.
Examples
TLS connection¶
pmm-admin add mysql --environment=test --custom-labels='source=slowlog' --username=root --password=password --query-source=slowlog MySQLSlowLog localhost:3306
Install PMM Client as a remote instance¶
-
Select ** PMM Configuration > PMM Inventory > Add Service**.
-
Choose MySQL > Add a remote instance.
-
Complete the required fields.
-
Click Add service.
For MySQL instances using TLS¶
If your MySQL instance is configured to use TLS:
- Click on the Use TLS for database connections check box.
- Fill in your TLS certificates and key.
Check the service¶
PMM user interface¶
To check the service with the UI:
- Select PMM Configuration > PMM Inventory.
- In the Services tab, verify the Service name, Addresses, and any other relevant information in the form.
- In the Options column, expand the Details section and check that the Agents are using the desired data source.
Command line¶
Look for your service in the output of this command.
pmm-admin inventory list services --service-type=mysql
Check data¶
To check the data:
- Open the MySQL Instance Summary dashboard.
- Set the Service Name to the newly-added service.
Percona Server for MySQL, MariaDB¶
If query response time plugin was installed, check for data in the MySQL Query Response Time Details dashboard or select a query in PMM Query Analytics to see the Query time distribution bar.
Percona XtraDB Cluster¶
Open the PXC/Galera Cluster Summary dashboard.
See also
- Percona Server for MySQL – Slow Query Log Extended
- Percona Server for MySQL – User Statistics
- MariaDB – Slow Query Log Overview
- MariaDB – Slow Query Log Extended Statistics
- MariaDB – User Statistics
- Percona Blog – PERFORMANCE_SCHEMA vs Slow Query Log
- Percona Blog – MySQL’s INNODB_METRICS table
- Percona Blog – Rotating MySQL Slow Logs Safely
- Percona Blog – Impact of logging on MySQL’s performance
- Percona Blog – Running Custom MySQL Queries in Percona Monitoring and Management
Get expert help¶
If you need assistance, visit the community forum for comprehensive and free database knowledge, or contact our Percona Database Experts for professional support and services.