Mysql – How to find MySQL users causing high load

MySQLmysql-5.7

I have a MySQL server with multiple websites running on it. These sites are mostly the same software, but since recently MySQL is under very high CPU load.

I'm pretty sure there is 1 MySQL user that is causing the higher load, but how can I identify the resources used per user?

I know I can limit the number of connections and number of queries per user, but before enforcing limits, I want to know what the current numbers are.

How can this be measured (in MySQL 5.7)?

Best Answer

The performance schema provides statistics aggregated by account (user + host), by user, and by host.

See for example table:

performance_schema.events_statements_summary_by_account_by_event_name

Reference manual https://dev.mysql.com/doc/refman/5.7/en/statement-summary-tables.html

This table will show statement statistics (aka, queries) broken down per account ... anyone causing heavy traffic (high number of queries) or expensive computation (a few very expensive and long latencies queries) should be very visible in these statistics.

[Edit] Example of query:

select `USER`, `HOST`, SUM(COUNT_STAR) AS TOTAL
  from events_statements_summary_by_account_by_event_name
  group by `USER`, `HOST`
  order by TOTAL DESC;

will show how many queries each account do, ordered by count descending.