Repairing and Optimizing all MySQL and MariaDB Databases on the Server

Server Maintenance | Updated 2026

Regularly repairing and optimizing MySQL/MariaDB tables reclaims unused space, fixes corrupted indexes, and improves query performance. Here is how to do it at both the account and server level.

01. Server-Wide Optimization (Root)

# Check, repair, and optimize all databases
mysqlcheck -u root -p --all-databases --check --optimize --auto-repair

# Or separately:
mysqlcheck -u root -p --all-databases --repair
mysqlcheck -u root -p --all-databases --optimize

02. Automate With Cron

# Weekly optimization - add to root crontab
0 3 * * 0 mysqlcheck -u root --all-databases --optimize --auto-repair >/dev/null 2>&1
InnoDB Note

For InnoDB tables, OPTIMIZE TABLE actually performs a table rebuild (ALTER TABLE ... ENGINE=InnoDB). This can temporarily lock the table and use significant disk space for large tables. Run during low-traffic periods. For ibdata1 bloat, see Shrink ibdata1.

For backups before maintenance: Backup MySQL.

Database Issues?

Open a Support Ticket

Quick Recap

  1. phpMyAdmin: Check all > Optimize table
  2. SSH: mysqlcheck --optimize --auto-repair
  3. Server-wide: --all-databases flag (root only)
  4. Automate weekly via cron
  5. InnoDB optimize rebuilds tables - Run off-peak

Database maintenance · Last updated March 2026 · Browse all Server Maintenance articles

  • 147 Users Found This Useful

Was this answer helpful?

Related Articles

How to convert InnoDB to MyISAM

Server Maintenance | Updated 2026 Converting tables between InnoDB and MyISAM storage engines...

FTP Failed: Service Check Unable to Connect to Port 21

Server Maintenance | Updated March 2026 When cPanel's service monitoring reports "FTP Failed:...

Remount /tmp with exec permission

Server Maintenance | Updated 2026 Some software installations and compilation tasks require...

VPS Time Not Correct with Time Server

Server Maintenance | Updated March 2026 When a VPS or dedicated server's system clock drifts...



Save 30% on web hosting - Use coupon code Hosting30