The "fault establishing a database connection" is probably i of the most common and frightening errors that WordPress users can encounter. It'south definitely a close tie with the white screen of death (WSOD).  This error means your website is no longer communicating or has access to your WordPress database, and thus your entire website goes downward. This is not something to be taken lightly and you should endeavor and resolve this immediately equally this tin can directly affect your sales, traffic, and analytics.

Just don't worry, today we'll discuss some common scenarios that crusade this mistake and some piece of cake ways to get your site support and running in no time.

  • What is the Error Establishing a Database Connexion?
  • Common Scenarios That Cause This Error
  • How to Set the Error Establishing a Database Connection

What is the Error Establishing a Database Connection?

All of the information on your WordPress site, such equally post data, page data, meta data, plugin settings, login information, etc. is stored in your MySQL database. The just data that isn't stored in that location is media content such as images and your theme/plugin/core files such as alphabetize.php, wp-login.php, etc. When someone visits your website, PHP executes the code on the page and queries the information from the database, which then displays it to the visitor in their browser.

If for some reason this isn't working properly, you are left with the error establishing a database connexion message, as seen beneath. The entire page is blank because no data can be retrieved to render the page, as the connection is not working properly. Not but does this interruption the frontend of your site, but it volition also prevent you lot from accessing your WordPress dashboard.

However, visitors might not come across this error on the frontend correct away. That is because your site is about likely yet serving from cache until it expires. For instance, at Kinsta, all WordPress sites are buried for one hour by default. Therefore, if a site is still serving from enshroud it might announced fine to a visitor.

Error establishing a database connection in Chrome
Error establishing a database connexion in Chrome

At Kinsta, our back up team can increase the elapsing of your cache to say an hour or even a calendar week if you want. If you have a site that doesn't alter very often, this tin can really increase your site'south performance as information technology's not having to grab fresh files as often after the enshroud expires. And in cases like the above, the frontend of the site in almost cases (unless you have a script or role of your site breaking the cache) would stay up a lot longer.

When visitors try to access your site while this error is happening it will generate a 500 HTTP status lawmaking in your logs. This same condition code appears when there is an "internal server error". It essentially means that something went wrong on the server and the requested resource was not delivered. When everything is working properly your site volition generate a 200 HTTP condition code, which means everything is fine.

Error establishing a database connection 500 error
Error establishing a database connection 500 mistake

If you're a Kinsta client yous could too look at the 500 error breakdown report in MyKinsta analytics to come across if it's something that has been a reoccurring problem.

500 error breakdown
500 error breakdown

Common Scenarios That Crusade This Error

And then why exactly does this happen? Well, hither are a few common reasons below. And don't worry, we volition go into each of these more in-depth so you can know how to gear up them. Typically you can resolve this error in under 15 minutes.

  • The most common issue is that your database login credentials are incorrect. Your WordPress site uses split login information to connect to its MySQL database.
  • Your database is corrupted. With and so many moving parts with themes, plugins, and users constantly deleting and installing them, sometimes databases become corrupted. This can be due to a missing or individually corrupted table, or perhaps some information was deleted by accident.
  • You may have corrupt files in your WordPress installation. This tin can fifty-fifty happen sometimes due to hackers.
  • Problems with your database server. A number of things could exist wrong on the web hosts end, such as the database beingness overloaded from a traffic spike or unresponsive from too many concurrent connections. This is actually quite common with shared hosts as they are utilizing the aforementioned resources for a lot of users on the same servers.

How to Fix the Error Establishing a Database Connection

Before you start troubleshooting the error, we always recommend taking a WordPress site backup. A lot of the recommendations beneath involve manipulating information in your database, so you don't desire to brand things worse. You lot should always backup before trying to fix things on your WordPress site no matter how tech-savvy y'all retrieve y'all are.

You can utilise a popular WordPress backup plugin such as VaultPress or WP Time Capsule to backup both your files and your database.

You can never accept also many backups; no matter how tech-savvy you call back you are. 😉 Click to Tweet

If y'all are a Kinsta user, you can merely take reward of our one-click backup feature. Simply click into your WordPress site in MyKinsta, click on "Backups" and and then on "Support now."

Backup WordPress site
Backup WordPress site

You then also have the option to restore a backup to production or staging. Or y'all can manually backup your MySQL database using phpMyAdmin. In one case you take a successful fill-in, information technology is then fourth dimension to commencement troubleshooting your database connectedness problems.

ane. Check Your Database Login Credentials

The very first thing to practice is bank check to ensure your database login credentials are correct. This is past far the well-nigh common reason why the error establishing a database connexion message occurs. Especially right after people migrate to a new hosting provider. The connection details for your WordPress site are stored in the wp-config.php file which is generally located at the root of your WordPress site.

It contains iv important pieces of data that all need to be correct in order for the connection to successfully take place.

Database Proper name

          // ** MySQL settings ** // /** The proper noun of the database for WordPress */ define('DB_NAME', 'xxxxxx');        

MySQL database username

          /** MySQL database username */ ascertain('DB_USER', 'xxxxxx');        

MySQL database countersign

          /** MySQL database password */ define('DB_PASSWORD', 'xxxxxxxxx');        

MySQL hostname (server)

          /** MySQL hostname */ define('DB_HOST', 'localhost');        

To admission your wp-config.php file yous tin connect to your site via SFTP and browse to the root of your site. Or if you are using cPanel, y'all can click on "File Manager," scan to the root of your site, and correct click to edit the file.

Subscribe At present

cPanel file manager
cPanel file manager

Hither is an example below of what the file looks similar when opened.

wp-config.php credentials
wp-config.php credentials

You lot need to now check your current values against those on your server to ensure they are correct. Follow the directions below for cPanel and Kinsta users.

Bank check Database Credentials in cPanel

The first thing to check is the database name. To do this you will need to login to phpMyAdmin in cPanel under the Databases section.

cpanel phpmyadmin
cPanel phpMyAdmin

On the left-mitt side, y'all should see your database name at the bottom. Yous can ignore the "information_schema" database as this is something used by the host. You lot will and so want to compare that name against the DB_NAME value in your wp-config.php file. If they match, and then this isn't the problem. If they don't match, and then you need to update your wp-config.php file.

cPanel database name
cPanel database name

You can also verify that this is the correct database past ensuring that it contains your WordPress site'southward URL. To practise this, simply click on the database, so click into the wp_options tabular array (this might exist named something slightly different for security purposes, such as wpxx_options). At the top of the table, you will see values for your site's URL and name. If these match your electric current site you can rest assured you are in the right place.

Check site URL in phpMyAdmin
Check site URL in phpMyAdmin

If your database proper name was already correct and you are yet getting the fault establishing a database connectedness bulletin then you will too desire to check your username and password. To do this you lot will need to create a new PHP file in the root directory of your WordPress site, and input the following code. You can proper name it whatever you want, such as checkdb.php. Simply change the values of db_user and db_password with those that are in your wp-config.php file.

          <?php $test = mysqli_connect('localhost', 'db_user', 'db_password'); if (!$test) { die('MySQL Mistake: ' . mysqli_error()); } echo 'Database connectedness is working properly!'; mysqli_close($testConnection);        

Then scan to the file on your WordPress site: https://yourdomain.com/checkdb.php. If you get a "MySQL Mistake: Access denied" and so you know your username or password is wrong and you will need to go on to the side by side step to reset your credentials.

Access denied mySQL
Access denied MySQL

Beneath is the bulletin you want to see, "Database connection is working properly." But of form, if it was then you wouldn't be here. Be sure to delete/remove this file after you lot are washed testing.

Database connection working properly
Database connection working properly

So next you need to reset your username and password. In cPanel, click on MySQL Databases nether the Databases section.

cPanel MySQL databases
cPanel MySQL databases

Curl downward and create a new MySQL user. Try and pick a unique username and password so that they can't hands be guessed. The password generator tool they provide really works great. Then click on "Create User." You could alternatively also change the password on this screen for the current database user that already exists.

Create new MySQL user
Create new MySQL user

And so scroll downward and add your new user to your database. The next screen will ask which privileges you desire to assign, select "All Privileges."

Add user to database in cPanel
Add user to database in cPanel

Then take those new credentials and update your wp-config.php file. Yous will want to update the DB_USER and DB_PASSWORD values. You lot could also run the test file again from earlier. This should and so resolve your credentials upshot. If not, it could exist that you nonetheless have the incorrect hostname (DB_HOST). Some hosts use unlike values, see a list of some common DB host values. Typically this will just be localhost. Merely you lot tin can ever reach out to your hosting provider or check their documentation if you aren't sure. Some might as well use 127.0.0.1 instead of localhost.

If you have followed everything to a higher place and are still receiving the error establishing a database connectedness message, then proceed to the next troubleshooting steps below.

Check Database Credentials With Kinsta

If you are a Kinsta user, the process of checking your credentials confronting your wp-config.php file and the server are much easier! All yous need to do is click on the info section of your site, and at towards the lesser, you lot will encounter the database name, database username, and database countersign. By default, Kinsta uses localhost for the DB_HOST.

Kinsta database login credentials
Kinsta database login credentials

Then take those new credentials and update your wp-config.php file. You will desire to update the DB_USER and DB_PASSWORD values. This should then resolve your credentials issue. If you need to reset these credentials, only reach out to our support team. If y'all take followed everything above and are nevertheless receiving the fault establishing a database connexion message, then go along to the next troubleshooting steps beneath.

2. Repairing Corrupt Database

In some cases, it could exist that your database has become corrupt. This can occasionally happen (although not very often) as over time hundreds of tables are constantly added/removed by new plugins and themes. If yous try to login to your WordPress site'due south dashboard and are receiving the following error, it means your database is corrupt: "One or more database tables are unavailable. The database may need to exist repaired." Information technology is important to note that y'all might only come across this error on the back-terminate, whereas you lot run into the fault establishing a database connexion message on the frontend.

WordPress actually has a database repair mode which you tin can initiate. Simply add the following to the lesser of your wp-config.php file.

          define('WP_ALLOW_REPAIR', true);        
WordPress repair mode
WordPress repair way

Then scan to the following location on your WordPress site: https://yourdomain.com/wp-admin/maint/repair.php. You lot volition and so take the option to repair the database or repair and optimize the database. Since you are probably troubleshooting an outage on your site at the moment, we recommend going with the repair database choice equally information technology is quicker.

WordPress repair database
WordPress repair database

Later on running the repair of the database above, ensure that you remove the line of code y'all added to your wp-config.php file, otherwise anyone could run the repair. If you are running cPanel y'all can also run a repair from within the MySQL databases screen.

cPanel repair database
cPanel repair database

Or you could run a repair from inside phpMyAdmin. Simply log in to phpMyAdmin, click on your database, and select all the tables. Then from the dropdown click on "Repair table." This is essentially just running the REPAIR Table command.

Repair tables in phpMyAdmin
Repair tables in phpMyAdmin

And finally, your other option would exist to run the repair using WP-CLI with the following command:

wp db repair

Run into more documentation on usage in the WordPress developer resource.

If you are wanting to optimize your database, we take some great tutorials on how to optimize WordPress revisions for performance, along with how to catechumen your MyISAM tables to InnoDB. If you are nonetheless having issues on your site, and so proceed to the next troubleshooting step.

Suggested reading: How to Gear up the "MySQL Server Has Gone Away" Error in WordPress.

3. Fixing Corrupt Files

The next possible reason yous might be seeing the fault establishing a database connexion bulletin is that your files have become corrupt. Whether this originated from an upshot with transferring files via FTP, a hacker gaining access to your site or a trouble with your host, you tin quickly fix this. However, once more nosotros recommend taking a fill-in of your site earlier trying this.

You are basically going to replace the core version of WordPress on your site. You aren't touching your plugins, themes, or media, merely the WordPress installation itself. To do this, you will need to download a fresh copy of WordPress from WordPress.org.

Download WordPress
Download WordPress

Unzip this file on your estimator. Inside, y'all will want to delete the wp-content folder, as well as the wp-config-sample.php file.

Delete wp-content folder
Delete wp-content folder

So upload the remaining files via SFTP to your site, overwriting your existing files. This will replace all the problematic files and ensure you have fresh ones that are clean and not corrupted. It is recommended to clear your browser cache afterwards doing this. Then check your WordPress site to see if the error even so exists.

4. Issues With Your Database Server

If nothing above has helped resolve your event, then we highly recommend checking with your hosting provider as it could be an issue with your database server. For example, if there are also many concurrent connections to your database at once, it could generate the fault. This is because a lot of hosts have limits on their servers on how many connections are allowed at one time. Utilizing a caching plugin can assistance minimize the database interactions on your site. If y'all are a Kinsta customer, y'all don't need caching plugins, as we accept fast server-level caching in place.

This problem can happen a lot on shared hosts every bit someone else could theoretically be affecting your site. This is because shared hosts utilize all the same resources on servers. This is yet another reason why we always recommend going with a high-functioning managed WordPress host, so things aren't overcrowded. Information technology also ways the surroundings is typically fine-tuned to handle big amounts of traffic specifically to WordPress sites.

The knowledgeable Kinsta support team is always a click away on the bottom right side of the dashboard and available 24/7 if you demand help. And don't forget nosotros have representatives in to help you in multiple languages including English, Spanish, French, Italian, and Portuguese.

Contact Kinsta support
Contact Kinsta support

5. Restore Latest Backup

And concluding but not least, yous tin can always resort to a backup if needed. In some cases, this might be a faster manner to resolve the upshot if you lot aren't worried about losing whatsoever data between when your last backup was taken. Many hosts accept their own backup restore process. Recollect that you lot might need to restore both your database and your files.

If you are Kinsta user you tin can easily restore a backup of your site within the Backups department of your site. Only choose the time you desire to revert to and click on "Restore to." You can then select whether you want to restore to Staging or your Live site.

Restore WordPress backup
Restore WordPress fill-in

You will and then be prompted to confirm the restore. Simply enter your site proper noun and click on "OK." It also creates a fill-in at the time of restore and so you can disengage the restoration if needed.

Confirm WordPress restore
Confirm WordPress restore

Summary

As y'all can see at that place are quite a few ways to set up the fault establishing a database connexion in WordPress. The nigh common being invalid credentials in the wp-config.php file. Checking to ensure those are correct is the best place to start. The last thing you want for a website is to experience downtime. So hopefully one of the steps above helped you get your site back up and running. Recollect, you lot tin e'er restore your site from a backup if needed.

Accept you experienced the fault establishing a database connection message on your site? If so, were you able to resolve it? Let usa know beneath in the comments.


Save time, costs and maximize site performance with:

  • Instant help from WordPress hosting experts, 24/vii.
  • Cloudflare Enterprise integration.
  • Global audience reach with 29 information centers worldwide.
  • Optimization with our congenital-in Application Performance Monitoring.

All of that and much more, in one plan with no long-term contracts, assisted migrations, and a 30-day-coin-dorsum-guarantee. Check out our plans or talk to sales to find the programme that's right for you.