You're working on your website, and suddenly you get an error message like this: “Error Establishing A Database Connection.” This error is caused by a specific problem with the database settings in WordPress.
If you don't eliminate this error, your website can lose visitors and become inaccessible, harming its reputation.
In this guide, I'll educate you on quickly eliminating this problem.
What Is the “Error Establishing a Database Connection” Problem in WordPress?
Incorrect database information in your WordPress settings, a faulty database, or an unresponsive database server all contribute to the “Error establishing a database connection” problem.
WordPress' great features are all powered by PHP and MySQL, two of its major technologies.
- WordPress files are written in a programming language called PHP. It uses PHP to query the database and retrieve the appropriate data when a visitor arrives at your website. It is displayed as the entire page.
- Database technology is MySQL. Your WordPress website's content is stored in a MySQL database. This contains your pages and articles and the smaller components like your site's title, widgets' layouts, color preferences, etc.
The MySQL database serves as its repository no matter how little or how much information your website contains.
WordPress retrieves the needed data from the MySQL database using PHP commands, displays it on the screen, and then connects to the MySQL database using PHP commands. However, it will display an error notice that reads, “Error Establishing a Database Connection,” if it cannot access your site's database.
Why Is This Error Caused?
When a WordPress database connection error occurs, it happens due to any one of the following reasons:
- Incorrect login information for a database.
- Damaged database.
- Corrupted fundamental WordPress files.
- Issues with the site server.
Suppose you're unsure which of these causes could be causing your problem. In that case, contacting your web host company for help resolving this issue is best before proceeding further with this article!
Incorrect Login Information for a Database
The simple fact that WordPress has the wrong login information for your database may be the most frequent reason for the error Establishing a Database Connection. This could be the username, password, or database name.
Login credentials are different from those used to access your website.
Corrupted Database
A MySQL database used by WordPress houses a lot of data. The mistake in question may appear if any part is lost or corrupted.
The database can get corrupted through manual intervention, but it can also happen naturally during regular use. Even though MySQL is exceptionally resilient, mistakes can still happen.
Corrupted Fundamental WordPress Files
The core WordPress files that make up your site might also become corrupt. Even though these files are not part of the database, they occasionally produce the same problem.
Issues With the Site Server
WordPress will display this error if it cannot connect to your host or server (if you are self-hosting) to query the database. Outages, data loss, and hardware malfunctions are just a few of these issues.
How to Fix the “Error Establishing a Database Connection” Problem? (Step-By-Step Guide)
Step 1: Check The Database Credentials.
The first step to fixing the error is to check the database credentials in your WordPress settings. When you migrate a site from one host to another, you should ensure that each database has the correct username and password. This information is stored in a wp-config.php file.
To access this data, you can use an SFTP client (Secure File Transfer Protocol) or access it directly via your host's file manager.
I'm using Hostinger to access the wp-config.php file via File Manager in this example.
Login to your hPanel to access the file manager.
Clicking on file manager will take you to a new tab containing your website's essential files. From here, move into the public_html folder.
Scroll down, and you'll find the folder wp-config.php. Click on download to access the data.
Once you download the file, check the data and take note of the following information.
- DB_NAME – database name
- DB_USER – username
- DB_PASSWORD – password
- DB_HOST – database host
After noting these details, head back to your hPanel and access the “Databases” folder.
Now from the list of “List of Current MySQL Databases And Users,” match your details with the ones stored in your wp-config.php. Make sure the details are the same.
Modify your wp-config.php file if your login data differs from your MySQL database settings. Edit the database name and user name if the information differs from the MySQL database details.
Now, save the changes and upload the file back to wp-config.php. Refresh the page
Refresh WordPress after making the necessary changes to see if the “Error Establishing a Database Connection” on the website has been fixed.
The database password might still not be correct if the problem still occurs. You should reset your MySQL password in this situation.
Here's how you can reset your password:
Click on your existing database in the List of Current MySQL Databases and Users box, then choose Change Password.
Click Update after entering the new password.
Change the database host information in the wp-config.php file if your database username and password are correct.
Suppose you still get the “error establishing a database connection” warning. In that case, I suggest contacting your web hosting provider to confirm your database host information.
Step 2: Repair Your WordPress Database.
You should try repairing the database to address potential corruption if you still see the error message.
Data loss from plugins or frequently added or removed themes may cause this.
If your WordPress dashboard displays a notification saying, “One or more database tables are inaccessible,” you might have a corrupted database. It is necessary to repair the database in such cases.
You can quickly fix this issue by adding the following code to the bottom of your wp-config.php file:
define('WP_ALLOW_REPAIR', true);
Access your file manager and download the wp-config.php file again to make the edits.
Input the code as shown in the image and save the file. Go back and upload the edited file to wp-config.php.
Next, open your browser and navigate to http://www.your-site-domain.com/wp-admin/maint/repair.php.
You will have two options for fixing your database; pick the best one.
Check if your WordPress site is back to normal after finishing the repair. If so, head to your wp-config.php file immediately and delete the function. Anyone can view the repair page without logging in, stopping website misuse.
Step 3: Fixing Corrupted Fundamental WordPress Files.
Your WordPress core files may have become corrupt, which is a potential cause of this error.
One of the causes of file corruption is a malfunctioning theme or plugin. Checking each one separately will help you determine which element is the problem.
If the error manifests after activating a new theme, your theme may be the cause.
You can change your theme by logging in to your WordPress dashboard. Go to Appearance > Themes and choose your desired theme.
The error may also arise due to a corrupted plugin if changing the theme doesn't resolve it. To identify the offender in this situation, try disabling all your plugins and then re-enabling them one at a time.
Step 4: Solving Site Server Problems.
When your MySQL server is unavailable, the statement “Error Establishing a Database Connection” may also appear on your WordPress website. Although there are numerous potential causes for this error, this issue typically has the highest potential.
There is a limit on how many clients can connect to a database server simultaneously. When that limit exceeds, it could be unable to manage more connections. For this reason, I suggest getting in touch with your web host to have your MySQL server checked.
On other websites that are stored on the same server as your own, you can test to see whether your MySQL server is down.
If you only host one website, you can use PHP to connect to MySQL and see if the WordPress database user has the proper permissions.
Access your public_html folder from your hosting management panel. There, make a new PHP file with whatever name you like. I named it review.php in this example.
In this file, paste the following code:
<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Change the username and password with your login credentials and save the file.
After saving the file, access it through a new tab. If it shows a message of “connected successfully,” it means something else is causing your error.
Pro Tips to Get Rid of “Error Establishing a Database Connection” Problem
If, after trying everything, your error didn't resolve, try these three tech-savvy tips.
- Reboot your web server.
- Update your WordPress website URL.
- Contact your host's customer support.
1. Rebooting the Web Server
Rebooting a server is an option for users of virtual private servers, dedicated servers, and local servers. Your web and database server will restart once you reboot, which might fix some issues. You can use a root account and SSH clients like Bitvise or Putty.
In some situations, you might have to restore your database and WordPress files from a backup.
2. Updating the Website's URL
You might need to modify your site URL in the database if you just migrated for any reason (like switching to a new domain name or changing to HTTPS).
Navigate to phpMyAdmin from your hosting dashboard to accomplish this. Access your WordPress database and select the SQL tab.
Paste the following code in the given area.
UPDATE wp_options SET option_value = replace(option_value, 'http://www.old-domain.com', 'http://www.new-domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.old-domain.com','http://www.new-domain.com');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.old-domain.com','http://www.new-domain.com');
You must replace your old domain with the new one and click on Go.
3. Contact Customer Support
You should ask for assistance if you don't feel confident performing troubleshooting or trying everything. Consider reaching out to your host, and they will help you out with this. Else, you can try communities on Quora or Reddit to discuss your problem.
Concluding the Thoughts!
It is annoying to receive the “Error Establishing a Database Connection” problem on your WordPress website. Your website won't load in such a situation, putting visitors and customers at risk.
Your website cannot connect to the server, which results in this database error. There could be several causes, including incorrect login information in the wp-config.php file or server overload.
Be careful with your WordPress database credentials! It will be tough for you if you don't know how to fix this problem. You have to find the root cause of this problem and then sort all related issues one by one until you get rid of the error.
Laiba Siddiqui is an SEO Content Writer who’s helping Tech businesses build brand image through content. Her work has been featured on Hackernoon and Thrive Global.
Comments