Error Establishing a Database Connection in WordPress

If your WordPress website is displaying, ‘Error establishing a database connection’, it means that there is an error that needs your attention. Why eliminating this error is so important? It is because this error is causing your WordPress website to become inaccessible to users. The reason behind this could be simply understood by the error itself. That is, WordPress is unable to make a connection to the database. No connection to the database means no accessibility to the users. It is one of the most prevalent errors in WordPress. You might have understood the error, but it might be a bit difficult for beginners to troubleshoot. The WordPress database connection can be affected by several factors. To find an easy solution for this error, continue reading the article.

Database:

For easy storage, organization, and retrieval of data into another software, we use a particular type of software, also known as a Database.

WordPress in itself is a content management system. It is not very difficult to understand why WordPress needs a database. The answer to this question is simple. That is, to store the content of all the users and other data of the website in one place. Every time there is a viewer on the website of a WordPress user, WordPress connects to the database each time to retrieve the data and store any new data.

To connect to the database, WordPress uses the below information, stored in the WordPress configuration file of the user, also called wp-config.php.:

  • Database name
  • Database username
  • Database password
  • Database server

Causes of the Error:

The causes behind the error establishing a database connection could be:

  • Incorrect database information in the WordPress settings (incorrect credentials)
  • A corrupt database (the database files are corrupt)
  • An irresponsive database server (database server is down)

An error will be generated to show the failure in connecting to the database server if any of the information required for connecting to the database is wrong. We will try to fix this error step-by-step.

Step 1: Check Your WordPress Database Credentials:

Incorrect database credentials in the WordPress settings is usually the reason behind the error in the database connection. It especially occurs when the WordPress site is moved to a new host. The wp-config.php file is the WordPress configuration file. It holds important WordPress settings including the WordPress database credentials.

Open the wp-config.php file and look for the following lines.

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

Recheck the database name, username, password, and database host and ensure that they are exact. To confirm the above credentials from your WordPress hosting account dashboard, follow the below steps:

  • Log in to your hosting account.
  • Click on MySQL databases.

  • The database management page will open in the hosting dashboard.
  • Look for the database name and username.

  • The links to change the password for different database users can be found below.

  • Go to your wp-config.php file if you need to change the credentials, once you have checked them.
  • Go to your website.
  • The database connection error should be gone by now.

If the error is still there, then continue troubleshooting and move to the next step.

Step 2: Check Your Database Host Information:

After ensuring the exactness of your database name, username, and password information, you need to ensure the exactitude of the database host information. While most WordPress hosting companies use localhost for hosting the database, some may use separate servers for the same. This being the case, you now need to verify your database host information from your WordPress hosting company, to ensure that the database host information is correct.

Step 3: Repair WordPress Database:

The need for repairing your WordPress database arises when the error on the wp-admin is different from establishing the database connection. It could be “One or more database tables are unavailable. The database may need to be repaired”.

Follow the below steps to repair your WordPress database:

  • Add the below line, just before ‘That’s all, stop editing! Happy blogging’ line in your wp-config.php file.
    define('WP_ALLOW_REPAIR', true);
  • Visit the page:
  • http://www.yoursite.com/wp-admin/maint/repair.php, to see the settings.

  • Log in is not a necessity to access the database repair page.
  • Remove the above code from the wp-config.php file after the execution of repairing and optimizing your database.

Step 4: Check if Your Database Server is Down:

If the above three reasons do not exist, the last reason for the issue of WordPress not connecting to the database could be that the database server might be down. Because of heavy traffic on the database server (MySQL server), the host server might not be able to manage the load. The ultimate result will be slowing the website or showing an error while visiting the website. This problem usually arises when an individual is on shared hosting.

The most competent solution to this problem is to check the responsiveness of the MySQL server. For multiple sites on the same server, the other websites should be checked to ensure if the SQL server is down. For the server with a single website follow the below steps:

  • Proceed to your hosting dashboard.
  • Try to access phpMyAdmin.
  • Connect the database.
  • After connection, move ahead to verify if your database user has sufficient permission.
  • Create a new file called testconnection.php.
  • Paste the following code in the newly created file.
    <?php
    $link = mysqli_connect('localhost', 'username', 'password');
    if (!$link) {
    die('Could not connect: ' . mysqli_error());
    }
    echo 'Connected successfully';
    mysqli_close($link);
    ?>
  • Replace the username and password in the above code.
  • Upload the testconnection.php file to the website.
  • Now, access the file via a web browser.
  • It will be verified that your user has sufficient permission if the file and the code inside the file are connected successfully.

In another case, the troubleshooting needs to be continued.

  • Go back to your wp-config file.
  • Ensure the correctness of every possible data.
  • Re-scan for typos, if needed.

Some Other Suggested Solutions:

You can try the below steps if nothing from the above steps works for you.

Step 5: Update WordPress Site URL:

To update the WordPress site URL, follow the below steps:

  • Go to your hosting account dashboard.
  • Access phpMyAdmin.
  • Select the WordPress database.

  • Scroll to the top.
  • Click on the SQL menu.
  • Enter the below MySQL query.
     UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
  • In the above query, replace your site URL and replace wp_options with your table name.

Step 6: Reboot the Web Server:

Rebooting the web server can be suggested for the local servers, dedicated servers, and virtual private servers (VPS) to restart the server. Through this step, the temporary glitches will be removed, if there will be any.

Step 7: Contact your web hosting company:

If you have chosen the right WordPress hosting companies, they will undoubtedly listen to you, if you will ask them for help. They might suggest you the solution or fix the issue themselves, depending on the problem.

Hiring a WordPress Developer is also a very good idea. Follow whatever step is suitable for you. The end goal is to resolve the error in establishing a database connection in WordPress.

 

Please follow and like us:
Content Protection by DMCA.com