How To Fix Error Establishing a Database Connection In WordPress (2024)
Learn how to fix the Error Establishing Database Connection with multiple solutions on your WordPress site with this easy guide.
How To Fix Error Establishing a Database Connection?
- Verify your database credentials using phpMyAdmin.
- Repair your database
- Restore your WordPress files.
- Load a backup.
- Contact your hosting provider.
Overview
WordPress is one of the best web platforms, though that does not mean it does not have any difficulties. The Error Establishing a Database Connection is one of the most common errors you can encounter in WordPress. It can be hard to solve sometimes, though, in this tutorial, we will show you how to solve it.
Before we can show you how to solve this issue, we must understand how it works. The core of this problem is that your website cannot connect to your database. Your database and WordPress use this connection to run your site. If this connection fails for some reason, then your site will break. If we want to know how to fix this, we must understand what a database is.
Simply put, a database is a separate place where all of your WordPress files and data are stored. The database collects and reserves all of your site’s data except for media, theme, and plugin files. The database stores everything from metadata to settings. When someone accesses your site, your site executes code, and the information from the database is sent to the user.
The connection to the database that allows WordPress to view, edit, access, and transfer data is required for your website to work. If something in your site causes this connection to fail, then your entire site will down. Although your site will be down, some users may be able to access it because of server caches.
Many things can cause this problem, from incorrect credentials to a security breach. Most of these causes have simple solutions, though some take a lot of time and require you to contact your host. The main thing to note is that you should take precautions and not panic. In the next section, we will show you the causes of this problem.
Possible Causes
Your WordPress site uses a name, username, password, and hostname to connect to the database. These credentials can be found in your wp-config file. If any of these credentials are incorrect, then your site will have trouble connecting to the database. This one of the leading causes for the Error Establishing a Database Connection.
Another thing that can cause Error Establishing a Database Connection is too much traffic. Excessive traffic may seem like a good thing at face-value, but it can create problems if your site cannot handle it. Traffic spikes can overwhelm your database very easily. Too much traffic is usually easily fixed by contacting your host.
Corrupt files in your WordPress server or database can both be causes for Error Establishing a Database Connection. If you have a theme or plugin installed containing corrupt files, that can be the root of the problem. A corrupt database is also a cause but is much less common. Thankfully, corrupt files and databases can both be easily solved.
Often, an outdated or buggy plugin can cause this error to happen. The same can happen with a theme. If you want to prevent problems like this, then you should have well-coded and updated themes and plugins. Labinator offers some of the best plugins and themes on the market for its price.
The last thing that can be a root for this error is a database server issue. A database server issue is usually something on your web host’s end that has to be solved by them. There is little you can do if this is the cause because the host has to handle this. Thankfully, you will not have to do much work since the host will solve this.
Preparations
Before you can try the different solutions to solve this problem, you need to make a few preparations. These preparations include creating a backup of your WordPress site. No matter what the cause is, backing up your site is crucial because it will give you a point to restart if you mess up. You should always make a backup before you make any major changes to your site.
The next thing you will need to do is have cPanel or an FTP ready. These will allow you to edit, access, and view your WordPress files. Accessing your WordPress files is needed for you to use some solutions. If you do not have an FTP client already set up, then FileZilla is a great place to start.
The last thing you will need to solve Error Establishing a Database connection is your hosting provider account. You need this account because they provide crucial tools like phpMyAdmin and MySQL. If you may also need to contact your host, so this step is vital. Once you have everything here done, you can now move onto solving the problem.
Though, if you want professional help on matters like this, I would recommend the WordPress Autopilot Service by Labinator. Their WordPress experts will give you the support and services you need to solve problems like this one. They can get your site up and running in only a couple of days, without you having to do anything. They are a perfect solution for any WordPress problem.
We also offer WordPress technical support services that covers debugging any problem on your WordPress website.
Fixing Credentials
The first method that we will be using to fix Error Establishing a Database Connection is verifying the credentials listed in your WordPress site. To do this, we must compare the data in your WordPress files to the data in your database. You need to have access to an FTP and cPanel, or whatever your hosting provider uses, to access these files.
This fix takes some time and requires you to be very careful, though it will be the easiest fix for this error. Wrong credentials are the most likely cause for Error Establishing a Database Connection. Once you have everything you need, follow these steps below:
Open your FTP/cPanel and open your WordPress files.
Open the file named wp-config.php.
Find code inside the file that looks like this:
The “xxxxxx” there is used as a placeholder for your credential.
This section of code is what WordPress registers as your database name, username, password, and hostname. We will be using the data stored in your database to compare the username, name, hostname, and password. Once you know what the values are in your wp-config file, proceed to the steps below:
- Log in to your host account and open cPanel.
- Find and open phpMyAdmin.
- Look at the menu on the left.
- Make sure the name of the database there and the database name found in the wp-config file match.
- Then go to the database server section.
- Make sure the hostname there matches the hostname found in the wp-config file. It will most likely be “localhost”.
Once you have completed these steps, we can verify if the problem was with the database name and the hostname. Try to open your site again and see if you still get the same error. If you do not get the same error, then congratulations! If you get the same problem, then we must correct the username and password. Follow these steps:
- Open cPanel/FTP and find your WordPress files directory.
- Create a new PHP file with any name you would like.
- In the file, put the following code:
$test = mysqli_connect('localhost', 'db_user', 'db_password'); if (!$test) { die('MySQL Error: ' . mysqli_error()); } echo 'Database connection is working properly!'; mysqli_close($testConnection);
Once you have inserted this code, replace “db_user” and “db_password” with the values found in wp-config. Then replace “localhost” with the correct value you got from the steps before. This code will run a final test that will show us if your credentials are the problem or not.
To run the test, put https://yourdomain.com/name.php into your browser and replace the domain name and file name with what you use. Then hit enter and open the URL. If the page displays a message saying “MySQL Error:” then we have to reset your credentials.
If the page displays a message displaying “Database connection is working properly.” then credentials are not the problem. If this is the case, then you need to try one of the other solutions listed below. Before you try another solution, make sure to delete the PHP file if your test succeeded.
To run the test, put https://yourdomain.com/name.php into your browser and replace the domain name and file name with what you use. Then hit enter and open the URL. If the page displays a message saying “MySQL Error:” then we have to reset your credentials.
If the page displays a message displaying “Database connection is working properly.” then credentials are not the problem. If this is the case, then you need to try one of the other solutions listed below. Before you try another solution, make sure to delete the PHP file if your test succeeded.
To reset your credentials, follow the steps below:
- Open your cPanel.
- Find and open the MySQL Users section.
- Create a new SQL user.
- Make sure to use a secure username and password.
- Finalize your credentials and go to the next page.
- Add your user to the database and remember to use the right database name.
- Give your user “All Privileges” when adding it to the database.
- Finish creating and adding the user, then exit cPanel.
- Open your wp-config.php file again and replace the old credentials with the new ones.
- Save the file and exit.
After completing those steps, run the PHP file once again. If your database connection now works, then you are good to go. If your database connection fails, you should contact your hosting provider and ask for further support. Now open your WordPress site again and see if you get the “Error Establishing a Database Connection“.
If the problem persists, then you need to try another one of the methods listed in this article. The rest of the solutions are much shorter and simpler, so you should have no trouble. Make sure to delete the PHP file we created for testing when you are finished.
Repairing The Database
The following method involves us solving the Error Establishing a Database Connection by repairing your database. This solution is needed when the cause is a broken or corrupt database. This method will repair and restore your database by fixing any corrupt files or data.
WordPress has a built-in tool you can use to repair your database. All corrupt files and data become restored, so your database can function properly again. If your database corrupt, then this message will display, “One or more database tables are unavailable”.
Database tables store important data, settings, and metadata for your site’s plugins and themes. Most of the time, buggy themes/plugins can cause this problem. To initiate a repair, we must enable the repairing tool by editing some code. Follow these steps below:
- Open your FTP or cPanel.
- Navigate to your WordPress file directory.
- Open your wp-config.php file.
- Add this code to the bottom of the file:
define('WP_ALLOW_REPAIR', true);
After inserting that code, save and exit the file. Then open this URL in a browser, yourdomain.com/wp-admin/maint/repair.php. Replace “yourdomain” with the name of your website’s domain. Then open the URL, and a screen resembling this one should appear.
On this page, you will choose what type of repair you want to do on your website. You can either repair your database or repair and optimize your database. I would recommend choosing the first repair option because we do not need to optimize, and it takes much less time to repair.
After the repair process is finished, open your site again and try to see if the error persists. If it does, then you need to try another one of the solutions. You can also repair your database using phpMyAdmin. To do this, you need to open phpMyAdmin, select a database/table, right-click, and select the Repair option listed there.
MySQL also offers a similar way to repair your database and its contents. Open up the section for MySQL Databases through your cPanel and find the repair section. Just type in the name of your database and hit repair. Just make sure to delete the line from the wp-config we added earlier when you finished repairing.
Restoring WordPress Files
The next thing you can do to solve the Error Establishing a Database Connection is to fix any corrupt files in your WordPress installation. This will involve you replacing your WordPress installation’s files with new ones. This method will give you new files that will overwrite any corrupt, broken, or old files.
To do this, follow the steps below:- Download a new installation of WordPress from here.
- Delete the wp-content folder.
- Delete the wp-config-sample and wp-config file.
- Open your FTP or cPanel to the WordPress root directory.
- Overwrite any existing files that are in the folder.
- Try to access your site again and see if it works.
If this does not work, you should back up the wp-config file and replace it with a downloaded copy. After doing that, reopen your site again and see if the problem persists. The last thing you can do to solve this error is loading up a backup of your site from a previous date. A simple backup might fix your site.
Contact Your Host
If all of the previous solutions didn’t work for you, then the best thing to do is contact your host. A lot of the time, a problem with your hosting providers may cause this issue. They are the ones who are hosting your information, so the problem may be something on their end. The hosting provider will probably be able to solve the issue.
To make sure you don’t encounter problems like this one, you should have a good host. I would highly recommend Host Armada because they provide the best bang for your buck! They offer blazing-fast performance with their cloud-SSD technology along with their quick support team of experts that can help you through problems like this one.
The Perfect Solution
I hope this tutorial helped you learn about the Error Establishing a Database Connection. Just remember to take caution when doing everything in this tutorial. If you want more in-depth WordPress guides like this one, then Labinator’s blog is the best place to go to. They offer tutorials and guides on pretty much everything WordPress related.
Want an all-in-one package for all of your WordPress solutions? Labinator features countless stunning themes, feature-rich plugins, expert services, and more, all for affordable prices. They also give you a 30-day money-back guarantee that comes with 24/7 customer support and lifetime updates.
WordPress Guides
Best Collection Of WordPress Guides & Checklists
Our complete and latest collection of WordPress guides and resources that cover everything you need to get the most out of your WordPress website.
Download Them All!
All of our guides will be sent to your inbox