How to fix Error establishing a database connection in WordPress site

What is ‘Error establishing a database connection’ error in WordPress?

In this post, we are going to discuss on “How to fix Error establishing a database connection in WordPress site”. You will be guided here with easy steps/methods to resolve the issue. Let’s starts the discussion.

‘Error establishing a database connection’ error: It is common error usually appeared with White Screen of Death (WSOD) on browser while trying to access or visits certain WordPress website. This error indicates the website you are trying to visit is no longer communicating or has access to WordPress Database. Several users reported about this error and explained they are unable to access certain WordPress website due to this error.

If you are facing this error with your own WordPress site or if you are WordPress site owner, then you can read & follow our instructions to resolve this error. This issue can be occurred due to some problems in your WordPress site’s database like WordPress site is unable to make connection to the database. The possible reason behind this WordPress error can be incorrect database information in your WordPress settings, corrupted database, or in irresponsive database server.

The information on your WordPress site including post data, page data, meta information, plug-in settings, login information, etc is stored in your MYSQL database. On other hand, the data that is not stored in MYSQL is media content like images, themes/plug-in/core files such as index.php, wp-login.php, etc. When you visit or visitors visit the your site, PHP executes the code on page and queries the information from the database, which then displays it to visitor or in your browser.

However, ‘Error establishing a database connection’ error can be occurred if for some reason this (Database) is not working properly. This error is appeared with blank screen because no data can be retrieved to render the page means connection is not working properly. This error blocks you from accessing your site frontend page means page for the visitors, and also prevents you from accessing WordPress Dashboard of your site.

If this error is limit to particular browser or computer, it means there is some issue in browser or computer itself that’s why the error is appeared. In such case, you can try to fix the issue by clearing browser cache and cookies, or cache of site that is causing this error. If error is still persist, then you can reset your browser in order to resolve the issue, or resolve your network settings and connections issue.

Reason behind ‘Error establishing a database connection’ error:

  • This issue can be occurred if WordPress database login credentials are incorrect. It might possible you have changed login credentials of you WordPress database and you are trying to access the database with old login credentials.
  • Corruption in WordPress files could be another reason behind this error.
  • Corruption in database could be reason behind this problem. This might be a result of rough plugin messing up the database internally, hacker’s attack, a theme failing, and many more.
  • Issue with database server or database is down could be another possible reason behind the issue. There is web server that stores your website files and run on different machine than the database, and that separate database server can simply be down for whatever reason.
  • Heavy traffic on site could be another possible reason behind this error. Your WordPress site database might be unresponsive due to high spike in traffic.

How to fix Error establishing a database connection in WordPress [For WordPress site owner]?

Method 1: Check your WordPress Database Credentials

One core reason behind this error can be incorrect database credentials you are trying to enter to login WordPress database. If you have recently moved your WordPress site to new host, then this could most likely reason. WordPress database credentials are stored in wp-config.php file. This file contains important WordPress settings in including database information.

You can check the information for database name, username, password and database host in wp-config.php file in WordPress in order to make sure these information are correct. To do so, login into your hosting account or WordPress hosting account and click on ‘MYSQL’ databases under ‘Database’ section. This will show you database management page in your hosting dashboard where you can find out your database name and username.

Under your database name and username, click on ‘Change password’ next to user. Once you have confirmed your database name, username and password, you can change that information in your wp-config-php file if required. Once done, try visiting your site again and check if the error is resolved.

Method 2: Check your database host information

You should make sure you are using correct database host information. Most WordPress hosting companies use ‘localhost’ as your database host. However, some WordPress hosting companies use separate servers on host databases. So, you should check if your database host information is localhost or sometime else. You can contact your WordPress hosting company to confirm your database host information.

Method 3: Repair WordPress database

Sometimes, you might face different error on your WordPress site wp-admin page like ‘One or more database tables are unavailable. The database may need to be repaired’ message, then you need to repair your database.

To do so, open ‘wp-config.php’ file and add ‘define (WP_ALLOW_REPAIR, true);’ line before ‘That’s all, stop editing! Happy blogging’ line, and once done save the file.

After that, visit ‘http://www.yoursite.com/wp-admin/maint/repair.php’ page in your browser. On the opened page, go to ‘Repair Database’ or ‘Repair and optimize the database section and repair your database. Since you are probably troubleshooting an outage on your site at the moment, we recommended going with the repair database option as it is quicker. Once repairing process is finished, you should remove the line you added in using this method in wp-config.php file. After that, retry accessing the page and check if the error is resolved.

Method 4: Check your databases server status

You should make sure your database server is up and running properly. This issue could be occurred due to heavy traffic on the server and your host server can’t handle the load especially when you on shared hosting. If you have other sites running on same server, then you can check if those sites to confirm that your SQL server is down or running. To check this, you can go to your hosting dashboard and try accessing phpMyAdmin page and connect the database.

If you can connect, then you need to verify your database user has sufficient permission. You can create a new file with ‘testconnection.php’ file and paste the following code in the created file.

<?php

$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);

if (!$link) {

die(‘Could not connect: ‘ . mysqli_error());

}

echo ‘Connected successfully’;

mysqli_close($link);

?>

Here, you need to replace your username and password in above texts. You can now upload this file on your website and access it via your browser, and check if script is connected successfully and check if you are not facing Error establishing a database connection anymore. If the error is still persist, you can go back to your wp-config.php file to make sure that everything there is correct.

Method 5: Update WordPress site URL

Another possible way to fix the issue is to update WordPress site URL. To do so, you need to access your phpMyAdmin page from your hosting account dashboard and select your WordPress database. After that, click on ‘SQL’ menu on top and enter the following command in required field.

UPDATE wp_options SET option_value=’YOUR_SITE_URL’ WHERE option_name=’siteurl’

Here, you need to provide your own site URL and change wp_options to your own table name as you may have changed the WordPress table prefix.

Method 6: Reboot web server

Another possible way to fix the issue is to reboot dedicated servers, local server and virtual private servers (VPS) in order to restart your web and database server. Once rebooted, please check if the error is resolved.

Method 7: Contact your WordPress web hosting company

If the issue is still persist, then you can contact your WordPress web hosting company and ask them to resolve the problem by reporting your problem. Or you can also contact WordPress developers that can help you fix the issue for reasonable rates.

Conclusion

I am sure this post helped you on How to fix Error establishing a database connection in WordPress site with several easy steps/methods. You can read & follow our instructions to do so. That’s all. For any suggestions or queries, please write on comment box below.