Repairing the WordPress database

Earlier this week one website was not responding and showing an error that the connection to the database was not possible. Such an error is usually a sign, that the credentials are invalid. But no changes have been made to either the credentials or the configuration file. After activating the WP_DEBUG mode I found out that the database was corrupt.

Repairing the database with the WP-CLI

My first approach was using the WP-CLI. There you have an option to optimize the tables, which can also sometimes repair errors:

wp db optimize

This command is usually been used when large amounts of rows where deleted but the table size was not reduced. But it will not always also fix issues with the tables. Fortunately there is another command for this task:

wp db repair

When a table was repaired, you will probably find a line like this in the output of the command:

database_name.wp_options
warning  : Number of rows changed from 378 to 381
status   : OK

After running the command, your WordPress website will hopefully be working again.

Repairing the database in the browser

For those of you who don’t have access to the server through SSH and unable to use the WP-CLI, there is an alternative. First you have to add this line to your wp-config.php file:

define( 'WP_ALLOW_REPAIR', true );

If you databases is broken and you try to access your backend, you should find a link to the repair page. If not, just navigat to the /wp-admin/maint/repair.php path. Here you should find this page:

Repair WordPress database page

All you have to do here is clicking the “Repair Database” button. You can also run the database optimization from here. The next page will show you the results of the repair process. If it was successful, you can remove the line from your wp-config.php file and login to your dashboard as usual.

Conclusion

It wasn’t the first time a database of a WordPress installation was corrupt, but I didn’t knew that there is a UI to repair the database. If you still have access to the wp-config.php page, repairing the database should be as easy as the click on a button.

Posted by

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

Leave a Reply

Your email address will not be published. Required fields are marked *