Skip to content

MySQL slow query log on cPanel — turning it on and reading it

The MySQL slow query log is the most important diagnostic for "the site is slow" tickets. cPanel ships it off by default. Here is how to enable it and what to look for.

The MySQL slow query log captures every query that exceeds a configurable threshold. Almost every “the site is slow” ticket on a WordPress or WooCommerce cPanel host turns out to be a slow query — and without the slow log enabled, you have no evidence and no path to a fix. cPanel ships it off by default because the log can grow large. Turning it on with a sensible threshold and a rotation policy is the highest-leverage diagnostic move on a cPanel host with database-heavy customers.

What’s happening

MySQL’s slow_query_log setting, when enabled, writes any query whose execution exceeds long_query_time (default 10 seconds, way too high) to a log file. For each entry, MySQL records the SQL, the user, the time taken, the rows examined, and the rows returned. The relationship between rows examined and rows returned is the key clue: a query that examines 100,000 rows to return 1 row is almost always missing an index.

The trap: with long_query_time = 10, you only catch the catastrophic queries. The slow queries that ruin TTFB — 500ms-2s queries that run 50 times per page render — never show up. Setting long_query_time = 1 (or even 0.5) is where the real diagnostic value lives.

How to verify

Check whether it is enabled:

mysql -e "SHOW VARIABLES LIKE 'slow_query%'"
mysql -e "SHOW VARIABLES LIKE 'long_query_time'"
mysql -e "SHOW VARIABLES LIKE 'log_queries_not_using_indexes'"

If slow_query_log is OFF, you have no diagnostic data. Enable and tune (see the fix). To see existing slow queries:

mysqldumpslow -s t /var/lib/mysql/slow.log | head -30
mysqldumpslow -s c /var/lib/mysql/slow.log | head -30
tail -200 /var/lib/mysql/slow.log

mysqldumpslow -s t sorts by total time (find the queries costing the most CPU). -s c sorts by count (find the queries running most often).

To attribute slow queries to a cPanel user:

tail -2000 /var/lib/mysql/slow.log | grep -B 1 "User_Host" | sort | uniq -c | sort -rn | head

The fix

  1. Enable the slow log with a useful threshold. In /etc/my.cnf:

    [mysqld]
    slow_query_log = 1
    slow_query_log_file = /var/lib/mysql/slow.log
    long_query_time = 1
    log_queries_not_using_indexes = 0
    log_slow_admin_statements = 0

    log_queries_not_using_indexes = 0 because on a busy host this fills the log with noise (every SELECT * FROM small_table shows up). Turn on temporarily during deep investigation.

  2. Set log rotation. Add to /etc/logrotate.d/mysql-slow:

    /var/lib/mysql/slow.log {
      daily
      rotate 14
      compress
      missingok
      notifempty
      postrotate
        mysql -e "FLUSH SLOW LOGS"
      endscript
    }
  3. Restart MySQL to pick up the new config: systemctl restart mysqld.

  4. Wait at least 24 hours, ideally a full week, then analyze with mysqldumpslow or pt-query-digest:

    pt-query-digest /var/lib/mysql/slow.log > /tmp/digest.txt

    pt-query-digest (from Percona Toolkit) groups similar queries and ranks them by total time impact. The top 10 queries usually represent 80% of the load.

  5. For each top query, run EXPLAIN in MySQL and look at the type (ALL is full table scan = bad, range/ref/eq_ref/const is index use = good) and rows examined.

  6. Add indexes for the offenders. Coordinate with the application developer — sometimes the right fix is rewriting the query, not adding an index.

The slow log is the single best tool for converting “the site is slow” complaints into “here is the specific query that needs an index, and we are adding it on <date>”. Stack Harbor turns on slow log as part of environment management.