Error Establishing a Database Connection: 4 Methods to Quickly Fix It

John Lennon Wordpress Database Error

You check your WordPress website and find the feared Error Establishing a Database Connection screen. Your users can’t access the website so you start to panic. But panic no more! Learn how to fix this problem quickly with these 4 awesome methods.

error establishing a database connection wordpress

Error establishing a database connection: What does it mean?

Having your WordPress website down is one of the most stressful moments you can face as a website owner. Maybe you were doing some changes that caused the website to go down, or you simply don’t know when this happened or why.

Your website is not working and you see the feared “Error Establishing a Database Connection”. But what does this exactly mean and what is causing it?

A database is just a software used to store, organize and retrieve data. WordPress is a CMS (content management system) that uses MySQL databases to store all the information and details of your website.

WordPress is in need to have a constantly open connection to your MySQL database server to work properly. When this connection breaks, the infamous Error Establishing a Database connection appears. Not complicated so far, right?

4 common causes error establishing a database connection

4 Most Common Causes For The Error Establishing a Database Connection

There are 4 main causes for this error: Incorrect database credentials, corrupted database, corrupted files and database server down.

error establishing a database connection incorrect credentials

1. Your credentials are incorrect

When your login credentials are not correct, the database connection can’t happen. It’s really common after a migration to a new server. If you are seeing the Error Establishing a Database Connection both in your front-end (example.com) and back-end (example.com/wp-admin) then this is how you can fix it:

Verify that the login details stored inside wp-config.php are the right ones. Wp-config.php stores all the important details related to your WordPress site configuration.

To access wp-config.php you’ll need an FTP client like Filezilla. On the FTP client you’ll need the hosting server ftp address, ftp password and port used. You can usually find these details on the control panel of your hosting company or in the welcome email they sent you when you signed up. Note: Although I’m talking about FTP, please use SFTP whenever possible for security purposes.

Once you connect to your server, go to your public_html folder and find wp-config.php. Download it and keep a copy in a safe place. Open it using the notepad or an editor like Notepad++.

Then you need to find the following code:

DB_NAME: Your database name

DB_USER: Your database user

DB_PASSWORD: Your database password

DB_HOST: Your database host

Write them down and then access your hosting server control panel. Go to the database section and check that the details displayed there are the same as the ones you just retrieved from wp-config.php

If any of the details is different, please modify your wp-config.php accordingly, save it and upload it to your public_html folder using Filezilla. It will ask you for confirmation to overwrite the existing wp-config.php file, click accept and then reload your website. 90% of the time this will fix the problem.

But what happens if it doesn’t? It’s possible that your password or host is still incorrect. To reset your MySQL database password you need to access PHPmyAdmin on your hosting company control panel, or the database editor. Select your database and click on “edit password”. Then copy this password into wp-config.php, upload it with Filezilla to your public_html folder, overwrite the existing wp-config.php and reload the website.

If it doesn’t fix the problem, you need to check that the host is correct. Most hosting companies use “localhost” but make sure to input the host your company uses correctly. If you can’t find it in the control panel you can either contact customer support for confirmation or, if your hosting company is well known, search on google how to find the host they are using.

error establishing a database connection corrupted database

2. Your database is corrupted

If you see Error establishing a database connection on the front-end, but when you go to wp-admin the error displayed is One or more database tables are unavailable. The database may need to be repaired as your database is most probably corrupted.

It sounds scary but it’s extremely easy to repair. You just need to insert the following code into wp-config.php

define(‘WP_ALLOW_REPAIR’, true);

Make sure you place it just before the: “That’s all, stop editing! Happy blogging.”

Once you’ve saved the new wp-config.php and uploaded it to your public_html, go to YOURSITE.COM/wp-admin/maint/repair.php. Here 2 options will display to repair your database:

Repair your database or Repair and Optimize your database

Personally I prefer using the second, but both will work. Once the database is repaired and your website is not displaying the Error Establishing a Database Connection, don’t forget to go to wp-config.php and delete the define(‘WP_ALLOW_REPAIR’, true); code. It’s really important as it’s a big security vulnerability and can be used to damage your website.

error establishing a database connection  corrupted files

3. Your files are corrupted

Another possible reason for the Error Establishing a Database Connection. A theme or plugin could be damaged and be causing the problem. To troubleshoot and find the culprit, start by going to your WordPress admin area, then go to Appearance > Themes, disable the current theme and activate one of the default themes.

You can also troubleshoot the plugins by disabling them one by one.

If you must use the theme or plugin causing the problem, unless an update has caused this and you can revert to a backup of your site previous to the update, you might need specialized help fixing it as it can be quite technical.

If you can’t access the admin area to disable the theme or plugins, you can do it via FTP. Access your public_html folder and go to wp-content. Go to your theme’s folder and change it’s name. This will disable it.

To disable the plugins, simply rename the plugins folder. Then rename it back again and it will restore all the plugins but deactivated. Then access the admin area and activate the plugins one by one to find which one is causing the problems.

error establishing a database connection server down

4. The database server is down

If none of the previous fixes has worked, it’s probable that your MySQL database server is down. There’re tons of reasons that could be causing this, one of the most common being excessive traffic. This happens when the server runs out of capacity to manage so many concurrent users.

If you have several sites hosted on the same hosting account, visit those to see if the database server is down. If you only have 1 site, In this case to your hosting control panel, access PHPmyAdmin and try to connect. If the connection works, then the problem might be the user not having sufficient permission.

Create a new file and name it testconnection.php and paste this code inside it:

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

Replace the username, password and host for the ones your database uses. Then go to YOURSITE.COM/testconnection.php

If the connection is successful it means your user has sufficient permission, so the problem will either be excessive traffic, which you can fix upgrading your server, or a typo on the database credentials, so check the wp-config.php file again.

2 alternative methods to fix error establishing a database connection

2 Bonus Methods When Everything Else Fails:

If none of the previous 4 methods worked we’re probably talking about a poltergeist inside your server a very rare case, so trying the following 2 bonus methods can solve it.

Bonus Method 1: Updating WordPress

Go to your hosting control panel, access PHPmyAdmin, access your WordPress database and go to SQL database and paste the following code:

UPDATE wp_options SET option_value=’YOUR_SITE_URL’ WHERE option_name=’siteurl’

Change siteurl for your site url and wp_options to your own table name.

Bonus Method 2: Hit it with a stick Reboot your server

We’ve all hit the TV repeatedly when the signal was bad back in the days, and when the computer froze we turned it off and on and it was magically fixed, so you can try and see if by rebooting your server the problem gets fixed. You should have the option to re-start the server if you’re in a VPS or dedicated server.

And that’s it! You have now in your arsenal 4 common methods and 2 bonus methods to fix the Error Establishing A Database Connection quickly.

Summary: Error Establishing a Database Connection: Fix It in Less Than 30 minutes

Method 1: Verify that your user, password, database name and host are correct.

Method 2: Repair your corrupted database

Method 3: Troubleshoot your theme and plugins for corrupted files

Method 4: Test if your database server is down

Bonus Method 5: Update WordPress through PHPmyAdmin

Bonus Method 6: Reboot your VPS or dedicated server.

Recommended Posts