When top shows mysqld sitting at the top with a CPU percentage in the hundreds, the whole server feels it: every site slows down, load average climbs, and the temptation is to start editing my.cnf buffers in the hope that more memory will calm it. It almost never does, because high mysqld CPU is rarely a tuning problem — it is a workload problem. Some specific query, run by some specific account, is doing far more work than it should, and the database is faithfully executing it over and over. The fix is to catch that query in the act, identify the account behind it, and address the cause (usually a missing index or an abusive caller) rather than feeding the symptom with bigger buffers.
What’s happening
MySQL uses CPU in proportion to the work its queries demand. A query that scans a million rows because it has no usable index costs a thousand times the CPU of the same query hitting an index — and if that query runs on every page load, mysqld pegs a core and stays there. The CPU is not “stuck”; it is being asked to do an enormous amount of pointless work, repeatedly.
On a shared cPanel host, the cause is almost always traceable to one account. A WordPress site with an unindexed meta_query, a WooCommerce store running an expensive report on every admin page, a plugin doing SELECT ... LIKE '%term%' across a huge table, or a search feature with no full-text index — any of these can drive server-wide load through one tenant. There are two other patterns worth ruling out: a flood of connections each running a cheap query (which is a concurrency problem, not a per-query one), and genuine under-provisioning where the buffer pool is too small for the working set so InnoDB constantly reads from disk and burns CPU on it. But the first move is always to see what mysqld is actually executing right now, because the live PROCESSLIST usually names the culprit immediately.
How to verify
Catch the running queries first — this is where the answer usually is:
# What is mysqld executing right now? Long Time + Query state = the suspect
mysql -e "SELECT id, user, host, db, command, time, state, LEFT(info,120) AS query
FROM information_schema.processlist WHERE command != 'Sleep' ORDER BY time DESC;"
# Watch it live, refreshing
watch -n1 'mysqladmin processlist | grep -v Sleep'
# Which account owns the active queries?
mysql -e "SELECT user, COUNT(*) FROM information_schema.processlist WHERE command!='Sleep' GROUP BY user ORDER BY 2 DESC;"
Confirm mysqld is the load source and see overall CPU shape:
# Is mysqld really the top consumer?
top -bn1 | head -15
ps -eo pid,pcpu,comm --sort=-pcpu | head
# CloudLinux: per-user DB load, if available
dbtop 2>/dev/null
If a query repeats, capture the slow ones over time rather than guessing:
# Turn on the slow query log briefly and read what accumulates
mysql -e "SET GLOBAL slow_query_log=1; SET GLOBAL long_query_time=2;"
tail -f /var/lib/mysql/$(hostname)-slow.log
# Summarize the worst offenders
mysqldumpslow -s t /var/lib/mysql/$(hostname)-slow.log 2>/dev/null | head -30
The fix
- Identify the live query before tuning anything — the
PROCESSLISTquery above almost always shows one statement with a highTimeand a tellingstate(Sending data,Copying to tmp table,Sorting result). That statement, and the account running it, is the diagnosis. Editingmy.cnfbefore you have seen the query is guessing. - EXPLAIN the offending query and add the missing index — copy the slow statement and run
EXPLAIN SELECT ...against it. Atype: ALLand a largerowscount means a full table scan — the query has no usable index. Add the index theWHERE/JOIN/ORDER BYneeds (ALTER TABLE ... ADD INDEX), and the CPU cost usually collapses. A single missing index is the most common cause of server-wide mysqld load. - Kill a runaway query to recover immediately, then fix the source — if one query is pinning the CPU right now,
KILL <id>;(using the id from PROCESSLIST) gives the server instant relief. This is triage — the query will return next page load — so follow it with the index or application fix. Do not kill in a loop as a “solution”; find why it is slow. - Fix or throttle the account generating the load — if the queries trace to one tenant (a plugin, a report, a scraper hitting search), the durable fix is in that account: update or replace the offending plugin, cache the expensive page, add a full-text index for
LIKE '%...%'searches, or rate-limit the caller. On CloudLinux, MySQL Governor can cap that user’s database CPU so one tenant cannot dominate the host while you fix the root cause. - Distinguish a connection flood from a heavy query — if PROCESSLIST shows many connections each running a cheap query (rather than one expensive one), the problem is concurrency, not a single query. That points at an application opening too many connections or a traffic spike; the fix is caching and connection limits, not an index. The two look similar in
topbut need opposite responses. - Right-size the InnoDB buffer pool only if the working set does not fit — if the slow log and PROCESSLIST show many queries reading from disk (high
Innodb_buffer_pool_reads), and the buffer pool is genuinely too small for the active data, theninnodb_buffer_pool_sizeis worth raising — to roughly the size of the hot data, within the server’s RAM. This is real tuning, but it is the fix only after you have ruled out a missing index, which is far more common. - Confirm load returns to normal under real traffic — after adding the index or fixing the account, watch
topand the slow log over the next traffic cycle. mysqld CPU should drop to a fraction of where it was. Re-runEXPLAINon the previously slow query to confirm it now uses the index (type: ref/range, smallrows). Load that stays down under the same traffic is the proof; a quiet moment is not.
High mysqld CPU is the database doing exactly what it was told — the work is the problem, not the engine. Catch the query, EXPLAIN it, add the index or fix the account, and reserve buffer tuning for the genuine case where the working set does not fit. Stack Harbor profiles and fixes database-driven server load under cPanel/WHM management — start a conversation when mysqld is dragging the whole host down.