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

Only don't worry, today nosotros'll hash out some mutual scenarios that crusade this fault and some easy ways to get your site back upwards 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 Connexion

What is the Error Establishing a Database Connectedness?

All of the information on your WordPress site, such as post information, folio data, meta information, plugin settings, login data, etc. is stored in your MySQL database. The only data that isn't stored there is media content such as images and your theme/plugin/core files such as index.php, wp-login.php, etc. When someone visits your website, PHP executes the lawmaking on the page 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 mistake establishing a database connection message, every bit seen below. The entire page is blank considering no data can be retrieved to render the page, as the connexion is not working properly. Not simply does this break the frontend of your site, but it will also forestall you from accessing your WordPress dashboard.

Nonetheless, visitors might not encounter this error on the frontend right away. That is because your site is most likely yet serving from cache until it expires. For case, at Kinsta, all WordPress sites are cached for one hour by default. Therefore, if a site is still serving from cache it might appear fine to a visitor.

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

At Kinsta, our support team can increase the elapsing of your cache to say an hr or even a week if you lot want. If you accept a site that doesn't change very often, this can really increase your site's performance every bit information technology'south non having to grab fresh files equally frequently after the cache expires. And in cases similar the above, the frontend of the site in most cases (unless yous have a script or part of your site breaking the cache) would stay upwardly a lot longer.

When visitors try to access your site while this error is happening it volition generate a 500 HTTP status lawmaking in your logs. This same status code appears when at that place 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 will generate a 200 HTTP status lawmaking, which means everything is fine.

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

If you're a Kinsta client you could too look at the 500 error breakup report in MyKinsta analytics to meet if information technology'south something that has been a reoccurring problem.

500 error breakdown
500 mistake breakdown

Common Scenarios That Cause This Error

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

  • The most mutual issue is that your database login credentials are incorrect. Your WordPress site uses separate login data to connect to its MySQL database.
  • Your database is corrupted. With 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.
  • Y'all may have corrupt files in your WordPress installation. This tin even happen sometimes due to hackers.
  • Issues with your database server. A number of things could exist wrong on the web hosts end, such every bit the database beingness overloaded from a traffic spike or unresponsive from also many concurrent connections. This is actually quite common with shared hosts as they are utilizing the same resources for a lot of users on the aforementioned servers.

How to Gear up the Error Establishing a Database Connection

Earlier you start troubleshooting the error, we e'er recommend taking a WordPress site fill-in. A lot of the recommendations below involve manipulating information in your database, then you don't desire to make things worse. You should ever fill-in before trying to fix things on your WordPress site no matter how tech-savvy you think you are.

You tin use a popular WordPress fill-in plugin such as VaultPress or WP Time Capsule to backup both your files and your database.

Y'all can never accept too many backups; no affair how tech-savvy you lot think y'all are. 😉 Click to Tweet

If you are a Kinsta user, you can just accept advantage of our 1-click fill-in characteristic. But click into your WordPress site in MyKinsta, click on "Backups" then on "Back up now."

Backup WordPress site
Backup WordPress site

You and then also have the option to restore a backup to production or staging. Or you can manually backup your MySQL database using phpMyAdmin. In one case you accept a successful backup, it is then time to start troubleshooting your database connection issues.

1. Cheque Your Database Login Credentials

The very first thing to practice is check to ensure your database login credentials are right. This is by far the most common reason why the mistake establishing a database connectedness message occurs. Particularly right subsequently 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 data that all need to exist correct in order for the connectedness to successfully take place.

Database Name

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

MySQL database username

          /** MySQL database username */ ascertain('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 scan to the root of your site. Or if you are using cPanel, y'all can click on "File Manager," browse to the root of your site, and correct click to edit the file.

Subscribe Now

cPanel file manager
cPanel file manager

Here is an case below of what the file looks like when opened.

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

You lot need to at present check your current values against those on your server to ensure they are right. Follow the directions beneath for cPanel and Kinsta users.

Check Database Credentials in cPanel

The starting time affair to cheque is the database name. To do this you volition need to login to phpMyAdmin in cPanel under the Databases section.

cpanel phpmyadmin
cPanel phpMyAdmin

On the left-hand side, you should see your database name at the bottom. You tin ignore the "information_schema" database as this is something used by the host. Y'all will and then desire to compare that proper name against the DB_NAME value in your wp-config.php file. If they match, then this isn't the trouble. If they don't lucifer, then y'all need to update your wp-config.php file.

cPanel database name
cPanel database name

Y'all can also verify that this is the correct database by ensuring that information technology contains your WordPress site'southward URL. To do this, simply click on the database, so click into the wp_options table (this might be named something slightly dissimilar for security purposes, such as wpxx_options). At the top of the table, you will see values for your site's URL and proper name. If these match your current site yous tin can rest assured you are in the correct place.

Check site URL in phpMyAdmin
Check site URL in phpMyAdmin

If your database name was already right and you are still getting the error establishing a database connection message and so y'all will also want to bank check your username and password. To practice this yous will need to create a new PHP file in the root directory of your WordPress site, and input the following code. You can name information technology any 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 $examination = mysqli_connect('localhost', 'db_user', 'db_password'); if (!$exam) { die('MySQL Mistake: ' . mysqli_error()); } repeat 'Database connection 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 Error: Access denied" and then yous know your username or countersign is wrong and you will need to proceed to the side by side step to reset your credentials.

Access denied mySQL
Access denied MySQL

Below is the message you desire to see, "Database connection is working properly." But of form, if it was then you lot wouldn't exist here. Be certain to delete/remove this file after you are done testing.

Database connection working properly
Database connectedness working properly

So 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

Scroll down and create a new MySQL user. Try and pick a unique username and countersign so that they can't hands be guessed. The password generator tool they provide actually works cracking. Then click on "Create User." You could alternatively as well change the password on this screen for the electric current database user that already exists.

Create new MySQL user
Create new MySQL user

Then roll down and add your new user to your database. The next screen will ask which privileges y'all want to assign, select "All Privileges."

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

And so accept those new credentials and update your wp-config.php file. Y'all will desire to update the DB_USER and DB_PASSWORD values. Yous could as well run the test file again from earlier. This should then resolve your credentials upshot. If non, it could be that you still have the wrong hostname (DB_HOST). Some hosts use different values, see a list of some common DB host values. Typically this volition simply be localhost. Merely you tin can always attain out to your hosting provider or check their documentation if you aren't sure. Some might too apply 127.0.0.1 instead of localhost.

If you lot have followed everything above and are withal receiving the mistake establishing a database connection bulletin, then proceed to the next troubleshooting steps beneath.

Check Database Credentials With Kinsta

If y'all 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 section of your site, and at towards the bottom, you will see the database proper name, database username, and database password. 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 volition want to update the DB_USER and DB_PASSWORD values. This should then resolve your credentials issue. If you demand to reset these credentials, only reach out to our back up team. If you take followed everything to a higher place and are nevertheless receiving the fault establishing a database connection bulletin, and so proceed to the next troubleshooting steps below.

2. Repairing Corrupt Database

In some cases, it could be that your database has become corrupt. This tin can occasionally happen (although not very frequently) every bit over time hundreds of tables are constantly added/removed by new plugins and themes. If y'all endeavor to login to your WordPress site's dashboard and are receiving the following error, it means your database is decadent: "1 or more database tables are unavailable. The database may need to be repaired." It is of import to annotation that y'all might only see this error on the dorsum-end, whereas you see the fault establishing a database connectedness message on the frontend.

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

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

Then browse to the following location on your WordPress site: https://yourdomain.com/wp-admin/maint/repair.php. You 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 option as it is quicker.

WordPress repair database
WordPress repair database

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

cPanel repair database
cPanel repair database

Or you lot could run a repair from inside phpMyAdmin. Just log in to phpMyAdmin, click on your database, and select all the tables. So from the dropdown click on "Repair table." This is substantially just running the REPAIR TABLE control.

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 have some great tutorials on how to optimize WordPress revisions for performance, forth with how to convert your MyISAM tables to InnoDB. If you are still having issues on your site, then go on to the next troubleshooting pace.

Suggested reading: How to Fix the "MySQL Server Has Gone Abroad" Error in WordPress.

three. Fixing Corrupt Files

The adjacent possible reason you might be seeing the error establishing a database connection message is that your files take become 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 tin quickly fix this. However, over again we recommend taking a backup of your site earlier trying this.

Y'all are basically going to supersede the core version of WordPress on your site. Yous aren't touching your plugins, themes, or media, just the WordPress installation itself. To do this, you volition need to download a fresh copy of WordPress from WordPress.org.

Download WordPress
Download WordPress

Unzip this file on your reckoner. Inside, you lot volition desire to delete the wp-content folder, every bit well as the wp-config-sample.php file.

Delete wp-content folder
Delete wp-content folder

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

4. Issues With Your Database Server

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

This problem tin can happen a lot on shared hosts as someone else could theoretically be affecting your site. This is considering shared hosts utilize all the aforementioned resources on servers. This is nevertheless another reason why we ever recommend going with a high-performance managed WordPress host, so things aren't overcrowded. It besides ways the environment is typically fine-tuned to handle big amounts of traffic specifically to WordPress sites.

The knowledgeable Kinsta back up team is always a click abroad on the bottom right side of the dashboard and bachelor 24/7 if you demand help. And don't forget we take representatives in to assist you in multiple languages including English, Spanish, French, Italian, and Portuguese.

Contact Kinsta support
Contact Kinsta back up

5. Restore Latest Fill-in

And last but non to the lowest degree, y'all tin always resort to a fill-in if needed. In some cases, this might be a faster way to resolve the consequence if you aren't worried about losing whatsoever data between when your concluding backup was taken. Many hosts take their ain fill-in restore process. Call up that you might need to restore both your database and your files.

If you are Kinsta user you tin hands restore a backup of your site inside the Backups department of your site. Simply choose the fourth dimension yous want to revert to and click on "Restore to." You can so select whether you desire to restore to Staging or your Live site.

Restore WordPress backup
Restore WordPress backup

Y'all volition then be prompted to confirm the restore. Only enter your site proper name and click on "OK." It also creates a backup at the fourth dimension of restore and so you tin undo the restoration if needed.

Confirm WordPress restore
Confirm WordPress restore

Summary

Every bit you tin see in that location are quite a few ways to fix the error establishing a database connexion in WordPress. The almost common being invalid credentials in the wp-config.php file. Checking to ensure those are correct is the best place to start. The concluding thing y'all want for a website is to experience downtime. So hopefully one of the steps to a higher place helped you get your site back upwardly and running. Call back, you lot can always restore your site from a backup if needed.

Take you experienced the mistake establishing a database connection bulletin on your site? If and so, were y'all able to resolve it? Permit u.s. know below in the comments.


Save fourth dimension, costs and maximize site performance with:

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

All of that and much more, in one plan 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'southward right for you.