Learn Web Design, Coding & More. No Risk. No Contracts. Learn More

Error establishing a database connection – How I Fixed It On My WordPress Blog


My love for WordPress was threatened when i woke up one morning, i decided to check my blog, i was shocked when i saw Error establishing a database connection. If you experiencing this problem, then this article will guide you on how to solve this error.

Causes & Solution To WordPress “Error establishing a database connection”

Below are some possible causes of this error and how to fix them.

  • Database Settings in Wp-config.php file
    If you are experiencing database error, the first thing you need to do is, navigate to wp-config.php file located at the root of your WordPress install folder and check if your MySQL Database settings are correct.
    The MySQL code snippet is as below:-

    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define('DB_NAME', 'your database name here');
    /** MySQL database username
    */
    define('DB_USER', ' database username here');
    /** MySQL database password
    */
    define('DB_PASSWORD', ' yourpasswordhere');
    /** MySQL hostname */
    define('DB_HOST', ' localhost');
    

    NOTE: Text inside /* */ are
    comments, for information purposes only. Also, at define(‘DB_HOST’, ‘ localhost’);, there is a good chance you will NOT have to change it. If you are unsure, try installing with the default value of ‘localhost’ and see if it works. If the install fails, contact your web hosting provider.

  • Mysql Database Broken
    Having broken database or sometimes a database table can lead to inability for WordPress to connect to database. In my ordeal, this was actually the cause.
    You can fix this problem with or without logging into PhpMyadmin.
    Solving This Problem Without PhpMyAdmin – To do this, add
    define('WP_ALLOW_REPAIR', true);

    to wp-config.php.
    Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php
    wordpress database repair page
    Please note that 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.

    With the above done, your database would have been repaired and your site up and running.
    But in my case, the repair was unsuccessful, but i was happy the process told me the database table that was corrupted, so i had to repair the table via PhpMyAdmin.

    Using PhpMyAdmin – You may decide to do a PhpMyAdmin Database test to detect the problem. to perform this test: Login to cPanel and click MySQL Databases, check all the tales, look for the drop-down called “Check DB”.check database for problem in cPanel
    This should reveal the cause of the problem.

    To Repair the detected database table, Go back to MySQL Databases -> Tick the corrupt table -> Scroll bottom, look for the drop-down called “Repair DB”, Select the database table which is having trouble, Click the Repair DB button. Repair Database Via cPanel
    Once complete, reload your website.

Update

To prevent future occurrence of this problem, install WP-DBmanager. This plugin has an option for you to schedule how frequent your database will be optimised and repair, also it backup and mail you your database file as well as an option to drop/empty a table and run SQL queries without having to use phpMyAdmin. it a must have plugin.

With the above instruction adhered to, you must have successfully recovered your WordPress blog.
If you have any contribution to make or any question to ask, feel free to use the comment form below, we’ll love to hear from you.

Don’t Want To Miss Our Update, Subscribe Now
About collizo4sky

Agbonghama Collins is a webdeveloper and an internet geek. I love writing and sharing the little idea & information i know. Hope you enjoyed my Article.

You can get in touch with him via these modes: Follow him on Twitter, add him on Facebook or on Google+

  • http://dawahnet.com Dawah

    Dis is really fantastic, dis is d problem many on wordpress are getting dis tym around, thks for this…

    • http://wapden.net collizo4sky

      Thanks bro.. Am glad you love it

  • http://www.milesweb.com Milesweb.com

    This error also occur when you enter the incorrect details in wp-config.php OR if required privileges are not assigned for mysql user. Thanks for your share as well.

    • http://wapden.net collizo4sky

      thanks for your contribution. the article was just a personal experience i deem fit to share.