Knowledgebase

How to fix MySQL/MariaDB high load and repair database tables

When MySQL or MariaDB uses a lot of CPU, websites become slow, show "Error establishing a database connection", or time out. This guide explains how to find which database and which queries cause high load on your server, how to check and repair tables, and what to change so the problem does not come back. It covers cPanel servers and VPS servers without a panel.

Shared hosting customers: you cannot see server-wide processes, but you can still repair your own databases in cPanel (see Step 4) and reduce heavy queries on your site. If your account hits its resource limits often, contact support.

Step 1: Confirm the database is the problem

top                      # press Shift+P to sort by CPU, look for mysqld/mariadbd
mysqladmin processlist   # or: mysql -e "SHOW FULL PROCESSLIST;"
mysqladmin status

If mysqld or mariadbd is at the top of CPU usage and the process list shows many queries in Sending data, Copying to tmp table or Waiting for table level lock, the database is the bottleneck. The db column shows which database the queries belong to.

Step 2: Stop a stuck query (if needed)

A single very long query can block others. You can stop it by its Id. This cancels that query only, but do not kill queries you do not understand, such as backups or imports in progress:

mysql -e "SHOW FULL PROCESSLIST;"
mysql -e "KILL 12345;"     # the Id of the stuck query

Step 3: Enable the slow query log

The slow query log records queries that take longer than a set time. Add these lines, restart the service (systemctl restart mariadb or mysqld), and let it collect data for a while:

# /etc/my.cnf  (inside the [mysqld] section)
slow_query_log = 1
slow_query_log_file = /var/lib/mysql/slow.log
long_query_time = 2

Then show the top 10 slowest query patterns:

mysqldumpslow -s t -t 10 /var/lib/mysql/slow.log

Turn the log off again when you finish, because it grows over time. On cPanel servers, you can also edit these settings in WHM under SQL Services.

Step 4: Check and repair tables

In cPanel: open Manage My Databases (MySQL Databases), and under Modify Databases use Check Database and Repair Database.

In WHM: search for Repair a MySQL Database.

From SSH: always take a backup first:

mysqldump --single-transaction dbname > /root/dbname-$(date +%F).sql   # backup first
mysqlcheck --check dbname
mysqlcheck --repair dbname          # MyISAM / Aria tables only
mysqlcheck --optimize dbname
Note: REPAIR works on MyISAM and Aria tables. InnoDB tables are not repaired this way; a damaged InnoDB table is usually fixed by restoring a backup. Never delete files inside /var/lib/mysql by hand.

Step 5: Fix the cause

  • Convert old MyISAM tables to InnoDB, which locks rows instead of whole tables. Find them with this query, then convert each table with ALTER TABLE table_name ENGINE=InnoDB; after a backup:
SELECT table_schema, table_name, engine
FROM information_schema.tables
WHERE engine = 'MyISAM' AND table_schema NOT IN ('mysql','information_schema','performance_schema','sys');
  • WordPress: use page caching, clean expired transients and very large autoloaded options, and remove plugins that run heavy queries on every page. See How to Speed Up Your WordPress Website.
  • Add indexes to columns used in WHERE and JOIN in the slow queries (a developer task).
  • Block bad bots that crawl search and filter pages thousands of times.
  • Tune memory on a VPS: innodb_buffer_pool_size should be large enough for your data but leave RAM for the web server and PHP. Tools such as MySQLTuner can suggest values; change one setting at a time.

Common problems

  • "Table is marked as crashed": repair the table as in Step 4.
  • "Too many connections": usually slow queries keep connections open. Fix the slow queries first; raising max_connections alone only hides the issue.
  • The database service does not start: read the error log (often /var/lib/mysql/HOSTNAME.err or journalctl -u mariadb). A full disk is a common cause; check with df -h.

Frequently asked questions

Is repairing a table safe?

Usually yes, but a repair can lose damaged rows. That is why you should take a backup first. See How to back up and restore your website.

Should I use MySQL or MariaDB?

Both work for most sites. cPanel supports both; changing from one to the other on a live server needs planning, so ask support before you upgrade.

Need help? Open a support ticket or message us on WhatsApp +20 127 113 3655.

  • MySQL, MariaDB, high load, slow query, repair tables, cPanel, WHM

Was this answer helpful?

0 Users Found This Useful