How to Fix the Error Establishing a Database Connection in WordPress
WordPress might be one of the most popular content management systems, but it’s not without its faults. One of the most common issues WordPress users encounter is the Error establishing a database connection in WordPress.
The Error Establishing a database connection is a serious issue that results in the complete inability to access your site. As a top priority, it’s important to fix this Error as soon as possible. However, if you can’t find your way around WordPress to fix the error, troubleshooting it becomes difficult.
In this blog, we’ll look at why Error establishing a database connection occurs and, more importantly, how you can fix it.
How to Fix Database Connection Error?
- What is the Error Establishing a Database Connection?
- Causes of WordPress Error Establishing a Database Connection
- Four ways to fix the Error Establishing a Database Connection in WordPress
- Wrapping Up
What is the Error Establishing a Database Connection?
The Error Establishing a Database Connection error is a very common WordPress error. It means that WordPress cannot connect to your database.
WordPress is written in PHP and uses MySQL as its database system. When a user visits your site, special PHP commands are used to fetch your site’s information from the database. This information is then displayed on the user’s browser.
The “Error establishing a database connection” message appears on a WordPress web page when there is no data to retrieve and display.
Fortunately, this error message only appears on the back end, not the front end, as long as the website’s cache is available. This database connection problem still restricts your access to the WordPress dashboard. In light of this, it’s crucial to identify the Error’s root cause and address it.
Causes of WordPress Error Establishing a Database Connection
The Error can happen for a number of reasons, including:
- Your database credentials are incorrect
Error Establishing a Database Connection is most commonly caused by incorrect login credentials for your database in WordPress. This includes the database name, username, or password, which is different from the login details used to access your site. Remember to keep these credentials separate for organizational and security purposes.
- Your database server is down
If any issues come up with your host or server, WordPress may be unable to access the database, resulting in this error. Some potential problems could be data loss, outages, or even hardware failures.
- Your WordPress files are corrupt
The 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 quite resilient, mistakes can still happen.
Now that you know the different reasons you might see the “Error Establishing a Database Connection” Error in WordPress, let’s look at some ways you can fix it.
Four ways to fix the Error Establishing a Database Connection in WordPress
Ensure that your domain and hosting are in order
When you change hosting companies, your files will be transferred automatically to the new host. However, database failure will result if your domain is not pointing to the host where the database is stored.
To do this, use the WHOIS lookup tool to check name servers in your hosting control panel. Users of Hostinger can do this by logging into the CPanel dashboard and going to Accounts -> Details. Your domain is pointed appropriately if both nameservers agree with one another.
If the results differ, tell your domain registrar to modify the nameservers. Get a list of the nameservers from your hosting dashboard, and then enter them into the control panel of your domain registrar.
You can also use the A record to point to your domain name. A particular kind of DNS record links your domain to the appropriate IP address. Remember that the A record only directs traffic to static IP addresses.
To complete this alternative, locate your present IP address or the A record value and change them with the ones from your new web host. You can use a local computer console or an online DNS checker to perform the task.
Here’s an example of what the website’s A record value might look like: 123.4.567.89. If this number matches your new hosting IP address, your domain name has already been pointed to the correct location. If the values don’t match, you’ll need to update it from your domain registrar’s control panel.
Check if your database server is working correctly
When your MySQL server is unavailable, the notice “Error Establishing a Database Connection” may also appear on your WordPress website. Although there are numerous potential causes for this error, the issue is typically too many connections. There is a limit on how many clients can be connected to a database server simultaneously.
When it exceeds that limit, it could be unable to manage more connections. For this reason, getting in touch with your web host is recommended to have your MySQL server checked.
You can check to see whether your MySQL server is down on other websites that are hosted on the same server as your own. While waiting, if you only host one website, connect to MySQL using PHP to see if the WordPress database user has the necessary permissions.
Access your public HTML folder from your hosting management panel. There, make a new PHP file with whatever name you like. We’ll call the file test.php in this example.
Then, paste the code given below:
<?PHP
$link = mysqli_connect('localhost', 'username', 'password'); if (!$link) {
die('Could not connect: ' . mysqli_error()); }
echo 'Connected successfully';
mysqli_close($link);
?>
Your credentials should be used in place of the login and database password. Finally, upload this file to your website so visitors can access it.
If it connects successfully, it signifies that your user has the necessary rights, and the “Error Establishing a Database Connection” warning is caused by something else.
Due to the sharing of resources, a shared server website typically has database issues. Using plugins like W3 Total Cache or WP Rocket to do website caching is a fantastic way to reduce the load on your server.
Find if there are any corrupted files
The cause of “Error Establishing a Database Connection” may be corrupted files. One of the causes of file corruption is a broken theme or plugin. Checking each one separately will help you determine which element is the problem. If the error shows the following while activating a new theme, your theme may be the cause.
To solve this issue, switch to a different theme from your WordPress dashboard’s Appearance >> Themes.
The “Error Establishing a Database Connection” issue can be caused by 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.
Here are the steps to disable all the plugins on WordPress:
- Once logged in, you need to go to the plugins section.
- n the plugins section, you will see a list of all the plugins installed on your WordPress site.
- To disable all plugins, select the checkbox next to each plugin.
- Once all the plugins are selected, you need to click on the “Bulk Actions” drop-down menu and select “Deactivate”.
- Finally, click the “Apply” button to deactivate all the plugins.
Disabling all plugins that you have installed will help you get rid of the Error. You can see if that helps resolve the Error. If not, proceed to the next step.
Repair WordPress database
If you are encountering an error message such as “One or more database tables are unavailable. The database may need to be repaired, ” you need to work on getting the WordPress database repaired.
You can change your WordPress config file to make the WordPress database work properly by adding the following line in your wp-config.php file. Just make sure to add it before the ‘That’s all, stop editing! Happy blogging’ line in wp-config.php.
1 define('WP_ALLOW_REPAIR', true);
After adding the line, you can view the settings by visiting the repair page: http://www.yoursite.com/wp-admin/maint/repair.php. This will show you all of the available options for repairing your site.
Note: You don’t need to be logged in as a user to access the database repair page. Once you’re done repairing and optimizing your database, remember to remove this code from your wp-config.php.
Wrapping Up
If you’re running into an error establishing a database connection in WordPress, we hope the above solution has helped you resolve the issue. These tips should also help you prevent similar issues from occurring in the future. Tech support and customer service are expensive but also crucial to business success.
There’s no need to feel discouraged if you can’t seem to fix the issue yourself – you can always get help from an expert. Contact WordPress support and maintenance experts like stylemix.net to get it fixed quickly.
We provide full WordPress support and maintenance, resolving bugs, doing daily audits, and providing real-time security monitoring. If you want to improve the consistency and speed of your website, you can also hire a committed team of experienced WordPress developers.