The "error establishing a database connexion" is probably 1 of the most common and frightening errors that WordPress users can encounter. Information technology's definitely a close necktie with the white screen of decease (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 non something to be taken lightly and you should attempt and resolve this immediately as this can directly bear upon your sales, traffic, and analytics.

But don't worry, today we'll discuss some common scenarios that cause this error and some easy ways to get your site back up and running in no fourth dimension.

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

What is the Error Establishing a Database Connexion?

All of the information on your WordPress site, such as post information, page information, meta data, 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/cadre files such equally index.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 so displays it to the company in their browser.

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

Yet, visitors might non run into this error on the frontend right away. That is because your site is most likely notwithstanding serving from cache until information technology expires. For example, at Kinsta, all WordPress sites are buried for one hour past default. Therefore, if a site is nevertheless 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 hour or even a calendar week if y'all want. If you accept a site that doesn't change very oft, this tin actually increase your site'southward functioning as it's not having to grab fresh files as ofttimes after the cache expires. And in cases like the to a higher place, the frontend of the site in nigh cases (unless yous accept 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 information technology will generate a 500 HTTP status code 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 non 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
Error establishing a database connection 500 error

If you're a Kinsta customer you could also wait at the 500 error breakup written report in MyKinsta analytics to meet if it'south something that has been a reoccurring problem.

500 error breakdown
500 error breakdown

Common Scenarios That Cause This Error

And 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 in-depth and so you can know how to gear up them. Typically you can resolve this error in under 15 minutes.

  • The about common event is that your database login credentials are incorrect. Your WordPress site uses separate login information 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 get corrupted. This tin can be due to a missing or individually corrupted table, or perhaps some data was deleted by accident.
  • You lot may take corrupt files in your WordPress installation. This tin even happen sometimes due to hackers.
  • Problems with your database server. A number of things could be wrong on the spider web hosts terminate, such as the database being overloaded from a traffic spike or unresponsive from besides many concurrent connections. This is actually quite common with shared hosts as they are utilizing the aforementioned resource for a lot of users on the aforementioned servers.

How to Fix the Error Establishing a Database Connexion

Earlier yous 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 desire to make things worse. You should always backup earlier trying to set things on your WordPress site no matter how tech-savvy y'all think you are.

You tin apply a popular WordPress backup plugin such equally VaultPress or WP Time Capsule to backup both your files and your database.

You tin never accept too many backups; no matter how tech-savvy you lot retrieve you lot are. 😉 Click to Tweet

If you are a Kinsta user, you can simply take advantage of our one-click backup feature. Simply click into your WordPress site in MyKinsta, click on "Backups" and so on "Support now."

Backup WordPress site
Fill-in WordPress site

Y'all and so too take the option to restore a backup to production or staging. Or you tin manually fill-in your MySQL database using phpMyAdmin. Once you have a successful backup, it is and so time to start troubleshooting your database connection bug.

1. Check Your Database Login Credentials

The very kickoff thing to do is check to ensure your database login credentials are correct. This is past far the most common reason why the fault establishing a database connection message occurs. Particularly right after people drift 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.

Information technology contains iv important pieces of information that all demand to exist correct in order for the connection 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 */ 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 lot can connect to your site via SFTP and browse to the root of your site. Or if y'all are using cPanel, you can click on "File Managing director," browse to the root of your site, and right 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

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

Check Database Credentials in cPanel

The first matter to cheque 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-manus side, you should come across your database name at the lesser. You can ignore the "information_schema" database as this is something used by the host. You will then want to compare that proper name against the DB_NAME value in your wp-config.php file. If they friction match, then this isn't the problem. If they don't match, and so you demand to update your wp-config.php file.

cPanel database name
cPanel database name

You tin can also verify that this is the correct database past ensuring that information technology contains your WordPress site'due south URL. To do this, simply click on the database, then click into the wp_options table (this might exist named something slightly different for security purposes, such as wpxx_options). At the top of the table, you will run into values for your site'south URL and name. If these match your current site yous can residual assured you are in the right place.

Check site URL in phpMyAdmin
Check site URL in phpMyAdmin

If your database name was already right and you are still getting the mistake establishing a database connection message then you volition too desire to check your username and password. To practise this you will need to create a new PHP file in the root directory of your WordPress site, and input the following code. You lot can proper name it whatever you want, such as checkdb.php. Only 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 (!$exam) { dice('MySQL Fault: ' . mysqli_error()); } echo 'Database connection is working properly!'; mysqli_close($testConnection);        

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

Access denied mySQL
Access denied MySQL

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

Database connection working properly
Database connection working properly

So next y'all need to reset your username and password. In cPanel, click on MySQL Databases under the Databases section.

cPanel MySQL databases
cPanel MySQL databases

Curlicue down and create a new MySQL user. Try and option a unique username and password and then that they 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 password on this screen for the current database user that already exists.

Create new MySQL user
Create new MySQL user

Then scroll downward and add your new user to your database. The next screen will ask which privileges you lot want 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 lot volition want to update the DB_USER and DB_PASSWORD values. You could too run the test file again from earlier. This should then resolve your credentials issue. If not, information technology could exist 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 will but be localhost. Only you lot can always reach out to your hosting provider or bank check their documentation if you aren't sure. Some might also utilize 127.0.0.1 instead of localhost.

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

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

Kinsta database login credentials
Kinsta database login credentials

So 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 need to reset these credentials, but reach out to our support team. If you have followed everything above and are all the same receiving the error establishing a database connection message, then proceed to the side by side troubleshooting steps below.

2. Repairing Corrupt Database

In some cases, it could be that your database has become decadent. This can occasionally happen (although non very often) every bit over time hundreds of tables are constantly added/removed past new plugins and themes. If y'all try to login to your WordPress site's dashboard and are receiving the following error, it ways your database is corrupt: "One or more database tables are unavailable. The database may need to exist repaired." It is important to notation that you might only see this mistake on the back-finish, whereas you see the mistake establishing a database connection message on the frontend.

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

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

Then browse to the post-obit location on your WordPress site: https://yourdomain.com/wp-admin/maint/repair.php. You lot volition then accept 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

After running the repair of the database to a higher place, ensure that y'all 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 can as well run a repair from inside the MySQL databases screen.

cPanel repair database
cPanel repair database

Or y'all 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 substantially 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 command:

wp db repair

Come across more than documentation on usage in the WordPress programmer resources.

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

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

3. Fixing Corrupt Files

The next possible reason yous might be seeing the error establishing a database connection bulletin is that your files have become decadent. Whether this originated from an result with transferring files via FTP, a hacker gaining access to your site or a problem with your host, you can quickly fix this. Notwithstanding, again we recommend taking a backup of your site before trying this.

You are basically going to replace the core version of WordPress on your site. Yous aren't touching your plugins, themes, or media, just the WordPress installation itself. To exercise 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 binder, as well as the wp-config-sample.php file.

Delete wp-content folder
Delete wp-content binder

And 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 make clean and non corrupted. It is recommended to articulate your browser cache after doing this. Then check your WordPress site to see if the error however exists.

4. Issues With Your Database Server

If cypher above has helped resolve your effect, and so we highly recommend checking with your hosting provider as it could exist an issue with your database server. For example, if at that place are too many concurrent connections to your database at in one case, it could generate the error. This is because a lot of hosts accept limits on their servers on how many connections are allowed at one time. 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 trouble tin can happen a lot on shared hosts every bit someone else could theoretically be affecting your site. This is because shared hosts utilize yet resources on servers. This is yet another reason why we ever recommend going with a high-performance managed WordPress host, and then 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 e'er a click away on the lesser right side of the dashboard and available 24/7 if you need help. And don't forget we have representatives in to assistance you in multiple languages including English, Spanish, French, Italian, and Portuguese.

Contact Kinsta support
Contact Kinsta support

five. Restore Latest Backup

And last but not least, you lot can always resort to a backup if needed. In some cases, this might be a faster style to resolve the issue if you aren't worried most losing whatsoever data betwixt when your last backup was taken. Many hosts have their ain fill-in restore process. Remember that you might need to restore both your database and your files.

If you lot are Kinsta user you tin easily restore a backup of your site inside the Backups department of your site. Simply cull the fourth dimension y'all 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 backup

Yous will so be prompted to confirm the restore. Simply enter your site name and click on "OK." It also creates a fill-in at the time of restore so y'all tin undo the restoration if needed.

Confirm WordPress restore
Confirm WordPress restore

Summary

As you can see there are quite a few ways to gear up the error establishing a database connectedness 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 affair you desire for a website is to feel downtime. So hopefully one of the steps above helped you lot get your site back upwardly and running. Remember, you tin can always restore your site from a backup if needed.

Accept you experienced the fault establishing a database connection bulletin on your site? If and then, were you able to resolve it? Allow us know below in the comments.


Save time, costs and maximize site functioning with:

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

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