How to Fix the “Error Establishing a Database Connection” in WordPress


Of the nearly 2 billion websites that exist, 836 million of them are hosted via WordPress. WordPress has gained this enormous popularity thanks in part to both its reliability and ease of use. However, issues and errors with a WordPress site can still occur.

WordPress powers millions of websites, from small blogs to large business platforms. While it’s known for its flexibility and ease of use, even well-maintained sites can run into technical issues that disrupt performance or take your site offline.
One of the most common WordPress issues is the “Error Establishing a Database Connection.” When this connection error appears, your site cannot access its WordPress database, so pages fail to load and visitors may see a blank screen or an error message.
We explain what the error means, what typically causes it, and the troubleshooting steps you can take to restore access and get your site back online quickly.
What does “Error Establishing a Database Connection” mean?
If you see an error message that says "Error Establishing a Database Connection,” it means WordPress is unable to connect to the database server (typically MySQL) that stores your website’s content and settings. As a result, your site may fail to load properly or become completely inaccessible.
Several issues can cause this error, including:
- Incorrect database credentials
- An unavailable database server
- Corrupted files
- Damaged or misconfigured core WordPress files
In some cases, insufficient database permissions, plugin or theme conflicts, or hosting-related issues can also prevent a successful connection.
To fix it, you need to identify the exact cause, which can usually be done with a few preliminary checks.
Initial checks
Before you dive into the troubleshooting, these checks will help you narrow down the likely cause.
- Start by verifying if the error occurs on the front end, WP-admin, or both. If the error only occurs on the front end, it could be a potential issue with the database connection settings or the database itself. If the error is specific to the WP-admin dashboard, it could indicate issues such as plugin or theme conflicts or file permission issues.
- You should also check with your hosting provider to ensure the database server is running smoothly. This will let you know if the issue is confined to your website or if it is affecting other users on the server as well.
How to fix the error step-by-step
After identifying the likely cause, these troubleshooting steps can help restore your database connection and get your site back online.
- Backup your files and database
- Verify database credentials
- Check database server status
- Repair the corrupted database
- Check WordPress files
- Disable conflicting plugins and themes
- Update WordPress site URL
- Contact hosting provider
The sections below explain each step in detail.
1. Backup your files and database
Create a full backup of your WordPress files and database before making any significant changes. A recent backup lets you restore your site if something goes wrong during troubleshooting.
2. Verify database credentials
Before you can establish a database connection, you have to have the correct database login credentials. Start by verifying and updating DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST in the wp-config.php file.
In the wp-config.php file, these credentials are defined as:
- DB_NAME: The name of your WordPress database
- DB_USER: The database username with access to the database
- DB_PASSWORD: The password for the database user
- DB_HOST: The database server hostname, often set to localhost but sometimes provided by your web hosting provider
All four values must match the credentials configured in your hosting account. Even a small mismatch can prevent WordPress from connecting.
Make sure the database user also has the correct permissions. Insufficient privileges can block access and prevent a successful connection.
3. Check database connection
If your credentials are correct but the error persists, the issue may be with the database server itself. You can use a simple PHP script to test the connection. This test helps confirm whether the database server is reachable.
Database server issues can result from maintenance, traffic spikes, or resource limits. When the server is offline or overloaded, WordPress may be unable to connect, which can lead to site downtime.
4. Repair the corrupted database
Repairing the WordPress database can often fix any connection issues you are experiencing. To do this, you'll need to enable the database repair feature by adding the wp_allow_repair code to the wp-config.php file. You can then access repair.php via a web browser to repair corrupted database tables.
5. Check WordPress files
Corrupted WordPress files can sometimes cause database connection errors. Check core files for corruption by looking for files that have been modified unexpectedly or appear to be missing essential code, then replace them using FTP or File Manager in cPanel.
6. Disable conflicting plugins and themes
Plugins and themes can sometimes conflict with the database connection. To see if this is the issue, you can deactivate all plugins using FTP or phpMyAdmin and test theme-related issues by reverting to a default WordPress theme.
7. Update WordPress site URL
The siteurl and home values in the wp_options table have to match your website's domain name. Use phpMyAdmin to verify that they match and update them if there are any discrepancies.
8. Contact hosting provider
If none of these steps resolves the issue, you should contact your hosting provider's support team for help. In a lot of cases, the hosting provider will be able to assist with MySQL database or web server issues.
Advanced solutions
Basic troubleshooting steps can often resolve database connection errors. However, for more complex issues, you will need to work with your hosting provider to explore advanced solutions. This is especially true for managed WordPress hosting or virtual private server (VPS) environments, since you are going to be limited in regard to the adjustments you can make on your own.
For example, you may need to adjust server-side configurations (such as PHP settings, MySQL configuration parameters, or firewall rules) to accommodate your website's requirements, and this is something you will have to work with your hosting provider or server administrator to do. Or, it may also be the case that you have insufficient server resources and need to evaluate your hosting plan to ensure you have adequate resources for your website's needs.
Preventing future errors
After you resolve the issue, you can take steps to reduce the chances of another database connection error. Keep WordPress updated to the latest version and schedule regular backups of your database and website files so you can restore your site quickly if needed.
You can also lower the risk of future connection issues by following these best practices:
- Choose a reliable WordPress hosting provider with stable database performance and responsive support.
- Monitor server resources so traffic spikes or limits do not take your site offline.
- Maintain strong security practices to protect against malware that can damage files or corrupt the database.
- Optimize and clean your WordPress database regularly to keep it running properly.
- Update plugins and themes often and remove any plugins you no longer use.
Partner with SiteLock for WordPress security
“Error Establishing a Database Connection” is a WordPress error that can commonly be fixed with basic troubleshooting steps, though working with your hosting provider to explore more advanced solutions is sometimes required. Whether you encounter this issue or not, be sure to follow WordPress.org tutorials and documentation for ongoing WordPress website maintenance to keep your website healthy and secure.
Strengthening your website’s security can also help reduce the risk of errors. SiteLock offers a range of solutions designed to help WordPress site owners protect their websites and maintain performance. The SiteLock WordPress security plugin provides a lightweight, easy-to-deploy way to strengthen your site with built-in hardening controls, login protection, and cloud-based checks that run without impacting performance.
To see how SiteLock supports stronger site security, learn more about SiteLock’s WordPress solutions and plugin.