Looking for ways to repair WordPress database? 🤔
The WordPress database needs troubleshooting and repair when you encounter the following errors:
- “Error establishing database connection”
- “No update required, your database is already up-to-date”
- “Warning: require_once(path/to/file.php) function.require-once: failed to open stream: No such file or directory in…”
Other signs of database issues include sudden unexplained changes on your site like missing content and images, plugins frequently malfunctioning, and the inability to upload images.
Database issues occur due to reasons like incorrect database credentials, corrupt files, hacked sites, and exceeding PHP memory limits, among other things.
Let’s dig in.
Common causes behind corrupt WordPress database
There are a number of causes that could have corrupted or broken your WordPress database files. The most common ones are:
1. Server issue
The errors you notice on your website could appear because the site can’t connect with your hosting server. The server is taking too long to respond or is completely unresponsive. Unfortunately, server issues are common in cheap hosting services.
Some hosting services also use separate servers for your site’s files and database. In these configurations, it’s possible for your site’s file server to still be working even if the database server is down.
2. Database credentials issue
WordPress databases can be accessed using the database credentials. These credentials are stored in the wp-config.php file. If the file has been tampered with, then the credentials may have been changed or removed from the file.
Therefore, the problems you are facing on your WordPress website could be springing from incorrect database credentials.
Database credential issues can also appear if you’ve recently migrated your WordPress site from one host to another.
3. Corrupt database tables
The WordPress database is made of tables that store your site’s data. Although the database is a robust system like any other technical system, it can experience failure and corruption.
Corruption of the tables can cause loss of data which is why probably some of your site’s content and images are missing.
4. Hacked site
Corruption of the database and modifications of the database credentials in the wp-config.php file can be a sign of a hacked website.
After gaining access to a WordPress website, hackers often tamper with files and databases. They use the site’s resources to store illegal files and folders or launch attacks on other websites.
These are the most common reasons that lead to database corruption. In the next section, you will learn how to repair your WordPress database and get your website up and running in no time.
How to prepare to repair WordPress database
There are a number of ways to repair the WordPress database. But before you start with the solutions, there are two crucial steps that you will need to take.
First, you need to take a backup of your entire website and then enable error logs in the wp-config.php file to find the exact cause of the database corruption. Let’s dive into these steps before learning how to repair the WordPress database.
1. Back up your WordPress site
To fix the database issue, you will be taking steps that can damage your website.
For instance, you will be accessing phpMyAdmin to automate database repair. A single misstep in the phpMyAdmin can lead to big trouble. You may end up breaking your website.
Having a backup is a safety net. When things go wrong, just restore the backup and try the other solutions.
If your host has a reliable backup tool, that’s often the best place to start. Alternatively, you can manually back up your site.
2. Enable error logs to pinpoint cause
As we said earlier, there are a number of reasons that could have caused the database corruption on your WordPress site. To fix the database, you need to first learn the exact reasons. The best way of doing this is by reviewing the WordPress error logs in the wp-config file.
The wp-config.php file is a core WordPress file, and it contains your website’s configuration details along with the database credentials. The file can be used as a debugging tool. It can generate log errors that you can review to identify whether a plugin or a theme or something else has been causing issues on your site.
To access the wp-config file, open your hosting account and go to cPanel and select the File Manager.
In File Manager, go to public_html → wp-config.php. Right-click on the file and select Edit.
Scroll down and insert the following code snippet above this sentence:
/* That's all, stop editing! Happy publishing. */
define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true );
The first line enables the debug mode and the second one ensures that the error logs are stored in the debug.log file.
You may find that the code already exists but the debug mode has been set to
false. In this case, you can just change the existing code to
Open the debug file, and it will show you all the warnings and errors that the site has encountered. Once you identify the real problem, you can start fixing your database by jumping to the relevant solutions below.
But if you can’t decipher the error messages and can’t pinpoint the exact reason behind the database corruption, then we recommend trying out the following solutions one by one.
How to repair the WordPress database
Now that you’ve completed the preliminary steps, you’re ready to jump into repairing your database.
The first step is to make sure you’re using the correct database credentials. Once you’ve done that, you can move on to additional steps designed to repair a corrupted database.
- Check your WordPress database credentials
- Repair WordPress database using a plugin
- Use the built-in WordPress database repair tool
- Repair database in phpMyAdmin
- Repair database using WP-CLI
1. Check your WordPress database credentials
One of the most common causes behind the error establishing database connection error is the presence of incorrect database credentials in the wp-config.php file. The database credentials include the following:
- database name
You need to check whether the database credentials in the wp-config.php file are correct.
You will find your site’s database credentials on MySQL Databases.
If your host uses cPanel, you can access the MySQL Databases by going to cPanel → MySQL Databases. If your host doesn’t use cPanel, you’ll need to consult your host’s documentation.
If the MySQL Databases do not show you the credentials, then you need to find the email that you received from your hosting provider when you first set up the hosting account. It will have your database name, user, password, and hostname.
But if you can’t find the email, then the only thing left to do is to talk to your hosting support and ask them to find your credentials for you.
Alternatively, you can also create a new database user and assign it to the database. You can then add the credentials for that user to your wp-config.php file.
When you have the credentials, open your hosting account once more and go to cPanel → public_html → wp-config.php. Next, open the file and match the credentials. If the credentials are incorrect, then edit the file and add the correct credentials.
📌 If this doesn’t fix the problem, you’ll need to continue and work on steps to repair your database.
2. Repair WordPress database using a plugin (if you have dashboard access)
If you’re still able to access your backend WordPress dashboard, you can use plugins to repair your database.
However, if you’re unable to access your WordPress dashboard, you’ll want to skip to the next sections for some database repair options that don’t require dashboard access.
Advanced Database Cleaner and WP-DBManager are the two popular database repair plugins. You can install any one of them to repair your database.
If you have installed the Advanced Database Cleaner on your site, then go to WP DB Cleaner on your dashboard menu and open the Tables tab.
Next, select all the tables and from the dropdown choose Repair and hit the Apply button.
If you have installed WP-DBManager, then go to Database → Repair DB, and on the next page, scroll down and hit the Repair button.
3. Use the built-in WordPress database repair tool
WordPress also includes its own built-in database repair tool to help you fix a corrupt WordPress database.
However, to access it, you’ll first need to enable it manually by editing your wp-config.php file using cPanel File Manager or FTP.
Open the file and add the following code snippet right above the
/* That's all, stop editing! Happy publishing. */ line:
define ('WP_ALLOW_REPAIR', true);
Next, open this URL:
https://example.com/wp-admin/maint/repair.php. Be sure to replace
example.com with the actual URL of your site.
Once you hit the enter button, you will be taken to the database repair page and you will have two options to choose from:
- Repair Database
- Repair and Optimize Database
The latter does a more efficient job but takes a long time. If you want to get your site up and running as soon as possible, choose the former. Or else, we recommend choosing the latter option to optimize database performance as well.
⚠️ Important: Once the database repair process finishes, you should go back to your wp-config.php file and remove the line of code that you added. You do not want to leave the database repair tool active once you’re finished using it.
4. Repair database in phpMyAdmin
You have the option to automate the repair process using phpMyAdmin. All you need to do is go to the cPanel → phpMyAdmin and select the database you want to repair.
Next, click on the Check all option and from the With Selection dropdown menu choose Repair Table. The database tables will start repairing themselves.
5. Repair database using WP-CLI
That’s all folks. 😎 By now you should have fixed your database. If you are still facing issues with your website, then you need to seek professional help from your hosting support.
Repair your WordPress database for good 🎯
The database is a crucial part of your WordPress website. Problems in the database can affect your site in a major way.
The true cause behind the database issue can be difficult to diagnose. It could have been caused by incorrect database credentials, a hacked site, corrupt database tables, and even a cheap hosting server.
You can find the real cause by enabling error logs or by simply trying out the following solutions:
- Check whether the database credentials in your wp-config.php file are correct.
- Use the built-in WordPress database repair tool by enabling it in the wp-config.php file.
- Enable phpMyAdmin to repair the tables automatically
- For developers, use the wp db repair command in WP-CLI to repair the database
- Use a plugin like Advanced Database Cleaner or WP-DBManager to repair the database
Once you get your database ⚙️ working, you’ll want to keep it in tip-top shape going forward. To that end, you can check out 👉 our collection of the best WordPress database maintenance plugins.
Do you still have any questions about how to repair the WordPress database? Ask away in the comments section!