How to repair a crashed WordPress database table

Last updated on July 14, 2020 by Dan Nanni

A WordPress database may experience failure and corruption under various circumstances. One of common problems with it is "crashed database." When that happens, you will find the following error in WordPress error log:

WordPress database error Table './my_home/wp_posts' is marked as crashed and last (automatic?) repair failed for query SELECT post_date_gmt FROM wp_posts WHERE post_status = 'publish' AND post_type IN ('post', 'page', 'attachment') ORDER BY post_date_gmt DESC LIMIT 1 made by require('wp-blog-header.php'), wp, WP->main, WP->send_headers, get_lastpostmodified, get_lastpostdate, _get_last_post_time

The symptom of such table crash varies. Depending on which table was crashed, your WordPress blog may malfunction differently. If a crashed table is wp_posts table which contains a list of posts, you will not see any post in your blog. Xmodulo experienced such database crash over the weekend:

The WordPress blog of Xmodulo is powered by MySQL at the moment, so in this tutorial, I will focus on MySQL.

Why a MySQL Table Crash?

There may be different reasons, but typically when mysqld or the host where mysqld is running is forcibly shut down in the middle of a database update, a database crash can be triggered. In the rest of the tutorial, I will show how to repair a crashed WordPress MySQL database table.

Method One

If you are using a shared web hosting, typically you will have access to Cpanel, which allows you to repair a database through Cpanel web interface.

Log in to Cpanel, and click into "MySQL Databases" under Databases section.

On "MySQL Databases" page, you will find "Repair DB" option under "Modify Databases" section.

Choose the database which contains a crashed table, and click "Repair DB" button. If repair is successful, you will see the status of every table as OK.

my_database.wp_oiopub_tracker_visits OK
my_database.wp_options OK
my_database.wp_postmeta OK
my_database.wp_posts
error : Keyblock size at page 13001728 is not correct. Block length: 532 key length: 2
Error : Incorrect key file for table './my_database/wp_posts.MYI'; try to repair it
error : Corrupt
---------
After repair:
----
my_database.wp_oiopub_tracker_visits OK
my_database.wp_options OK
my_database.wp_postmeta OK
my_database.wp_posts OK
my_database.wp_sharebar OK
my_database.wp_term_relationships OK

Method Two

If you have root access on the host (e.g., VPS) where a MySQL sever is running, you can try command-line interface to fix the crashed database table.

For a MyISAM-type database, you can fix a crashed table with a command-line utility called myisamchk. This utility comes with MySQL server installation.

First, stop MySQL server.

$ sudo service mysql stop (old Debian/Ubuntu)
$ sudo service mysqld stop (old CentOS/RHEL)
$ sudo systemctl stop mysqld (CentOS/RHEL 7+, Fedora)
$ sudo systemctl stop mysql (Debian 7+, Ubuntu 15.04+)

Then use this command to fix a particular table.

$ cd /var/lib/
$ sudo myisamchk -r -v -f mysql/<database_name>/<table_name>

Finally, start MySQL server again.

Do you have any experience with a crashed database, or any other problem with a WordPress database? Do you regularly back up your WordPress database? Tell us what was your approach to fix the problem and maintain the database.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean