How To Fix The Error Establishing a Database Connection WordPress

By Dmytro Spilka

Oct 19, 2017

Fix Error Establishing Database Connection

WordPress is one of the most powerful content management systems available. It is also very simple to install and configure — most of the time! Occasionally, you might run into an unexpected technical issue that will stop you dead in your tracks.

One common issue that WordPress users run into is the message “Error Establishing A Database Connection”. This message indicates that WordPress is unable to make a connection with the correct database. Fortunately, this issue is usually very easy to fix, as this guide will show!

What does “Error Establishing A Database Connection” mean?

To understand what this connection error means, you need to understand how WordPress works. WordPress is written using two technologies — PHP and MySQL. 

PHP is the coding language that handles the logic side of WordPress. That includes deciding how pages should be displayed, managing user registrations, deciding what happens when a user clicks a link and much more. MySQL stores most of the website’s data. That includes the contents of your saved blog posts, user details, and data relating to plugins. PHP processes and validates data before saving it to the MySQL database.

If PHP cannot connect to MySQL to store or retrieve data, it will not be able to correctly render your website.  Instead of displaying a blank or broken page when a connection failure occurs, PHP will display the “Error Establishing A Database Connection” error message. The causes of this error could be:

  1. Your MySQL database has become corrupted: Database corruption can occur for a number of reasons including a MySQL table crash, a power failure, software bugs, or hardware problems. When PHP attempts to connect to a corrupted database or table, MySQL will return an error that causes PHP to display “Error Establishing A Database Connection”.
  2. Your login credentials are incorrect: To connect to a MySQL database, you must supply a username, password, and MySQL server location. If your username or password is incorrect, the database will return an authentication error and PHP will display the “Error Establishing A Database Connection” error message.
  3. Your database server is down: The server that is holding your MySQL databases may be experiencing an outage. When PHP attempts to connect to the database, it will fail and display the “Error Establishing A Database Connection” error message.
  4. Some of your WordPress files are corrupted: If some of your WordPress files have been corrupted, they may not be returning a correct response. This could confuse WordPress into believing there is a database connection error.

Fixing the “Error Establishing A Database Connection”

Fortunately, resolving this error is usually straightforward.

Checking if your WordPress database is corrupted

We will start off by checking if the database or tables within the database are corrupted. The WordPress administration can help us determine if the error is caused by a corrupted database.

Visit your WordPress installation’s administration section.  It will usually be found at http://www.yoursite.com/wp-admin/ (replace http://www.yoursite.com/ with the URL of your website).

If both the front page of your website and the administration section are displaying the “Error Establishing A Database Connection” that means your database is not corrupt. You can go on to the next possible solution.

If you get a different error message on your wp-admin page, that usually means your database is corrupt. A corrupt database table will display a message similar to “One or more database tables are unavailable…”.  No need to panic, it is usually quite easy to fix!

In many cases, WordPress can repair a corrupted table for you, but you will have to enable database maintenance mode first. You do this by editing the wp-config.php file. 

If you are using cPanel, you can edit the file by using the cPanel File Manager. You can also edit this file by using an FTP program like Cyberduck, FileZilla, WinSCP or Transmit. Simply log in and navigate to the WordPress root directory, then open the wp-config.php file.

Next, add the following line to the bottom of the file and save it:

[pastacode lang=”php” manual=”define(%20’WP_ALLOW_REPAIR’%2C%20true%20)%3B” message=”” highlight=”” provider=”manual”/]

You can now navigate to http://www.yoursite.com/wp-admin/maint/repair.php to repair your database (replace http://www.yoursite.com with the URL of your website). Once you enter that URL, you will be presented with two options. Repair Database or Repair and Optimize Database.

Repair Database WordPress

Choose either option — they will both attempt to repair any issues that your database has. The time it takes to repair your database will vary depending on the speed of your server and the size of your database.

Once you have repaired the database, it is important to remove the line you added from the config. Open the wp-config.php file again and remove the line

[pastacode lang=”php” manual=”define(%20’WP_ALLOW_REPAIR’%2C%20true%20)%3B” message=”” highlight=”” provider=”manual”/]

It is important to remove this line because it will disable the repair database page — ensuring that no malicious users can continually cause your database to shut down and repair itself.

If you prefer to not use this method, you can also repair your database via PHPMyAdmin or by repair your database via the command line.

If the “Error Establishing A Database Connection” message still appears, move onto the next step.

Check your database settings in wp-config.php

The next step involves checking your database settings inside the wp-config.php file. Open the file once more. There are four settings that relate to your database connection.

Wp Config File WordPress

They are:

  • DB_NAME: The name of your MySQL database
  • DB_USER: The login name you use when accessing the database
  • DB_PASSWORD: The password that you use when accessing the database
  • DB_HOST: The server where the database is hosted. Normally, this will be on the same server as WordPress. If it is, you can use the word localhost. If it not, you will have to specify the IP address of the server where your MySQL databases are located. 

Your database may have been moved to a new server or its password may have changed. Check that all of the details in the file are correct. If you are unsure about the username and password associated with your database, contact your server administrator. You can also change the password via phpMyAdmin.

Contact your web hosting company

If you have updated your MySQL details and you still cannot connect to your database, it may be a more serious problem with your web host. For example, the entire MySQL server could be offline or the database has been seriously damaged. You should contact your server administrator for assistance or talk to your technical support staff.

Thanks for reading How To Fix The Error Establishing a Database Connection in WordPress. For more WordPress hints and tips, subscribe to the blog or follow us on social media!

Join 1000s of

using Solvid.

234 customers signed up in the last 30 days

SEO Accreditation Large

Solvid is a creative SEO, Content and Digital PR agency. Solvid is a registered trademark of Solvi & Heirs LTD, registered in England and Wales. Registered Address: 6 St. Davids Square, London, England, E14 3WA

VAT: GB 326425708

Reg: 09697233

020 7072 8788

hello@solvid.co.uk