Fixing “Error Establishing a Database Connection” on WordPress

error establishing a database connection

Are you seeing “Error establishing a database connection” on your screen? If yes, it’s a bad day for you! This WordPress database error is one of the most frightening and common errors faced by WP users. It’s pretty similar to the white screen of death (WSoD); all you can see is a white screen with “Error establishing a database connection” written on it.

This error simply states that your WordPress site no longer has access to or communication with your WP database. Hence, your website is down! This error isn’t something you should ignore. It would help if you immediately came up with a solution as the error will affect your analytics, traffic, sales, and rankings on search engines.

Don’t worry!

We have seen this error many times and have the perfect solution to get past it. Read along to learn more about this WordPress database error and how to resolve it.

What is “Error Establishing a Database Connection”?

Error establishing database connection

All the data for your WordPress website is stored in your MySQL database. This includes login information, plugin settings, page data, posts, metadata, etc. However, media files and plugin/theme/core files aren’t stored. Once a user visits your website, PHP executes the code on the page for information from the WP database; after this, the user can see the page clearly on their browsers. If this process isn’t working correctly, you’ll see an error message establishing a database connection on your screen. As the connection isn’t working correctly, no data will be displayed. Not only does this prevent you from accessing your WordPress dashboard, but it also hits the frontend of your site. 

Luckily, users may not see this error immediately. Your WP site may still be served from a cache until it expires.

Common Causes of this WordPress Database Error

Are you wondering why this happens? Here are a few common reasons why WP users face this challenge.

  • Database corruption
  • Incorrect database login credentials
  • Problems with hosting provider or web server
  • WordPress core file corruption

Any of these can be the cause of this error. Therefore, in order to fix this error, some troubleshooting will be involved. 

How to Fix the WordPress “Error establishing a database connection”

This error is considered pretty serious, but fixing it isn’t that hard. Here, you’ll find easy troubleshooting tips to resolve this error in no time. 

1. Repair your database

The first thing you should check is if your database is okay or not. If your website is usually opening, but you are facing an error while trying to open the wp-admin page, or you see an error saying “One or more database tables are unavailable,” you may need to repair the database. The good thing here is that your database can be repaired with the help of a built-in WP feature. 

By default, this repair feature is disabled. You can enable it by going into the wp-admin.php file and adding the following code;

define( ‘WP_ALLOW_REPAIR’, true );

This code permits you to repair and optimize your database. Once you add this line, browse www.yoursite.com/wp-admin/maint/repair.php. A page with two options will open up;

  • Repair
  • Repair and Optimize
Repair WordPress Database

The repair option will simply repair your database. On the other hand, the 2nd option will both repair and optimize your database. If you are short on time, then avoid choosing the repair and optimize option as it is time-consuming. 

Note: the line of code you entered isn’t secure. Anyone can use the URL we shared with you without having to login. Therefore, remove the code from the wp-admin.php file once you are done. This will ensure that no one can mess around with your database. 

2. Update Login Credentials

Repairing your database didn’t work? No problem! You can go through the wp-config file to take a look at the database login settings. If you have changed some useful information about your database, or your hosting provider, the database login credentials stop working. 

wp-config file

Open the wp-config file and find your database credentials. The name of the database will be “DB_NAME,” the login password as “DB_PASSWORD,” the login username as “DB_USER,” and the hosting service as “DB_HOST.” Once you find all this information, recheck and make sure it is all correct. WordPress won’t be able to connect with the database if the info isn’t correct.

You can confirm if these details are correct from your CloudPages dashboard. Once you login, open the databases section. Here you can see the database name and when it was created.

Error establishing database connection

CloudPages does not allow users to change their password. However, you can create a new database to update both the username and password.


Once you have made sure that the details are correct, you can make changes in the wp-config.php file if you want to.

After that, try to open your website again to see if the error is fixed or not. If you still see the error, then that means there is something else wrong.

3. Recheck Your Database Host Information

Once you are sure your DB username, name, and password are correct, you’ll want to check if your database host information is accurate. Usually, hosting providers utilize localhost as your database host. However, some leading companies use good WordPress hosting servers to host databases. In such a case, your database information will not be localhost. 

To find out for sure, you’ll need to ask your WordPress hosting company for more information about your database host.

4. Check If Your Database Server Is Down

If you find all things are correct and still see “Error establishing a database connection” on your screen, then your database server, in other words, your MySQL server, may be down.

This may occur due to an unexpected traffic spike on your website. To be precise, your host server was unable to handle the load. This usually happens when you are using shared hosting. 

This will make your website very slow. On the other hand, many users will see this error. If that’s the case, you should pick up your phone or use live chat to connect with your hosting provider and ask them about the current status of your MySQL server. If you have multiple sites running on the same server, you can check if they are running or not. If all sites are showing this WordPress database error, it’s likely that your SQL server is down. 

If you don’t have any other website on this server, you’ll have to use phpMyAdmin. To access this online tool, you’ll first have to set it up on CloudPages.

Click on CloudPages v2 (Beta), in the header of your dashboard

Next, click on the phpMyAdmin logo to begin the setup process.

For further guidance on how to install phpMyAdmin on CloudPages take a look at the phpMyAdmin setup guide.

Once you are done, you can access phpMyAdmin, and connect to the database.

Once you access it, you need to make sure that your database user has sufficient permission. Create a new file called testconnection.php and paste the following code in it:

12345678<?php$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);if (!$link) {die(‘Could not connect: ‘ . mysqli_error());}echo ‘Connected successfully’;mysqli_close($link);?>

Ensure that you change the password and username. Upload this file to your WP site and access it with a browser. If the script connects successfully, then that means your users have sufficient permission, and something else is wrong. Now it’s better to go back to your wp-config file to recheck everything, especially typos. Also, you can try rebooting your servers. This will restart your database servers and website and fix some temporary glitches that may be causing the error. 

5. Take Professional Help

If you fail to fix this WordPress database error, it’s high time you contact your web hosting company. All good hosting providers will assist you in getting your website back online. They’ll help you fix the problem and won’t stop until your website is live. 

You can also consider hiring the services of professional WordPress developers who can help you fix this error for a reasonable price. WP developers know what they are doing and will resolve your issue in no time. 

Wrap Up

“Error establishing a database connection” is one of the most common WordPress database errors you’ll encounter. However, fixing this error isn’t that hard. The tips and steps mentioned above are ideal to resolve this issue without facing the consequences of having your website down. On the other hand, you can always rely on WP development agencies to help you get past any WordPress database connection errors. 

Did you find what you were looking for? Share your feedback in the comments section below. 

Become CloudPages

Community Member to Get Latest Updates.

Scroll to Top
Scroll to Top