How do you know a table has crashed?
The error log tells you first: it names the table and the storage engine involved. Applications usually show something vaguer, such as Table './shop/orders' is marked as crashed and should be repaired, Incorrect key file for table, or a lost connection whenever a certain query runs.
Log locations differ by distribution and server flavor:
- MySQL on Ubuntu or Debian:
/var/log/mysql/error.log - MariaDB on Ubuntu or Debian: the systemd journal, read with
journalctl -u mariadb, plus/var/log/mysql/error.logif logging to a file is switched on - MySQL on Rocky Linux 9 or AlmaLinux 9:
/var/log/mysql/mysqld.log - MariaDB on Rocky Linux 9 or AlmaLinux 9:
/var/log/mariadb/mariadb.log
sudo tail -n 100 /var/log/mysql/error.log
sudo journalctl -u mysql -u mysqld -u mariadb --since "1 hour ago"
mysql --versionThe most common causes are a full disk, the kernel killing the server for lack of memory, and a hard power cycle in the middle of a write. Check those before repairing, or the damage comes straight back. df -h and sudo dmesg | grep -i -E "killed process|out of memory" answer both questions, and the disk usage guide helps when space is the culprit.
Which storage engine is the table using?
Look it up in information_schema, because the repair method depends entirely on the engine.
sudo mysql -e "SELECT table_schema, table_name, engine FROM information_schema.tables WHERE table_schema NOT IN ('mysql','sys','information_schema','performance_schema') ORDER BY engine;"- InnoDB (the default for years) is transactional and crash safe. It replays its redo log after an unclean stop, so real corruption is rare and usually points at failing storage or a filled disk. REPAIR TABLE does nothing useful here.
- MyISAM has no crash recovery. An interrupted write leaves the index file marked as crashed, and REPAIR TABLE or
myisamchkrebuilds it. - Aria is the MariaDB replacement for MyISAM, used for some system tables. It has its own log and its own offline tool,
aria_chk.
If you find MyISAM tables in an application schema, plan to convert them to InnoDB once they are healthy again; the last section shows how.
What should you do before any repair?
Take a copy of the files as they are right now. A repair rewrites index and data files, and a failed attempt on the only copy can lose more rows than the original crash did.
sudo systemctl stop mysql # mysqld on Rocky/Alma, mariadb for MariaDB
sudo mkdir -p /root/db-before-repair
sudo cp -a /var/lib/mysql /root/db-before-repair/
sudo du -sh /root/db-before-repair/mysql
sudo systemctl start mysqlMake sure the target disk has room for the copy first. If it does not, stream a compressed archive to another machine over SSH instead, for example sudo tar -C /var/lib -czf - mysql | ssh deploy@198.51.100.30 "cat > mysql-copy.tar.gz". Keep the copy until the application has run cleanly for a few days. The daily backups tutorial covers keeping regular copies so the next incident starts from a known good point.
How do you check tables with SQL or mysqlcheck?
Use CHECK TABLE for one table and mysqlcheck to sweep a whole database or the full server. Both run while the server is up.
sudo mysql shop -e "CHECK TABLE orders, customers EXTENDED;"
# every table in one database
sudo mysqlcheck --check shop
# every database, only tables not closed cleanly
sudo mysqlcheck --check --check-only-changed --all-databasesA healthy table returns status OK. Anything else (a warning about the key count, Corrupt, or a note that the table is marked as crashed) tells you which table needs work. On MariaDB the client is also installed as mariadb-check, with the same options.
The EXTENDED option reads every row and every key, so it takes a while on large tables and holds a read lock, which blocks writers. Run it outside busy hours if the application cannot pause.
How do you repair a MyISAM or Aria table?
Run REPAIR TABLE online, or mysqlcheck --repair for many tables at once. Writes to the table wait until the repair finishes.
sudo mysql shop -e "REPAIR TABLE orders;"
sudo mysqlcheck --repair shop orders
# rebuild everything in a database that reports problems
sudo mysqlcheck --auto-repair --check shopWhen the online repair fails, stop the server and work on the files directly. Offline tools must never run while the server has the table open.
sudo systemctl stop mysql
cd /var/lib/mysql/shop
sudo myisamchk --recover orders.MYI
# if that still fails, try the slower safe mode
sudo myisamchk --safe-recover orders.MYI
# Aria tables on MariaDB
sudo aria_chk --recover orders.MAI
sudo chown -R mysql:mysql /var/lib/mysql/shop
sudo systemctl start mysqlIf only the index is beyond saving, REPAIR TABLE orders USE_FRM; rebuilds it from the table definition. Treat that as a last resort: it resets the row count from the data file and can drop rows that the index alone pointed at.
How do you recover a damaged InnoDB table?
Start the server in forced recovery mode, dump what is readable, and rebuild from the dump. InnoDB cannot be patched in place, so the goal is to get data out cleanly.
Add the setting to the server section of the config file (/etc/mysql/mysql.conf.d/mysqld.cnf for MySQL on Ubuntu, /etc/mysql/mariadb.conf.d/50-server.cnf for MariaDB on Debian family systems, /etc/my.cnf.d/ on Rocky and Alma):
[mysqld]
innodb_force_recovery = 1sudo systemctl restart mysql
sudo mysqldump --single-transaction shop orders > /root/orders.sqlRaise the value one step at a time (1, 2, 3 and so on) only when the server refuses to start or the dump fails. Levels 1 to 3 are fairly safe; 4 and higher can leave pages permanently damaged, and at those levels the server opens tables read only. Once you have a clean dump:
sudo mysql shop -e "RENAME TABLE orders TO orders_broken;"
# remove innodb_force_recovery from the config, then
sudo systemctl restart mysql
sudo mysql shop < /root/orders.sql
sudo mysql shop -e "CHECK TABLE orders; SELECT COUNT(*) FROM orders;"When the dump stops partway through, export in primary key ranges with --where="id < 500000" and skip the broken range. Compare the counts against your last backup to see what was lost; the backup and restore guide explains restoring single tables.
How do you stop it happening again?
Convert leftover MyISAM tables, keep the disk well below full, and give the server enough memory headroom that it is never the process the kernel kills.
sudo mysql shop -e "ALTER TABLE orders ENGINE=InnoDB;"
sudo mysql -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';"
free -h- Size
innodb_buffer_pool_sizeto roughly half of RAM on a server that also runs a web stack, and add a swap file as a cushion. - Shut the server down with
sudo systemctl stopbefore reinstalling or powering off from the console, rather than cutting power with a running database. - Schedule
mysqlcheck --check --all-databasesweekly with a cron job and send its output to a log you read. - Keep
mysqldumpor physical backups that you have actually test restored.
If a database keeps outgrowing its memory, move to a plan with more RAM: the migration tutorial shows the steps.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Repair a crashed MySQL table
Does REPAIR TABLE work on InnoDB?
No. For InnoDB it only reports that the engine does not support repair. Use forced recovery plus a dump and reload, or restore from a backup.
Can I run myisamchk while MySQL is running?
Only with --check and even then the result may be misleading. Any repair with myisamchk or aria_chk needs the server stopped, otherwise both processes write to the same files.
Is innodb_force_recovery safe to leave on?
No. It is a rescue setting. Remove it as soon as you have your dump, restart, and check that the server runs normally. Values of 4 and above can make the damage permanent.
Why does the same table crash every few days?
Repeated crashes usually mean the server is killed by the out of memory handler, the disk fills up, or the table is MyISAM under heavy writes. Check dmesg, df -h and the engine, then fix the cause.
Will mysqlcheck --auto-repair lock my site?
Each table is locked while it is checked or repaired, so pages that write to that table wait. Small tables finish in seconds; run large sweeps during quiet hours.
Is the process the same on MariaDB?
Mostly. The SQL commands and mysqlcheck match, the service is called mariadb, the clients are also available as mariadb-check and mariadb-dump, and Aria tables use aria_chk.
More in Databases
- Install MySQL 8
- Install MariaDB
- Install PostgreSQL
- Install MongoDB
- PostgreSQL JSON, arrays and special types
- PostgreSQL views, triggers and CTEs
- PostgreSQL full-text search and encryption
- MySQL keys, indexes and full-text search
- MySQL JSON, views and stored procedures
- Apache Solr search server
- Install Redis or Valkey
- Install Memcached
- Apache Cassandra cluster
- Install Apache CouchDB
- InfluxDB for time series
- Install Elasticsearch
- Back up and restore MySQL and MariaDB
- Back up and restore PostgreSQL
- MySQL users, roles and passwords
- PostgreSQL roles and permissions
- PostgreSQL streaming replication
- MongoDB replica set
- PostGIS for location data
- Database insight with PgHero
- Couchbase Server
- ArangoDB multi-model database
- Remote database access over SSH