How To Fix The Error of Database Connection to the WordPress Site

How To Fix The Error of Database Connection to the WordPress Site

“Error Establishing a Database Connection” is one of those errors common to every WordPress user and annoying at the same time. This can cripple your site, making it unreachable to users. With some step-by-step approaches and a little bit of effort, though, this kind of error is resolvable. 

As a WordPress development services provider, we are going to highlight some of the causes and take a stepwise approach to correcting this error.

Error Establishing

Understanding the Error

The error “Error Establishing a Database Connection” usually means your WordPress site cannot talk to your database. By default, WordPress uses the MySQL database to store all information from your website, including posts, pages, user data, and more. If WordPress can’t reach this database, it can’t load your website, thus serving the error.

Error Establishing DB Connection

Common Causes of the Error:

  • Improper Database Credentials: WordPress uses a certain database name, username, password, and host to connect with your database. If any of these is incorrect, the connection will never be established.
  • Database Corruption: The database might get corrupted due to several reasons, including malfunctioning plugins or themes or an issue with the server.
  • Server Issues: Your web hosting server may be down or there might be some issue with the server’s configuration.
  • Entering the Exceeded Database Limit: A good number of hosting providers put either a limit on a database size or concurrent database connections. Upon crossing a limit on any count, connections to it may fail.

Solution—Step-by-Step Procedure:

1. Check Your Database Credentials:

Get sure your database credentials are accurate. The positioning of it is in your WordPress root folder’s ‘wp-config.php’.

  • Access the ‘wp-config.php’: Use an FTP service or your service provider file manager.
  • Look for the following details:

DB_NAME’: it is the name of the database.

‘DB_USER’: Your database username.

‘DB_PASSWORD’: Your database password.

‘DB_HOST’: host where the database server is kept. Usually, it is localhost.

If any detail isn’t correct, then do the correction and try reloading your website.

// ** 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' );

 

2. Repair Your Database:

After the credentials are right, find out whether your database corrupts itself.

  • Enable Database Repair:

Insert the following line into your wp-config.php file:

define(‘WP_ALLOW_REPAIR’, true);

Access the address ‘http://yourwebsite.com/wp-admin/maint/repair.php’ and click to start the repair.

Repair Database

  • Run the Repair: At this point, you will have two options: either to repair or to repair and optimize your database. Opt for the option of your choice.
  • Disable Repair Mode: After the process is complete, delete the line from your ‘wp-config.php’ because leaving it there keeps the site in repair mode.

 

3. Check Your Server:

Fixing the database still does not resolve the issue; there may be chances that it is your Server that creates the problem.

Contact Your Hosting Provider: Query if the server has some issues or your account may consume more resources than provisioned.

Check Server Logs: Your hosting provider could also provide access to the server logs to help diagnose the problem.

 

4. Test Your Database Connection:

Sometimes this is due to a hiccup in the database connection. You can test this by creating the following small PHP script.

  • Create New File: For this, create a new file in your WordPress root directory and name it ‘testdb.php’
  • Add the Following Code:
<?php

$link = mysqli_connect('DB_HOST', 'DB_USER', 'DB_PASSWORD',

  $link = mysqli_connect('DB_HOST', 'DB_USER', 'DB_PASSWORD');

  if (!$link) {

      die('Could not connect: ' . mysqli_error());

  }

  echo 'Connected successfully';

  mysqli_close($link);

  ?>

Replace the placeholders, (DB_HOST, DB_USER, and DB_PASSWORD), with actual values from your `wp-config.php`.

  • Run the Script:

Visit ‘http://yourwebsite.com/testdb.php’. If you get a message saying `Connected successfully`, your database connection is just perfect. If not, then either the credentials are wrong, or there is some issue with your database server.

5. Restore WordPress Files:

Sometimes, even these core files of WordPress can get corrupted and result in database connection issues. These files may be restored without losing your content.

  • Download a Fresh Copy of WordPress from WordPress.org’.
  • Replace wp-admin and wp-includes folder: 

Un-zip the downloaded files of WordPress on your computer. 

By using FTP, connect to your site, or by using your host file manager. Copy the fresh download’s ‘wp-admin’ and ‘wp-includes’ folders to your server.

Replace code by FTP

  • Visit Your Site: Go back to visit your site after performing the above step.

6. Reboot Your Server:

Rebooting your server can be an adequate solution in most cases. The problem might just be a temporary server glitch or reaching a type of resource limit.

  • Reboot via Hosting Control Panel: Almost all hosting providers will give you options to reboot your server or restart services via their control panel.
  • Check Server Errors: After the reboot, check your site again and go through the error logs to check other possible clues.

Preventing Future Database Connection Errors:

  • Regular Backups: Regularly back up your WordPress site along with the database to prevent any possible data loss in case of issues.
  • Optimize your database regularly to keep it healthy and reduce corruption in general.
  • Do monitor the server resources to ensure it does not reach a certain limit, which might bring about this error.
  • Use Reliable Hosting: Make sure you are on a reliable hosting with good server performance and support.

Conclusion:

Error establishing a database connection may be one of the scary issues, yet the steps are far from impossible to tackle: from checking your credentials to repairing your database if required, to issues dealing with the servers. 

Don’t forget to practice preventive measures so that you don’t see this error again. Some of the preventive measures include regular backups, database optimization, and monitoring server resources to help keep your WordPress site healthy.