Md Obydullah Follow I'm Md Obydullah. I build open-source projects and write about Laravel, Linux server, modern JavaScript and web development.

How to Solve Error Establishing a Database Connection in WordPress

Last modified on March 9, 2019 4 min read

Daily users of the internet often encounter many web errors; they can be very frustrating especially if it’s a beginner using WordPress. One of the most frequent errors faced is the error establishing a database connection.
After much research and facing this issue countless times ourselves, we’ve compiled the best possible solutions for you to save your time and effort. So if you’re an individual stuck in this problem, then keep on reading.

Note: You need to have an ample backup before you try to make any changes in the database.

Why are you facing this error?

The main cause of this error is that WordPress is unable to establish a database connection. This could be because of many reasons mentioned below

  • Database login, username and password is entered incorrectly or changed
  • The database server is nonresponsive
  • The database has become corrupted
  • Let’s try troubleshooting this problem now.

    Is there a problem in /wp-admin/ too?

    This problem could either be on the front end or back end (wp-admin) of WordPress, or on both ends. So you need to take a look at those ends to make sure which solution will suit you best.

    If the issue is encountered only on the back end which says “One or more database tables are unavailable. The database may need to be repaired”
    Then you need to repair the database by:

    1. Add this line in the wp-config.php file, before the line ‘That’s all, stop editing! Happy blogging’

    define('WP_ALLOW_REPAIR', true);

    2. Go to the page: http://www.yoursite.com/wp-admin/maint/repair.php
    3. See the setting

    How to Fix 505 Internal Server Errors in WordPress

    In order to repair a corrupt database, when this define is set, the user does not necessarily be logged in to access the functionality. When you are finished optimizing and repairing the database, remove the line from wp-config.php.

    If the instructions above didn’t solve the problem, then try following our other solutions.

    Check the WP-Config file

    While installing WordPress, the wp-config.php is of importance. All the details related to connection to the database should be written in this file. This file needs to be changed even if you modify the root password or user password of the database.

    The first action to be performed is to check whether everything in the wp-config 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 in which your WordPress is running, the DB_Host value will not always be named as localhost. For some popular hosts like Site5, HostGator, BlueHost it would remain the same, but for the others it might change accordingly.

    If you’re on a local server environment, and you are running the WordPress eg MAMP, the problem might resolve by changing the DB_Host to your IP address. Different host servers will have different IP addresses, check yours and make the replacement.

    define('DB_HOST', '127.0.0.1:8889');

    If the problem is continuing, let’s take a look at the server side.

    Check your Web Host (MySQL Server)

    If your site is generating a huge amount of traffic, and you observer this error in the presence of high traffic, it means that the host server (especially shared host) is incapable of handling the load. Your website will start showing errors to some users and will definitely slow down. For this error to resolve you need to call or live to chat your hosting provider and request them to check whether your MySQL server is responding or not.

    If you want to take matters into your own hands and check the MySQL server yourself, you need to test some of your other sites that run on the similar server and check if they are encountering an issue. If you don’t have other sites on the same server then perform the given instructions

    1. Go to cPanel
    2. Access phpMyAdmin
    3. Connect to the database
    4. If the connection is established, create a new file testconnection.php
    5. The following code should be pasted in it

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

    Note: Replace the username and password with your own password and username.

    If the connection has established successfully, then the user has the required permission, and we need to troubleshoot this problem further. Revisit the wp-config file and ensure that there were no typing errors and everything is correct.

    If the phpMyAdmin could not be accessed, then it means that the server is experiencing some issues. It could be down, or the user might not have enough accessing permission.

    When we were facing this problem, MySQL server was active and running. Every other site was running correctly except for this one. We then went to phpMyAdmin and got the following error.

    #1045 – Access denied for user ‘foo’@’%’ (using password: YES)

    We immediately contacted our host server, and their customer support found out the root cause of the problem. All they did was restore the user permissions and our site started to run perfectly.

    So if you’re having this same error, you need to get in touch with your host provider and let them handle the situation.

    Other solutions tried by users

    We have compiled a list of other solutions, which worked successfully for different people but it might not work for you. Before trying this you need to have sufficient backup, to make sure you don’t lose any data.

    Solution 1: A user faced an error which said that the database should to be repaired. The error was not resolved even when the database was replaced. Trying many fruitless techniques by hit and trial, he discovered that the error was in the site URL. He then opened phpmyadmin and ran the following SQL query.

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

    You need to write your own URL in the YOUR_SITE_URL. By any reason you have changed the default WordPress database prefix then the wp_options mentioned above will be different for you.

    Solution 2:
    One user said that through testconnection.php he connected successfully to the database. He then changed wp-config.php user to root user, and the error was resolved. After that he changed the settings back to database-user, the error kept at bay, and did not come up.

    Solution 3: Another user said that his problem was resolved, after he deleted the content present in active plugins, which is in the table of wp_options. After that, he edited the contents of recently_edited, and Voila! It worked.

    Solution 4: This method has been tried by many users and has worked for most of them. The solution is to just upload a new WordPress.

    So that’s the end of our blog, we hope we helped you with this problem.

    You're welcome to suggest any article to write!

    Md Obydullah Follow I'm Md Obydullah. I build open-source projects and write about Laravel, Linux server, modern JavaScript and web development.

    Leave a Reply

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