One of the most common errors WordPress users encounter when opening their site is the Error Establishing a Database Connection error. This error appears when WordPress is unable to connect to the database, resulting in the site becoming completely inaccessible. Although it may seem daunting at first glance, this issue can often be resolved with a few simple checks. In this article, we will explain the step-by-step methods for fixing the Error Establishing a Database Connection error.

What is the Error Establishing a Database Connection error?

WordPress needs a database to load all site information such as posts, users, settings, and more. Whenever this connection is interrupted, WordPress cannot read the information and displays the error message directly. This problem is usually caused by the following reasons:
- Incorrect database connection details
- Database corruption
- Issue with the hosting server
- Host or server resource exhaustion
- Sudden database password change
Below, we will examine the above reasons step-by-step and explain how to resolve them.
1. Check Database Information in the wp-config.php File

The first and most important step is to check the wp-config.php file. This file contains the information needed for WordPress to connect to the database. Even a small mistake in the database name, username, password, or database server can cause an error to appear.
In this file, which can be accessed in the public_html folder of your host, check the following:
define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );
- The value may differ on virtual servers (such as the IP address or the MySQL service address).
Note: If you change the database password from within the hosting panel, you must enter the new password in this file; otherwise, WordPress will not be able to connect.
2. Checking the Database Status via phpMyAdmin

If the information in wp-config.php is correct, the next step is to check the health of the database.
- Log in to phpMyAdmin

- Select your website’s database

- Check all the boxes.

- From the bottom menu, run “Check Table” or “Repair Table”.

If the database has crashed or become corrupted, it can be repaired using this method.
3. Fixing Errors with Database Repair in WordPress

WordPress has a built-in capability to automatically repair the database. To enable it, simply add this line to the wp-config.php file:
define( 'WP_ALLOW_REPAIR', true );
Then, visit the following link:
yourdomain.com/wp-admin/maint/repair.php
On this page, you have two options to repair or repair and optimize the database. After performing the operation, remove the added line from wp-config.php. Please note that in this address, you need to replace ‘yourdomain’ with your actual domain name.
4. Check Server Resources or Hosting

Full RAM or CPU usage can cause MySQL to stop responding and WordPress to display a database connection error. This is especially common on shared hosting or high-traffic websites.
- Upgrade your hosting plan
- Use a Virtual Private Server (VPS) instead of shared hosting
- Enable caching
- Check for resource-intensive plugins
If you don’t have enough experience managing servers, your hosting support can usually check logs and resource usage. Also, choosing the right WordPress host can help prevent this issue.
5. Changing Your PHP Version or Related Settings

Sometimes, the PHP version is incompatible with your website’s plugins or themes, causing database connection errors. Changing the PHP version or enabling/disabling essential extensions like mysqli may resolve the issue.
- Log in to your hosting account and navigate to the Select PHP Version option in cPanel.

- Change your hosting’s PHP version opposite the Current PHP Version option.

If you are using MultiPHP to manage your hosting’s PHP version:
- Log in to the MultiPHP Manager in your hosting account.

- Change your hosting PHP version using the PHP Version menu. Then, click Apply to save the changes.

6. Check Your Site URL in the Database

If you have recently changed your domain name, the address in the database may not have been updated. In this case, WordPress may be unable to load various elements, sometimes resulting in a database error.
- Log in to your hosting phpMyAdmin.

- Select your website’s database.

- Within your website’s database, locate and select the `wp_options` table.

- In this table, check the `siteurl` and `home` fields. If the address entered in these fields differs from your website’s domain address, modify them accordingly.

7. Restore a Backup
If all of the above methods fail, the best course of action is to restore from a backup. On our cPanel hosting, automatic backups are created daily, weekly, and monthly by the Jetbackup5 system.

You can use this system to restore your hosting backups.
Alternatively, you can restore a specific backup through the Backup Wizard option in cPanel.
The Error Establishing a Database Connection error is a common, yet fixable, issue in WordPress. Most often, the problem stems from incorrect database credentials, corrupted tables, or server configuration. By following the steps in this article, you can easily identify and resolve the issue.