The "mistake establishing a database connection" is probably one of the nearly common and frightening errors that WordPress users tin run into. It's 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 down. This is not something to be taken lightly and you should endeavor and resolve this immediately as this can directly affect your sales, traffic, and analytics.

But don't worry, today nosotros'll hash out some common scenarios that crusade this mistake and some easy means to get your site dorsum up and running in no time.

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

What is the Error Establishing a Database Connexion?

All of the information on your WordPress site, such as mail data, page information, meta data, plugin settings, login information, etc. is stored in your MySQL database. The only information that isn't stored at that place is media content such every bit images and your theme/plugin/core files such every bit index.php, wp-login.php, etc. When someone visits your website, PHP executes the lawmaking on the folio and queries the information from the database, which then displays information technology to the visitor in their browser.

If for some reason this isn't working properly, y'all are left with the error establishing a database connexion message, as seen beneath. The entire folio is blank because no data can be retrieved to render the page, every bit the connection is non working properly. Not but does this break the frontend of your site, only information technology will also prevent you from accessing your WordPress dashboard.

Even so, visitors might not come across this error on the frontend right away. That is because your site is most likely even so serving from cache until information technology expires. For instance, at Kinsta, all WordPress sites are cached for one hour by default. Therefore, if a site is still serving from cache information technology might appear fine to a visitor.

Error establishing a database connection in Chrome
Fault establishing a database connection in Chrome

At Kinsta, our support team can increase the duration of your cache to say an hr or fifty-fifty a week if you want. If y'all have a site that doesn't change very ofttimes, this can really increment your site's operation as it'southward not having to grab fresh files every bit frequently later on the cache expires. And in cases similar the above, the frontend of the site in almost cases (unless y'all have a script or role of your site breaking the cache) would stay upward a lot longer.

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

Error establishing a database connection 500 error
Mistake establishing a database connection 500 error

If yous're a Kinsta client you could too await at the 500 error breakdown study in MyKinsta analytics to run into if information technology'south something that has been a reoccurring trouble.

500 error breakdown
500 error breakdown

Mutual Scenarios That Cause This Fault

So why exactly does this happen? Well, here are a few mutual reasons below. And don't worry, nosotros volition go into each of these more in-depth so you tin know how to fix them. Typically you can resolve this error in under 15 minutes.

  • The well-nigh common issue is that your database login credentials are wrong. Your WordPress site uses divide login data to connect to its MySQL database.
  • Your database is corrupted. With and then many moving parts with themes, plugins, and users constantly deleting and installing them, sometimes databases get corrupted. This can be due to a missing or individually corrupted table, or perhaps some data was deleted by accident.
  • You lot may have decadent files in your WordPress installation. This can fifty-fifty happen sometimes due to hackers.
  • Bug with your database server. A number of things could be wrong on the web hosts stop, such as the database existence overloaded from a traffic fasten or unresponsive from also many concurrent connections. This is really quite common with shared hosts equally they are utilizing the same resources for a lot of users on the same servers.

How to Fix the Error Establishing a Database Connexion

Earlier yous offset troubleshooting the fault, we always recommend taking a WordPress site backup. A lot of the recommendations beneath involve manipulating data in your database, then you don't desire to brand things worse. You should ever backup before trying to ready things on your WordPress site no thing how tech-savvy yous recollect you are.

You tin can use a popular WordPress backup plugin such equally VaultPress or WP Fourth dimension Capsule to backup both your files and your database.

You lot tin never accept as well many backups; no matter how tech-savvy you think yous are. 😉 Click to Tweet

If you are a Kinsta user, you can just accept advantage of our one-click fill-in feature. Only click into your WordPress site in MyKinsta, click on "Backups" and then on "Support now."

Backup WordPress site
Backup WordPress site

You then too take the selection to restore a backup to production or staging. Or you can manually backup your MySQL database using phpMyAdmin. Once y'all have a successful backup, information technology is and then time to start troubleshooting your database connectedness issues.

one. Check Your Database Login Credentials

The very get-go thing to do is check to ensure your database login credentials are correct. This is by far the most common reason why the error establishing a database connection message occurs. Peculiarly right after people migrate to a new hosting provider. The connectedness 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 four important pieces of information that all demand to be correct in order for the connection to successfully have place.

Database Name

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

MySQL database username

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

MySQL database password

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

MySQL hostname (server)

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

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

Subscribe At present

cPanel file manager
cPanel file manager

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

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

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

Check Database Credentials in cPanel

The outset affair to check is the database name. To exercise this yous will need to login to phpMyAdmin in cPanel nether the Databases section.

cpanel phpmyadmin
cPanel phpMyAdmin

On the left-manus side, yous should see your database name at the bottom. You tin can ignore the "information_schema" database as this is something used by the host. You lot volition and then desire to compare that proper name confronting the DB_NAME value in your wp-config.php file. If they lucifer, then this isn't the problem. If they don't match, so y'all demand to update your wp-config.php file.

cPanel database name
cPanel database proper noun

Y'all can likewise verify that this is the right database by ensuring that it contains your WordPress site's URL. To do this, but click on the database, then click into the wp_options table (this might exist named something slightly dissimilar for security purposes, such as wpxx_options). At the top of the table, you lot volition come across values for your site'due south URL and name. If these friction match your current site you can residue assured y'all are in the correct identify.

Check site URL in phpMyAdmin
Check site URL in phpMyAdmin

If your database name was already right and you are still getting the fault establishing a database connection message then you will too want to check your username and password. To practise this yous volition need to create a new PHP file in the root directory of your WordPress site, and input the following code. You tin can proper noun it whatever you want, such equally 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 connexion is working properly!'; mysqli_close($testConnection);        

Then browse to the file on your WordPress site: https://yourdomain.com/checkdb.php. If you get a "MySQL Fault: Access denied" so y'all know your username or password is wrong and you lot will need to keep to the next step to reset your credentials.

Access denied mySQL
Access denied MySQL

Below is the bulletin you lot want to see, "Database connection is working properly." But of class, if it was then y'all wouldn't be hither. Be sure to delete/remove this file afterward you are washed testing.

Database connection working properly
Database connection working properly

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

cPanel MySQL databases
cPanel MySQL databases

Curl downward and create a new MySQL user. Endeavor and pick a unique username and password and then that they can't easily be guessed. The password generator tool they provide actually works smashing. So click on "Create User." Y'all could alternatively also change the countersign on this screen for the current database user that already exists.

Create new MySQL user
Create new MySQL user

Then ringlet down 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

So take those new credentials and update your wp-config.php file. You will want to update the DB_USER and DB_PASSWORD values. You could also run the examination file once more from earlier. This should and then resolve your credentials result. If not, it could be that you nevertheless have the wrong hostname (DB_HOST). Some hosts utilize different values, see a listing of some mutual DB host values. Typically this volition merely exist localhost. Merely y'all can always reach out to your hosting provider or check their documentation if you aren't sure. Some might also utilize 127.0.0.i instead of localhost.

If you take followed everything higher up and are still receiving the error establishing a database connection message, then go on to the next troubleshooting steps beneath.

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 you need to do is click on the info department of your site, and at towards the bottom, you volition see the database proper noun, database username, and database password. By default, Kinsta uses localhost for the DB_HOST.

Kinsta database login credentials
Kinsta database login credentials

Then accept those new credentials and update your wp-config.php file. You will want to update the DB_USER and DB_PASSWORD values. This should then resolve your credentials issue. If you need to reset these credentials, just reach out to our back up team. If you have followed everything above and are still receiving the error establishing a database connection message, then continue to the next troubleshooting steps below.

2. Repairing Corrupt Database

In some cases, it could be that your database has go 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 you try to login to your WordPress site'due south dashboard and are receiving the following error, it ways your database is corrupt: "I or more database tables are unavailable. The database may need to be repaired." It is important to note that you might only see this error on the back-cease, whereas you run into the fault establishing a database connection message on the frontend.

WordPress really has a database repair way which yous can initiate. Just add together the following to the bottom of your wp-config.php file.

          ascertain('WP_ALLOW_REPAIR', true);        
WordPress repair mode
WordPress repair manner

So browse to the following location on your WordPress site: https://yourdomain.com/wp-admin/maint/repair.php. Y'all will then have 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 selection every bit information technology is quicker.

WordPress repair database
WordPress repair database

After running the repair of the database above, ensure that you remove the line of lawmaking you added to your wp-config.php file, otherwise anyone could run the repair. If you lot are running cPanel you can as well run a repair from inside the MySQL databases screen.

cPanel repair database
cPanel repair database

Or you could run a repair from within 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 simply running the REPAIR Tabular array command.

Repair tables in phpMyAdmin
Repair tables in phpMyAdmin

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

wp db repair

See more documentation on usage in the WordPress developer resources.

If you are wanting to optimize your database, we take some slap-up tutorials on how to optimize WordPress revisions for functioning, along with how to convert your MyISAM tables to InnoDB. If you are notwithstanding having issues on your site, then keep to the next troubleshooting pace.

Suggested reading: How to Fix the "MySQL Server Has Gone Away" Mistake in WordPress.

3. Fixing Corrupt Files

The next possible reason y'all might be seeing the error establishing a database connection message is that your files accept get corrupt. Whether this originated from an issue with transferring files via FTP, a hacker gaining access to your site or a problem with your host, you can speedily gear up this. However, again we recommend taking a backup of your site earlier trying this.

You are basically going to supercede the core version of WordPress on your site. You aren't touching your plugins, themes, or media, just the WordPress installation itself. To exercise this, y'all will demand to download a fresh re-create of WordPress from WordPress.org.

Download WordPress
Download WordPress

Unzip this file on your figurer. Inside, you will want to delete the wp-content folder, also as the wp-config-sample.php file.

Delete wp-content folder
Delete wp-content folder

And then upload the remaining files via SFTP to your site, overwriting your existing files. This will replace all the problematic files and ensure you lot have fresh ones that are clean and not corrupted. It is recommended to clear your browser cache later on doing this. And then check your WordPress site to come across if the error still exists.

four. Bug With Your Database Server

If zero to a higher place has helped resolve your outcome, and so we highly recommend checking with your hosting provider equally it could be an result with your database server. For example, if there are besides many concurrent connections to your database at once, it could generate the fault. This is because a lot of hosts take limits on their servers on how many connections are allowed at once. Utilizing a caching plugin can help minimize the database interactions on your site. If you are a Kinsta client, you don't need caching plugins, as we take fast server-level caching in place.

This problem can happen a lot on shared hosts as someone else could theoretically be affecting your site. This is because shared hosts apply however resources on servers. This is yet some other reason why we always recommend going with a high-performance managed WordPress host, so things aren't overcrowded. It also means the environment is typically fine-tuned to handle large amounts of traffic specifically to WordPress sites.

The knowledgeable Kinsta support team is always a click away on the lesser right side of the dashboard and available 24/7 if yous need assist. And don't forget we take representatives in to assistance you in multiple languages including English, Spanish, French, Italian, and Portuguese.

Contact Kinsta support
Contact Kinsta support

5. Restore Latest Backup

And last only not least, you can always resort to a backup if needed. In some cases, this might be a faster way to resolve the issue if yous aren't worried nigh losing any information between when your last backup was taken. Many hosts have their ain backup restore process. Remember that you might need to restore both your database and your files.

If yous are Kinsta user you lot can hands restore a backup of your site within the Backups section of your site. Only choose the time you lot want to revert to and click on "Restore to." You tin can then select whether y'all want to restore to Staging or your Live site.

Restore WordPress backup
Restore WordPress backup

Yous will and then be prompted to confirm the restore. Merely enter your site proper name and click on "OK." It likewise creates a backup at the time of restore so you can disengage the restoration if needed.

Confirm WordPress restore
Confirm WordPress restore

Summary

As y'all can see there are quite a few ways to set the fault establishing a database connection in WordPress. The most mutual being invalid credentials in the wp-config.php file. Checking to ensure those are right is the best place to start. The last thing you want for a website is to experience reanimation. So hopefully one of the steps above helped you get your site back up and running. Remember, you can e'er restore your site from a backup if needed.

Accept you experienced the error establishing a database connection bulletin on your site? If so, were you able to resolve it? Let us know below in the comments.


Save time, costs and maximize site performance with:

  • Instant aid from WordPress hosting experts, 24/7.
  • Cloudflare Enterprise integration.
  • Global audience reach with 29 information centers worldwide.
  • Optimization with our built-in Awarding Performance Monitoring.

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