Monday, December 10, 2018
Tips

Fix the “Error establishing a database connection” of WordPress

WordPress sometimes throws errors that while they may scare, the solution can be quite varied but not so complicated. This is the case of the message “Error establishing a database connection” that WordPress sometimes shows and that has many alternatives to solve it.

Why does this problem happen?

The problem of connection to the database is common when you try to install a clone of your web page, install your web manually or take your site to another location. WordPress tries to access via PHP the files of the database that is hosted on your hosting and when it can’t, it throws the message: “Error establishing a database connection”.

That could be happening?

WordPress files are corrupted, they can be damaged during updates that were not carried out correctly.

  • Too much traffic on the page.
  • The server is down.
  • Bad or recently changed credentials.

How to solve the error?

Before starting, we must make a copy of the database and download it in SQL format. The procedure will depend on your server but you can also use Phpmyadmin. You can also make a backup of your WordPress, as we always recommend.

Contact the server

A good server company has fast and efficient technical support. Contact them and make sure that the server database isn’t dropped and that there is no peak traffic on the server. They are likely to help you answer those two questions and even help you solve the error. If it doesn’t happen, let’s try next step…

Search for corrupt files

As we mentioned before, it’s possible that in some update procedure of themes or plugins the files have been damaged.

To verify this, access your server via FTP and enter the root file of your WordPress. Enter wp-content and change the name to the Plugins folder. Go back to your website and if the corrupt file is a plugin, it will be gone.

If it doesn’t work, start renaming each plugin folder and return to your site to rule out which one may be causing the error.

If the error persists, perform the same procedure in the Themes folder. If it doesn’t work, return the original name to each file and let’s see more solution options.

Check the location of the database

Contact your hosting company and request the address of your database. Find the line define (‘DB_HOST’, ‘localhost’); in your wp-config file and replace ‘localhost’ with the address. Go back to your website and verify. If it does not work, keep reading.

Check the database

Enter the wp-admin of your web page. If you see the same error that we talked about from the beginning, skip this step.

If you see another message, pay attention because, in the most recent versions, WordPress has added help to solve problems with suggestions for you to try to solve it.

If the message you see is “One or more database tables are unavailable […]” it means that you must repair the database.

You do this by entering via FTP and downloading the wp-config.php file. Then, in a notepad, edit the file adding define (‘WP_ALLOW_REPAIR’, true); at the end of the lines.

Upload the file to your website via FTP and replace the previous one. Enter the address /wp-admin/maint/repair.php of your website and you will see a message with 2 options: Repair the database or Repair and optimize the database. Click on the two buttons and wait for the automatic repair to finish.
Return to your web page and verify that the error has disappeared.

If it worked, remove the line of code that you had placed in the wp-config.php file and upload it again.

If the previous solution does not work, we should also check the connection of the server and the database. To verify that the MySQL server is working correctly we will upload a PHP file similar to the following one that you will see. You can also use Phpmyadmin to check this connection and know that the database is available.

If the error persists, there are more details to review. Let’s keep going!

Check the credentials of the database

WordPress uses user-specific credentials and password and usually do not change, but just in case …
Place these lines in your wp-config.php file and copy them to the technical support of the hosting company. Ask them to verify that the details are correct.

If something has changed, they will give you the necessary data so that you can replace it in your wp-config.php file. Upload it again via FTP and verify that the error has disappeared.

Restore the original WordPress files

This is one of the last instances, but it can work since sometimes errors can be produced by inadvertent modifications or hacker attacks. Download the most current WordPress version on WordPress.org and delete the wp-content file. Upload the file via FTP replacing the previous ones and verify.

Avoid the mistake in the future

There are two measures we can take to reduce the risk of the error manifesting again: 1. Contract quality hosting, optimized for WordPress, that has efficient technical support and ensures that the risk of falling is minimal. 2. Make backups of your website regularly, a quality hosting should guarantee constant and automatic backups of your WordPress.

If with all these solutions the problem persists, contact WordPress experts before modifying anything else. If this tutorial worked or you know other solutions, let us know!

Jefferson Maldonado
the authorJefferson Maldonado
UX WordPress Designer
Ux & Web Designer. Portafolio: http://maldonadoz.com/portafolio/ Divi Blogger, WordPress Expert, UX Designer, Business Consultant.

Leave a Reply