How To Fix “Error Establishing A Database Connection” In WordPress

The “error establishing a database connection” message is an especially frustrating WordPress error where your entire site gets replaced by a message that looks like this:

If you’re reading this post, there’s a good chance that you’re experiencing this error right now. If not, you are in luck.

And even if you’re not staring at this message, learning how to fix the error establishing a database connection problem is still something that every WordPress user should know.

In this post, you’ll learn three things:

  1. What the “error establishing a database connection” message means
  2. Some of the most common reasons why this message appears
  3. How to fix this error in WordPress

What Does The “Error Establishing A Database Connection” Actually Mean?

If you’re not familiar with how WordPress works, there are two essential parts to a WordPress website:

  • Your files – this includes the WordPress software, your themes, your plugins, your media uploads, etc.
  • Your database – this is where the actual content of your posts and pages are stored (as well as lots of other important information).

Whenever a visitor lands on your WordPress site, the PHP files in WordPress reach out to your database to determine exactly what information to display.

Without a working connection to the database, the PHP-file part of your WordPress site can’t get the information it needs to render a working page. That’s why it displays the “error establishing a database connection” message instead.

What Causes The “Error Establishing A Database Connection” Message?

There are multiple potential reasons why you’re seeing this message.

Most of the time, these causes boil down to:

  • A corrupt database
  • Incorrect database configuration details in your wp-config.php file
  • Corrupt WordPress core files
  • Problems with your web host

Because you can’t be sure what’s causing the message for your specific situation, the best way to fix the problem is to take a diagnostic approach.

That is, you need to systematically follow a series of tests to isolate (and then fix) the problem.

Here’s what to do:

How To Fix “Error Establishing A Database Connection” in WordPress

Potential Fix 1: Check If You Can Still Access The WordPress Dashboard

When you see this message on the front-end of your site, the first thing to do is check whether or not you get the same error when you try to access your wp-admin dashboard.

If you still see the same exact error message when you try to access your wp-admin area, skip ahead to the next step. You’ve already eliminated this problem as a potential cause.

If you see a different message mentioning something like “The database may need to be repaired”, continue with this section to learn how to repair your WordPress database.

Step 1: Turn on the repair utility in the wp-config.php file.

To get started, you need to edit the wp-config.php file for your site. You can do this by either:

  • Connecting to your site via FTP. (FileZilla is a good program for that.)
  • Using cPanel’s built-in File Manager tool.

Your wp-config.php file is located in the root folder of your WordPress site. That’s the same folder that contains wp-admin and wp-content:

Once you’re able to edit your wp-config.php file, add this short code snippet to the bottom of the file:

define( 'WP_ALLOW_REPAIR', true );

Make sure to save your changes before continuing.

Step 2: Run the automatic database repair utility.
Once you’ve added the code snippet, you can access the WordPress database repair tool by going to:

On that page, click Repair Database to run the tool:

Once the tool completes its repairs, your site should hopefully work again. If it does, all you need to do is return to your wp-config.php file and remove the same line of code that you added to enable the repair utility.

If your site still isn’t working, continue onto the next step.

Potential Fix 2: Check Your Database Login Credentials

If your database isn’t corrupt, the next thing you should check is whether your database login credentials are working.

Your database credentials are defined in your wp-config.php file:

If this information doesn’t match the database credentials that you created via your host’s dashboard, WordPress won’t be able to access your database.

To test whether your database credentials are working, you can use the mysql_connect() function.

Use a text editor like Notepad to create a file called databasecheck.php.

Then, add this code snippet to the file:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
echo 'Connected successfully';

Make sure to replace “mysql_user” and “mysql_password” with the actual username and password from your wp-config.php file.

Then, upload that file to your website via the cPanel or by FTP and access it by going to:

If you see a message that says “Connected successfully”, your database credentials are working and you can skip to the next step.

But if you see a message like the one below, you have a problem with your database credentials:

The easiest way to fix the problem is to create a new set of database credentials to use for your WordPress site.

Step 1: Go to MySQL Databases in cPanel.
Assuming your host uses cPanel, you can create a new database user by going to the MySQL Databases option in your cPanel dashboard:

Step 2: Create a new user.

Scroll down to the MySQL Users area and find the box to Add a New User.

Enter a new:

  • Username
  • Password

Make sure to remember these details as you’ll need them in a second.

Step 3: Add a new user to the database.
After you create a new user, go to the Add a User to a Database option and add the new user to your WordPress site’s database:

On the next screen, make sure to give the user ALL PRIVILEGES:

Step 4: Update the wp-config.php file with the new database user.

To finish, you need to go back to your site’s wp-config.php file and update the DB_USER and DB_PASSWORD fields to the new user:

Once you save your changes, your site should hopefully be working again!

Potential Fix 3: Reupload Core WordPress Files

If neither of the above methods fixed the error, some of your core WordPress files may be corrupt.

To ensure this isn’t the issue, you can upload a fresh copy of WordPress to your site. Done right, this won’t affect any of your existing content, plugins, or themes.

Go to and download the latest version of WordPress:

Then, extract the ZIP file and delete the wp-content folder and the wp-config-sample.php file:

Upload the remaining files to your WordPress site via FTP. When your FTP program asks you what to do about duplicate files, make sure to choose the option to Overwriteduplicate files.

This will ensure that none of your core WordPress files are corrupt, and it will hopefully fix the issue.

Potential Fix 4: Speak To Your Host

If none of the above solutions have worked, you may just be dealing with a hosting issue.

There are a couple of potential hosting problems:

  • Your database server is down. Some hosts use a separate server for databases, which means your database might be down even if your regular server still works.
  • Your database has met its quota. Some shared hosts put a cap on your database usage. If your site has met its quota, that might explain why your database is down.

To work through these problems, you’ll need to talk to your host’s support team.

Final Thoughts

By this point, you’ve hopefully been able to fix the error establishing a database connection message and have gotten your site working again.

There are still some smaller issues that might also cause this error. These issues are often unique to your site and are therefore hard to diagnose in a post like this.

If you continue seeing the error message after going through these fixes and talking to your host, you might want to consider reaching out to a WordPress developer to further investigate the issue.

Or, you can always leave a comment and hopefully we can all fix the problem together!

Related posts

8 Replies to “How To Fix “Error Establishing A Database Connection” In WordPress
  1. 無論您是首次置業、換樓或是物業投資者,富邦樓宇按揭服務,為您提供多元化按揭計劃,全面配合不同的財務需要;加上專業按揭顧問的一站式服務,助您抓緊每個 …

  2. 當蛋白線埋入皮膚後,皮下組織會將蛋白線視為異物,啟動異物反應,因此刺激膠原蛋白生長,且可促進新陳代謝,更新老化肌膚,所以客戶在術後也會發覺膚質變得較透亮白皙。膠原蛋白提拉線像一個“磁力線”一樣,將埋入處附近的肌肉和脂肪固定在原處,不會往下墜,其後會吸引皮下組織往蛋白線集中部位移動,就會逐漸產生提拉的緊實效果。也被用於填充美容的微整形,蛋白線材就好比蓋房子的鋼骨支架,先埋入皮膚當作基底結構,然後再注入好比水泥的玻尿酸或自體脂肪,因為有吸附力不易擴散,就可穩定固定住填充物,讓立體支撐力效果更好。水光槍-美白祛斑-水潤亮肌

  3. White Musk® 白麝香手部及指甲护理View AllThe Body Shop® 从 The Body Shop 购买 White Musk® 白麝香手部及指甲护理 : 这款润手霜质地轻盈,将令双手有柔滑饱满之感,留下 White Musk® 白麝香经典细腻的花香。与其他 White Musk® 白麝香产品叠加使用,树立鲜明的香气特征。礦物

  4. 無論您是首次置業、換樓或是物業投資者,富邦樓宇按揭服務,為您提供多元化按揭計劃,全面配合不同的財務需要;加上專業按揭顧問的一站式服務,助您抓緊每個 … play online games on mac

Leave a Reply