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:
- 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”.
- 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.
- 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.
- 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:
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.
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 thewp-config.php file again and remove the line
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.
- 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!
Full Disclosure: This post may contain affiliate links, which means that if you purchase or sign-up with one of the services, we may receive a commission. There is no additional cost to you. This affiliate relationship in no way has affected the structure of this post nor the items included.