How To Fix WordPress Error Establishing A Database Connection

Are you getting an “Error establishing a database connection” message on your site? This step-by-step tutorial will show you how to fix this WordPress issue …

How To Fix WordPress Error Establishing A Database ConnectionThis tutorial is part of our FREE WordPress Management training and WordPress Troubleshooting tutorial series.

In this series of step-by-step tutorials, we show you how to troubleshoot and fix common WordPress errors.

Info

Important: Always do a backup of your WordPress site (files and data) before modifying any files or performing any upgrades or installations! 

If you don’t want to do your own backups, then consider hiring a professional, or use backup plugins.

How To Fix WordPress Error Establishing A Database Connection

Error establishing a database connection

(Error establishing a database connection)

All WordPress data is stored in a database on your server. If your website isn’t able to connect to the database to retrieve its data, you will get an “error establishing a database connection” message …

Error establishing a database connection message

(“Error establishing a database connection” message)

Sometimes, you will also get this error after logging into your WordPress admin …

Error establishing a database connection message

(“Error establishing a database connection” message)

This common WordPress issue can be caused by a number of reasons, including:

  • Incorrect WordPress login credentials,
  • Corrupt WordPress database tables,
  • Corrupt files caused by plugin conflicts or malware,
  • Database server overload, malfunction, or being unavailable due to maintenance or some other issue.

As the error message gives no indication of which of the above is causing the issue, a ‘trial and error’ method may be required to troubleshoot and fix the problem.

Is Your Server Up And Running?

If you experience an “error establishing a database connection,” it’s best to start by eliminating the possibility that your web server may be causing the error.

If you host more than one website with the same hosting service provider, check if your other websites have also been affected.

Contact your web host’s technical support team to notify them of the issue. If there is a problem with your server, this should correct the problem. Depending on your web host, they may also be able to fix the error from their end.

Below are additional troubleshooting methods you can try if you want to try to diagnose and correct the issue yourself.

Can You Login To Your WordPress Admin?

Normally, the cause of the database error will either be an issue on the front-end of your site (i.e. your home page or other web pages), or something that has happened on the back-end (i.e. your site’s administration area).

The first step in identifying the issue, then, is to establish if the error is happening on the front-end or back-end of your site.

If everything looks fine when you visit the site but you get the database connection error when you try to log into your WordPress admin area, the issue may be caused by incorrect login credentials or a corrupted database table.

Check Your wp-config.php File

Checking to see if your login credentials are correct involves accessing your wp-config.php file using either FTP or cPanel’s File Manager. If you need help with this, see this tutorial:

After accessing your server, look for the wp-config.php file …

wp-config.php file

(wp-config.php file)

Right-click on the file and select ‘Download’ to save a copy to your hard drive …

Download wp-config.php to your hard drive

(Download wp-config.php to your hard drive)

Next, open up the wp-config.php file using a plain text editor (use your operating system’s built-in editor or an application like NoteTab).

Practical Tip

If you can’t see the file in your text editor browser, make sure to select ‘All files (*.*)’ in the ‘Files of type:’ menu …

Open wp-config.php using a text editor

(Open wp-config.php using a text editor)

Scroll down all of the code until you see a section containing the following fields:

  • DB_NAME: Database name
  • DB_USER: Databaseuser
  • DB_PASSWORD: Database password
  • DB_HOST: Database host (typically ‘localhost’)

Make sure that all of the details in this section are filled in with the correct details of your database …

Check if your database details are correct

(Check if your database details are correct)

If you need help with the above, see this tutorial:

If you can’t find your details, contact your web host

If your WordPress database details are correct, proceed to the section below to continue troubleshooting the issue.

WordPress Database Repair Feature

WordPress has a built-in function that tries to repair issues with its database. To access this function, however, requires adding a line of code to your WordPress wp-config.php file.

If you are not comfortable editing files on your server, contact your web hosting company or a professional WordPress tech support service provider to help you.

To access the WordPress database repair feature, open your wp-config.php file and add the line below to the end of your file …

WordPress Database Repair command

(WordPress Database Repair command)

Your wp-config.php file should look like the screenshot below …

Add this line of code to your wp-config.php file

(Add this line of code to your wp-config.php file)

Save the file and upload the modified wp-config.php file to your server, overwriting the existing server file …

Upload your edited wp-config.php file to your server

(Upload your edited wp-config.php file to your server)

After uploading the edited wp-config.php file to your server, open up a new web browser and type in the following URL (replace ‘yourdomain.com’ with your actual domain) …

Activate the WordPress Database Repair function

(Activate the WordPress Database Repair function)

This will bring up the WordPress Database Repair screen. Click on ‘Repair Database’ to begin the process …

WordPress Database Repair screen

(WordPress Database Repair screen)

Allow a few seconds for the WordPress Database Repair function to run. After the process has completed, you will see the results displayed on your screen …

WordPress Database Repair process

(WordPress Database Repair process)

After the WordPress Database Repair process has completed, do the following:

  • Open up your wp-config.php file, delete the line of code previously added to activate the repair function, resave the file and reupload it to your server, overwriting the previous file. This is very important, as leaving the line of code in your wp-config file creates a security vulnerability that can be easily exploited by hackers or malicious users.
  • Go back to your WordPress site, reload your screen and check to see if this has fixed the issue.

If the issue has not been fixed, the problem may be a corrupted file on your server.

Corrupted WordPress File

If the error has to do with a corrupted WordPress file on your server, you may be able to fix the problem by re-uploading your WordPress installation files …

WordPress installation files

(WordPress installation files)

Warning

If you plan to re-upload your WordPress installation files, DO NOT overwrite the wp-config.php file on your server or the contents inside your wp-content folder.

Overwriting the wp-config.php file will render your website inaccessible. Overwriting your wp-content folder can delete your plugins, themes, and other important settings.

For the safest way to re-upload your WordPress installation files, go here:

If none of the above fixes the problem, you may need the help of a professional WordPress technical support expert or a better web hosting service provider.

In addition to the information above, check out this video on how to fix the error establishing database connection in WordPress from the team at MyThemeShop:

(How to fix the error establishing database connection in WordPress. Video: MyThemeShop)

More WordPress Troubleshooting Tutorials

If you experience any other errors with WordPress, check out the tutorials in the section below:

How To Fix WordPress Error Establishing A Database Connection

(Source: Pixabay)

***

"If you're new to WordPress, this can stand on its own as a training course and will stay with you as you progress from beginner to advanced and even guru status." - Bruce (Columbus, Ohio)

***

Recommended Video Courses For WordPress Users

How To Use Amazon S3How To Use Amazon S3

Learn how to set up and use Amazon S3 to upload, store, manage, and protect your site’s images, large media files, downloadable files, stream videos and more.

More info: How To Use Amazon S3

Recommended Video Courses For WordPress Users

Using Password ManagersUsing Password Managers

Password Managers provide an easy and secure way to keep track of all your passwords. This video course shows you how to use two FREE powerful password management tools.

More info: Using Password Managers

Recommended Video Courses For WordPress Users

How To Set Up WordPress On LocalhostHow To Set Up WordPress On Localhost

Learn how to install, set up, and locally host a fully functioning WordPress site on your computer.

More info: How To Set Up WordPress On Localhost

Recommended Video Courses For WordPress Users

How To Install, Set Up & Use WordPress MultisiteHow To Install, Set Up & Use WordPress Multisite

Learn how to quickly and securely set up an online community of websites or blogs using the powerful WordPress Multisite feature.

More info: WordPress Multisite

Recommended Video Courses For WordPress Users

WordPress SecurityWordPress Security

Learn how to keep your WordPress site or blog secure and protected from malware, hackers and brute-force attacks.

More info: WordPress Security

Recommended Video Courses For WordPress Users

Google ToolsGoogle Tools

This video course will help you learn the basics of using Google Tools like Google Analytics and Gmail.

More info: Google Tools

Recommended Video Courses For WordPress Users

How To Optimize WordPressHow To Optimize WordPress

Learn how to optimize your WordPress site and improve your page loading speed.

More info: How To Optimize WordPress

Recommended Video Courses For WordPress Users

How To Use cPanelHow To Use cPanel

cPanel is a powerful and simple-to-use web hosting management software application that gives website owners the ability to quickly and easily manage their servers and websites using a simple and intuitive dashboard.

This video course will teach you how to use the main features of cPanel to manage your web hosting.

More info: How To Use cPanel

Recommended Video Courses For WordPress Users

How To Back Up & Restore WordPress SitesHow To Back Up & Restore WordPress Sites

Learn how to safely and automatically backup your WordPress files and database and how to easily restore your WordPress site if something were to happen.

More info: How To Back Up & Restore WordPress Sites

Recommended Video Courses For WordPress Users

How To Use FTPHow To Use FTP

This video course shows you how to use FTP (File Transfer Protocol) to transfer and upload files between your hard drive and your server using a free FTP program called Filezilla.

More info: How To Use FTP

Author: Martin Aranovitch

Martin Aranovitch is the founder of WPCompendium.org and the author of The Small Business Digital Manager. WPCompendium.org provides hundreds of FREE tutorials that show you how to use WordPress to grow your business online with no coding skills required! Get our FREE "101+ WordPress Tips, Tricks & Hacks For Non-Techies" e-course with loads of useful WordPress tips!

Originally published as How To Fix WordPress Error Establishing A Database Connection.