How to Fix Error Establishing a Database Connection in WordPress

If you have been surfing the web for a long time, you must be aware of this error. Error Establishing a Database Connection is one of those problems that occur with most of us. In particular, it becomes more frustrating and dreadful for the WordPress users because it happened on its own. In order to resolve this issue, we have done profound research to find possible causes of this error. Finally, we have listed down some comprehensive solutions in this article to fix the error establishing a database connection in WordPress.

Instructions

  • 1

    Why do you get this error?


    This error occurs because WordPress is somehow unable to create a database connection. There are several reasons for not creating a database connection by WordPress. Most probably your database login credentials are incorrect or might be changed. There are some chances that your database server is not responding properly. Your database might got corrupted. However, this problem also arises because of some server errors. Let’s take a look on how to deal this problem.

  • 2

    Does the problem occur for /wp-admin/ as well?


    First of all, you have to confirm that this error is occurring on front and back end of the site. If you are facing with same error message on both pages, then proceed further. On the other hand, if you’re getting a different error, then you need to work on your database. Add the below code in your wp-config.php file in order to resolve this issue:


    - define('WP_ALLOW_REPAIR', true);

  • 3

    Checking the WP-Config file


    WP-Config.php is usually considered as a significant file in your entire WordPress installation. It is a place where yo provide the details for WordPress so that you can connect your database. You will have to change the file, if you have changed your root password, or database user password. Make sure that 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');

    One thing you should keep in mind that your DB_Host value is not always similar to localhost. It will be varying, depending on the host. Some people say that by replacing localhost with the IP, they have resolved this issue.

  • 4

    Check your Web Host (MySQL Server)


    At times, it happens that this error establishing database connection when you have a lot of traffic on your website. It is because your host server is unable to manage the load. Eventually, your site gets slow down and for some users it will display some sort of errors. In this situation, you just call your hosting provider and inquire from them about the responsiveness of your MySQL.


    If you want to see that MySQL server is running, you have to do a few things. Open some other sites on the same server to see if they are running properly. In case they are into the same error, then it is confirmed that there is an issue with your MySQL server.


    In case, you don’t have any other site on the similar hosting account then follow the given steps;


    - Go to cPanel
    - Access phpMyAdmin
    - Connect the database


    Now, make a new file by the name of testconnection.php and insert the following code in it;




    < !--?php
    $link = mysql_connect('localhost', 'root', 'password');
    if (!$link) {
    die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_close($link);
    ?>

    The username and password should be replaced. If they connected without giving any error, then it means that your user has enough permission, and the problem is in some other part. Return to your wp-config file and see that everything is working correctly or not.


    If you’re facing the problem of connecting the database by going to phpMyAdmin, then there is an issue with your server. It does not mean that there is a problem with your MySQL. It might be a problem on your user end, and he might not have enough permission.

  • 5

    Solutions that Worked for Others


    According to a developer, his client was facing the same issue and his database needs some repairing. However, the error did not get resolved even after repairing the database. After making several attempts to resolve this issue, it was found that there was a problem with the site url. To resolve this issue, SQL query was run by going to phpMyAdmin;


    - UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Leave a Reply

Your email address will not be published. Required fields are marked *


2 − = zero