Home Tech Trends Fix Error Establishing Database Connection In WordPress

Fix Error Establishing Database Connection In WordPress

Error Establishing a Database Connection is one of the so many errors that one could come accross while using WordPress. So frustrating and also very bad for website's reputation and SEO.
Whenever a page is loaded on your website, it’s built on-the-fly. PHP code is used to access your MySQL database and retrieve every bit of information needed to create the page.
WordPress is built using PHP and MySQL. The information that makes up your website is stored in a MySQL database, and PHP scripts tell WordPress how to store and retrieve that information to create and update your pages.
WordPress is perhaps, a free and extremely popular site management platform, but this is a fairly well known WordPress Challenge "Error Establishing Database Connection".
When you get the message “Error establishing a database connection” WordPress is telling your that your PHP code is unable to connect with your site’s MySQL database to retrieve the information it needs to put a page up running.
Error Establishing Database In WordPress fix

Causes of Error Establishing Database Connection

  • Incorrect login credentials. Your database requires two separate items authenticate any user, WordPress or otherwise. That’s your login name and password. If those credentials have been changed, your database can’t be accessed, and no info means no page.
  • Your database or file is corrupt. It does happen, for several different reasons, from malware or bad plugins, to Challenges at your host server.
  • Server not Available. Sometimes the server just can’t be reached. It may be down for maintenance, overwhelmed with traffic and unable to support new connections. Maybe there is a Challenge with your ISP and Internet connection, or browser.
  • How to Repair and Fix Database Connection Error

    To repair your database, you’ll need to access your wp-config.php file, which contains your WordPress installation settings and configuration.
    Once you have wp-config open, add this line of code to the bottom:

    define( 'WP_ALLOW_REPAIR' , true );

    This line will enable you to optimize and repair your database by navigating to www.example.com/wp-admin/maint/repair.php (just replace “example.com” with your actual URL). Please note that once you’re done repairing your database, be sure to remove the above line of code you added to wp-config. This will disable access to the repair page and prevent external invading from messing with your database.
    If you changed your root password, or the database user password, then you will need to change this file as well. First thing you should always check is if everything in your wp-config.php file is the same.

    define( 'DB_NAME' , 'database-name' );
    define( 'DB_USER' , 'database-username' );
    define( 'DB_PASSWORD' , 'database-password' );
    define( 'DB_HOST' , 'localhost' );

    Depending on the host, your DB_Host value might not always be localhost. Sometimes it will be IP address e.g 

    define( 'DB_HOST' , '' );

    You can see the information WordPress needs to connect to your database: database name, username, password, and host computer. If any one of these is wrong, WordPress can’t connect to the data it needs. If in doubt, you can check on your database from your cPanel dashboard, which usually has a feature called PHPMyAdmin for using databases outside of WordPress. You might want to make a backup of the database first. Since you’ll be directly changing your site’s data from PHPMyAdmin, a mistake at this point could cause even more damage.
    One way to check on your database outside of WordPress is by using PHPMyAdmin, a tool for accessing and editing MySQL databases. PHPMyAdmin is included with most hosting plans, and you can find it in your cPanel dashboard.
    When you click on your database name, you’ll a screen listing the names of the tables in the database. If in doubt, click “Browse” next to the table named wp-options, and you should find your site name and URL along with other settings. If it’s there, and the name matches your wp-config entry, you have the right database. If the URL is right, but your wp-config file is wrong, change the name there to match the right database.
    Now you know whether or not you have the correct database name in your wp-config file. If it’s not correct, go ahead and fix it in wp-config.
    Now let’s see if your username and password is correct.
    To do so, create a .php file and add the following,  for example sqlsolution.php:

    $test Connection = mysql_connect( 'localhost' , 'root' , 'password' );
    if (! $testConnection ) {
    die ( 'Error: ' . mysql_error());
    echo 'Database connection working!' ;
    mysql_close( $testConnection );

    If you cannot connect to the database by going to phpMyAdmin, then you know it is something with your server. It does not necessarily means that your MySQL server is down. It could mean that your user does not have sufficient permission.
    Furthermore, once you’ve created that file, just navigate to the URL of the file in your browser (e.g. examplecom/sqlsolution.php). You’ll see either a successful connection message, or an error with more details.
    If the username and password isn’t working, we can just create a new one.
    If you can’t find the given username and password in the table, go ahead and create a new username and password in the wp-options table - just make sure you also apply the changes to the wp-config file.
    The best way to do this is from another cPanel tool called “MySQL Databases”. Click on that and scroll to “MySQL Users: Add New User”. Create you new username and password, and make sure the changes are updated in wp-config. Click “Create User” and then find “Add User to Database”. Select the username and password you just created, and click “Add”. If you’ve made the exact same entries in your wp-config, your MySQL credentials are now correct.
    With these processes, you can now fix error establishing database in WordPress. If not, quickly contact your web hosting service provider for effective solution as soon as possible.