The "mistake establishing a database connexion" is probably ane of the most mutual and frightening errors that WordPress users tin can encounter. Information technology's definitely a close tie with the white screen of death (WSOD).  This error ways your website is no longer communicating or has admission to your WordPress database, and thus your unabridged website goes down. This is non something to be taken lightly and you should try and resolve this immediately every bit this tin directly affect your sales, traffic, and analytics.

Only don't worry, today we'll discuss some common scenarios that cause this fault and some easy ways to get your site support and running in no time.

  • What is the Error Establishing a Database Connection?
  • 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 data on your WordPress site, such as post data, folio data, meta information, plugin settings, login data, etc. is stored in your MySQL database. The only information that isn't stored there is media content such every bit images and your theme/plugin/core files such equally 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 it to the visitor in their browser.

If for some reason this isn't working properly, yous are left with the error establishing a database connection message, as seen below. The entire page is bare because no data can be retrieved to return the folio, as the connection is not working properly. Not but does this break the frontend of your site, but information technology volition also prevent you from accessing your WordPress dashboard.

Nevertheless, visitors might non see this error on the frontend right abroad. That is because your site is most likely still serving from cache until it expires. For example, at Kinsta, all WordPress sites are cached for one hr past default. Therefore, if a site is still serving from cache it 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 hour or even a week if you want. If you lot have a site that doesn't change very ofttimes, this tin actually increase your site's performance as information technology'south not having to grab fresh files equally oft afterward the cache expires. And in cases like the above, the frontend of the site in most cases (unless you accept a script or function of your site breaking the cache) would stay upwards a lot longer.

When visitors attempt to admission your site while this error is happening it will generate a 500 HTTP status code 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 non delivered. When everything is working properly your site will generate a 200 HTTP condition code, which ways everything is fine.

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

If you're a Kinsta customer you could besides look at the 500 error breakdown written report in MyKinsta analytics to run into if it'due 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, we will go into each of these more than in-depth so you lot can know how to set up them. Typically yous can resolve this fault in under xv minutes.

  • The most common issue is that your database login credentials are incorrect. Your WordPress site uses dissever login information 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 information was deleted by accident.
  • You may have decadent files in your WordPress installation. This can even happen sometimes due to hackers.
  • Issues with your database server. A number of things could be wrong on the web hosts end, such as the database being overloaded from a traffic spike or unresponsive from as well 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

Earlier y'all start troubleshooting the error, we always recommend taking a WordPress site backup. A lot of the recommendations below involve manipulating information in your database, so you don't want to make things worse. You lot should ever backup before trying to prepare things on your WordPress site no thing how tech-savvy you think yous are.

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

Y'all can never have as well many backups; no matter how tech-savvy you lot retrieve you are. 😉 Click to Tweet

If you lot are a Kinsta user, you tin can just take reward of our one-click fill-in characteristic. Simply click into your WordPress site in MyKinsta, click on "Backups" and and so on "Back up now."

Backup WordPress site
Backup WordPress site

You then also accept the option to restore a fill-in to production or staging. Or you tin can manually fill-in your MySQL database using phpMyAdmin. Once you have a successful backup, it is then fourth dimension to start troubleshooting your database connection bug.

i. Check Your Database Login Credentials

The very outset thing to practise is check to ensure your database login credentials are right. This is past far the most common reason why the error establishing a database connection bulletin occurs. Especially correct 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 information that all demand to be correct in order for the connection to successfully take place.

Database Name

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

MySQL database username

          /** MySQL database username */ define('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 you can connect to your site via SFTP and scan to the root of your site. Or if you are using cPanel, you can click on "File Manager," scan to the root of your site, and right click to edit the file.

Subscribe At present

cPanel file manager
cPanel file manager

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

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

Y'all need to at present bank check your current values against those on your server to ensure they are right. Follow the directions below for cPanel and Kinsta users.

Check Database Credentials in cPanel

The starting time matter to check is the database name. To do this y'all will need to login to phpMyAdmin in cPanel under the Databases section.

cpanel phpmyadmin
cPanel phpMyAdmin

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

cPanel database name
cPanel database name

You lot tin can as well verify that this is the correct database by ensuring that it contains your WordPress site's URL. To do this, simply click on the database, and so click into the wp_options table (this might be named something slightly different for security purposes, such every bit wpxx_options). At the pinnacle of the table, you will meet values for your site's URL and name. If these match your current site you lot can residual assured y'all are in the right place.

Check site URL in phpMyAdmin
Bank check site URL in phpMyAdmin

If your database proper name was already correct and you are yet getting the fault establishing a database connexion bulletin then you will also want to bank check your username and password. To do this y'all volition need to create a new PHP file in the root directory of your WordPress site, and input the post-obit code. You tin name it any y'all want, such as checkdb.php. Simply alter 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 Error: ' . mysqli_error()); } echo 'Database connectedness is working properly!'; mysqli_close($testConnection);        

Then browse to the file on your WordPress site: https://yourdomain.com/checkdb.php. If you become a "MySQL Error: Access denied" and then y'all know your username or countersign is wrong and you volition need to proceed to the next step to reset your credentials.

Access denied mySQL
Access denied MySQL

Below is the bulletin you want to run into, "Database connection is working properly." Just of course, if it was and so you lot wouldn't be here. Be sure to delete/remove this file after yous are washed testing.

Database connection working properly
Database connexion working properly

So side by side you need to reset your username and countersign. 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 password and so that they tin can't easily be guessed. The password generator tool they provide actually works great. Then click on "Create User." You 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 scroll down and add your new user to your database. The next screen will ask which privileges yous want 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. You will want to update the DB_USER and DB_PASSWORD values. Y'all could too run the test file once more from earlier. This should so resolve your credentials outcome. If not, information technology could exist that you still accept the wrong hostname (DB_HOST). Some hosts use different values, meet a list of some common DB host values. Typically this will simply exist localhost. But you can always achieve out to your hosting provider or bank check their documentation if you aren't sure. Some might also utilize 127.0.0.one instead of localhost.

If yous have followed everything above and are even so receiving the error establishing a database connection bulletin, then proceed to the side by side troubleshooting steps beneath.

Check Database Credentials With Kinsta

If you are a Kinsta user, the process of checking your credentials against your wp-config.php file and the server are much easier! All yous need to do is click on the info department of your site, and at towards the bottom, you volition see the database proper name, database username, and database countersign. Past default, Kinsta uses localhost for the DB_HOST.

Kinsta database login credentials
Kinsta database login credentials

And so take those new credentials and update your wp-config.php file. You lot will desire to update the DB_USER and DB_PASSWORD values. This should then resolve your credentials outcome. If yous demand to reset these credentials, but accomplish out to our support team. If yous take followed everything to a higher place and are still receiving the error establishing a database connexion bulletin, then proceed to the next troubleshooting steps below.

two. 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 past new plugins and themes. If you lot try 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 important to annotation that you might only meet this error on the dorsum-end, whereas you see the error establishing a database connection bulletin on the frontend.

WordPress actually has a database repair style which y'all can initiate. Simply add together the following to the bottom of your wp-config.php file.

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

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

WordPress repair database
WordPress repair database

Later running the repair of the database above, ensure that yous remove the line of code you added to your wp-config.php file, otherwise anyone could run the repair. If yous are running cPanel you 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. Only log in to phpMyAdmin, click on your database, and select all the tables. Then from the dropdown click on "Repair tabular array." This is essentially just running the REPAIR TABLE 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 control:

wp db repair

See more documentation on usage in the WordPress developer resources.

If yous are wanting to optimize your database, nosotros have some great tutorials on how to optimize WordPress revisions for functioning, along with how to catechumen your MyISAM tables to InnoDB. If you are still having bug on your site, then proceed to the adjacent troubleshooting stride.

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

3. Fixing Corrupt Files

The next possible reason you might be seeing the error establishing a database connection message is that your files have 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 can quickly prepare this. However, again nosotros recommend taking a fill-in of your site before trying this.

You lot are basically going to supplant the core version of WordPress on your site. You aren't touching your plugins, themes, or media, simply 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 computer. Inside, you will want to delete the wp-content folder, equally well equally 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 supervene upon all the problematic files and ensure yous take fresh ones that are clean and not corrupted. Information technology is recommended to articulate your browser cache after doing this. Then check your WordPress site to see if the error notwithstanding exists.

4. Issues With Your Database Server

If nothing above has helped resolve your issue, and so we highly recommend checking with your hosting provider every bit it could exist an issue with your database server. For example, if there are as well many concurrent connections to your database at one time, information technology could generate the error. This is because a lot of hosts have 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 yous are a Kinsta client, y'all don't need caching plugins, every bit we have fast server-level caching in identify.

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

The knowledgeable Kinsta support team is e'er a click abroad on the bottom correct side of the dashboard and bachelor 24/7 if y'all need aid. And don't forget we have representatives in to help y'all in multiple languages including English, Spanish, French, Italian, and Portuguese.

Contact Kinsta support
Contact Kinsta back up

five. Restore Latest Backup

And terminal only not least, you can e'er resort to a backup if needed. In some cases, this might exist a faster way to resolve the issue if you aren't worried about losing any data betwixt when your concluding backup was taken. Many hosts have their ain backup restore process. Remember that you might demand to restore both your database and your files.

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

Restore WordPress backup
Restore WordPress backup

You volition so be prompted to confirm the restore. Simply enter your site proper name and click on "OK." It also creates a backup at the time of restore so you lot can undo the restoration if needed.

Confirm WordPress restore
Ostend WordPress restore

Summary

As yous can see there are quite a few means to set up the mistake establishing a database connection in WordPress. The most common being invalid credentials in the wp-config.php file. Checking to ensure those are right is the best place to kickoff. The last matter y'all desire for a website is to experience downtime. So hopefully one of the steps above helped you go your site back up and running. Remember, you lot tin can always restore your site from a fill-in if needed.

Have yous experienced the error establishing a database connection message on your site? If so, were you able to resolve it? Allow usa know below in the comments.


Salve time, costs and maximize site performance with:

  • Instant assistance from WordPress hosting experts, 24/7.
  • Cloudflare Enterprise integration.
  • Global audience accomplish with 32 data centers worldwide.
  • Optimization with our built-in Awarding Functioning Monitoring.

All of that and much more, in i program with no long-term contracts, assisted migrations, and a xxx-day-money-back-guarantee. Cheque out our plans or talk to sales to find the program that's right for y'all.