How to Fix “Error Establishing a Database Connection” in WordPress (2024)
Are you looking for ways to fix the “Error establishing a database connection” issue to regain access to your site? If yes, you’ve landed on the right article.
The “Error Establishing a Database Connection” is a common issue that causes you to lose access to your WordPress site. It is better to understand how to diagnose and fix this problem to ensure the accessibility of your site.
This is why we will explore 5 easy methods for quickly fixing the “WordPress error establishing database connection”.
Error Establishing a Database Connection (TOC):
What is the “Error Establishing a Database Connection” in WordPress?
The “Error Establishing a Database Connection” is a common issue in WordPress that often results in a blank page with just the error message.
See the image below:
This is usually seen when your site cannot connect to its database, which contains your site’s content and settings.
This error can be frustrating because it blocks access to your site.
What Causes the “Error Establishing a Database Connection” Issue?
There are multiple causes for the WordPress error in establishing a database connection, including the following:
- Incorrect Database Credentials**: The most common cause is erroneous database login details, i.e., database name, username, password, or host, within the `wp-config.php` file.
- Corrupted Database: This error occurs when your database is corrupted due to a plugin, theme, or external factors like a server crash.
- Database Server is Down: If your hosting provider’s MySQL server is down, you may face this issue.
- Too Many Connections: Some shared hosting providers have a limit on database connections. If this limit is exceeded, you might encounter this error.
- Corrupted WordPress Files: Corrupted core WordPress files also prevent the site from establishing a connection with the database.
How to Fix the “Error Establishing a Database Connection” in WordPress? (5 Easy methods)
Method 1. Check Your Database Server
A database server contains all the connected clients. There are multiple things wrong on the web host’s end, such as:
Connections Exceed the Limit:
If connections exceed the set limit, the server will automatically drop them; your site will fail to fetch the requested data.
As a result, you’ll see an Error Establishing a Database Connection issue.
To resolve this issue, you can eliminate the backend issue with caching plugins like W3 Total Cache or WP Rocket.
Network Issue:
Sometimes, network issues also create the following error message.
To resolve this issue, you can contact your web host and ask them if the rest of the sites are facing the same issue.
If all the sites are experiencing the same issue, it means the issue is on the hosting provider’s end. They’ll resolve it on their own.
Insufficient User Permission:
Insufficient user permissions are another reason for getting the Error Establishing a Database Connection issue.
This issue can be quickly resolved by accessing the MySQL server using PHP.
To do so, go to the hPanel, select the website from the top menu, and click the Dashboard option.
On the website management page, select the File Manager option.
See the image below:
Go to the public_html folder and click the Create a New File icon on the left sidebar.
Now, create a new PHP file with any name.
Next, you must paste the following code into the file. For this guide, we’re using testing.php.
Note: Don’t forget to replace the MySQL database password and username with the actual value.
Here is the code for you:
<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Next, click the disk icon on the top right corner to save the file.
Now, open a new tab in your web browser and search for the file name and path after the domain like so:
domain.com/public_html/test.php
If there is no problem, your database user has sufficient permissions.
See the image below:
But if you fail, you’ll need to adjust the user permissions.
To do so, go to the hPanel > Databases > List of Current MySQL Databases And Users.
Now, select the three-dot icon and select Change Permissions. Next, check all the boxes and select Update.
When done, rerun the debug script to see if the issue is resolved.
Method 2. Check Your Database Login Credentials
You might also face this issue due to the incorrect credentials.
Your WordPress credentials are stored in the wp-config.php file. The WordPress configuration file contains essential settings, like database information.
You will be looking for the following lines in the wp-config.php file:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
Ensure the database name, username, password, and host information are correct.
To confirm it, you must go to your WordPress hosting account dashboard.
Note: We’re using Bluehost here, but the process will also be similar for other hosting panels.
After logging into your WordPress hosting account, select the Website tab in the left column and then the Advanced tab.
Go to Database on the same screen and click the Manage button.
This will open the MySQL Databases page in cPanel in a new window.
Your database name and username are in the ‘Current Database’ section. Copy and paste these details into a notepad to add them to the wp-config file.
Now, your database will be automatically repaired by the hosting panel.
When the process is completed, you’ll get a success message.
Now, visit your WordPress site to see if the error persists.
Method 3. Check Your Database Host Information
If your database login credentials, i.e., database name, username, password, etc., are accurate, you can also check if you use the correct database host information (the name of the host that has the database) in your WordPress configuration file (wp-config.php).
Usually, WordPress hosting companies use localhost as their database host.
See below:
define('DB_HOST', 'localhost');
But with managed WordPress hosting companies, you’ll get separate database servers.
In that case, your database host information will not be localhost.
So, you must contact your WordPress hosting company to confirm these details.
Method 4. Check and Fix Corrupted Files
Sometimes, corrupted plugins and theme files become the cause of displaying an “Error Establishing a Database Connection” issue.
You must check each plugin and theme file individually to fix this issue.
For example, if your plugin is causing trouble, go to the File Manager menu on your cPanel dashboard.
Next, select public_html > wp_content and go to the plugins folder. Right-click on the folder, then choose Rename.
Next, you’ve to add a new name for the folder.
If the plugin was causing the “Error Establishing a Database Connection” issue, it will fix it for you.
If the theme is the cause behind this issue, you can perform the same procedure for it, i.e., go to the File Manager menu on your cPanel dashboard > public_html > wp_content > themes folder > rename it.
It will fix the issue for you.
Method 5. Update WordPress Website URL
You can also try to update the WordPress site URL via phpMyAdmin in cPanel.
You can access phpMyAdmin from your hosting account dashboard. Next, select your WordPress database from the list and switch to the SQL menu at the top.
Next, you’re required to enter the following MySQL query:
UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
Note: You’re required to add your site’s URL and change wp_options to your table name, because you may have changed the WordPress table prefix.
This is what it looks like:
It will resolve the issue for you!
How to prevent the “Error Establishing a Database Connection” in Future?
You can easily prevent the Error Establishing a Database Connection” in the future by adopting proactive measures.
Here are some key strategies:
- Timely Backup: Regularly back up your database. It will help you restore it if anything goes wrong.
- Optimized Datase Tables: Use plugins like WP-Optimize or phpMyAdmin to clean up and optimize your database tables.
- Limit Database User Privileges: It is better to assign only necessary privileges to the WordPress database user, e.g., avoid using the `root` user.
- Update the Core Files: Keep your WordPress, themes, and plugins updated.
- Reliable Hosting Provider: Use a reliable hosting provider with good server uptime.
- Regular Checkups: Use tools that help you monitor server load and resource usage to find and resolve any issues satisfactorily and on time.
By following these strategies, you can ensure that your WordPress site remains stable and accessible.
Error Establishing a Database Connection FAQs
How do I check if my database credentials are correct?
You can quickly check if your database credentials are correct by accessing your site’s wp-config.php file via FTP or your hosting control panel. Now, verify the database name, username, password, and host. That’s all.
What should I do if my hosting server is down?
If your hosting server is down, contact your hosting provider immediately and ask about the server status. They will need to resolve the issue on their end.
What is error code 3024 in MySQL?
This error code 3024 in MySQL shows that a query has crossed the time limit (i.e., set by the server) and has been terminated.
How do you fix an error connecting to the database?
If you fail to connect to the database, you’ve entered the incorrect credentials, i.e., username or password. Because the server cannot authenticate the user, you must recheck and update the credentials to fix this issue.
Can a plugin or theme cause “Error Establishing a Database Connection” issue?
Yes, sometimes plugins or themes can cause database corruption or conflicts. As a result, you get this error.
How do I restore my site from a backup?
You can restore your site from a backup. All you need to do is access your hosting control panel. When done, make sure that the backup contains your files and the database. Follow the restoration process provided by your hosting provider. You can also use a backup plugin to restore a previous version of your website.
Should I contact a professional if I can’t fix the error?
If you cannot resolve the issue after trying the steps given in this guide, it’ll be best to contact a WordPress professional or your hosting provider for further assistance.
Final Thoughts
In summary, facing the “Error Establishing a Database Connection” is common, and we outlined 5 significant causes and their possible solutions to help you restore your site on time.
These troubleshooting steps will help you maintain your online presence.
That’s all for this article!
Here are the key takeaways:
- Method 1. Check Your Database Server
- Method 2. Check Your Database Login Credentials
- Method 3. Check Your Database Host Information
- Method 4. Check and Fix Corrupted Files
- Method 5. Update WordPress Website URL
You may also like to read our other valuable how-to guides, including: