Fix the Error Establishing a Database Connection in WordPress

Trending 7 Sep , 2013  

Buffer

If you have been surfing the web for a while, you have at least seen this error a few times. Error Establishing a Database Connection is one of those curses that could be caused by many reasons. As a WordPress beginner, this could be awfully frustrating specially when it happened on its own without you changing anything. We ran into this issue yesterday on our own site. It took a little over 20 minutes to detect and fix the problem. While doing the research to find possible causes, we realized that there was no good article that covered everything. In this article, we will show you how to fix the error establishing a database connection in WordPress by compiling a list of solutions all in one place.

Note: Before you make any database changes, make sure you have sufficient backups.

dbrepair

Two days ago, out of nowhere, a particular table in the WordPress in codesupport.info went corrupted. The ‘wp_comments‘ table of the WordPress database just crashed without signs, warnings nor error messages. So how did I came to know? Here are the symptoms:

  • If you try to back up your database, you’ll be thrown with this error message – wp_comments is marked as crashed and should be repaired when using LOCK TABLES
  • Recent comments widget appears blank.
  • Top commenter widget appears blank.
  • Comment posted will not be written into DB, therefore will not be shown.
  • Comments section in WP Admin appears to have zero records.

The Fix / Solution

To fix this, you must have access to the wordpress database, either via PhpMyAdmin or Secure Shell (SSH). Most shared hosting account comes with phpMyAdmin, but either option you should be extremely careful because you dealing with the database. Any wrong move will cause more data lost.

Remember, the user does not need to be logged in to access this functionality when this define is set. This is because its main intent is to repair a corrupted database, Users can often not login when the database is corrupt. So once you are done repairing and optimizing your database, make sure to remove this from your wp-config.php.

If this repair did not fix the problem, or you are having trouble running the repair then continue reading this article as you might find another solution to work.

Using SSH Shell Securily

  1. Login to your shell
  2. Enter ‘mysql -u username -p database_name‘ to access databases
  3. Enter ‘show databases;’ to view all databases
  4. Enter ‘use database_name;‘ to engage the database
  5. Enter ‘show tables;‘ to show all tables within the database
  6. Enter ‘check table wordpress_table_name‘ to verify this is the corrupted table
  7. Fix it with ‘repair table wordpress_table_name

shell-fix

After searching the Internet high and low in hoping to find similar cases to help me identify and fix the problem, I end up with nothing. So I tried it my way and luckily enough, I managed to solved it. I thought writing the entry out will not only help those who suffered the same, also keep myself reminded just in case I hit on this error again in the future.

, , ,