Errors and problems

How to fix the database connection error in WordPress?

Error connecting to the database is one of the most common issues WordPress users may encounter. It can stem from various causes, such as incorrect database login credentials, corrupted files, or server issues. In this article, we'll discuss six common methods to fix this error and help you restore the functionality of your website.

What is the "Error Establishing a Database Connection"?

The "Error Establishing a Database Connection" occurs when a website is unable to connect to the WordPress database, preventing the page from loading. The most common causes of this problem are incorrect login details or database corruption.

How to Fix the "Error Establishing a Database Connection"?

Check if Your Domain Points to Your Hosting Provider:

The first step in resolving a database connection issue is to ensure that your domain points to the correct hosting provider. If your domain doesn't point to the server where your database is located, it can lead to connection errors. You can check this by comparing the nameservers in the hosting control panel and WHOIS tool. If the nameservers don't match, you need to update them at the domain registrar.

Verify Database Login Credentials:

The initial step in troubleshooting is to check if the database login credentials are correct. This information is stored in the wp-config.php file, usually located in the main directory of your WordPress site. The file contains four crucial pieces of information that must be accurate for the connection to occur:

define('DB_NAME', 'database_name'); define('DB_USER', 'username'); define('DB_PASSWORD', 'password'); define('DB_HOST', 'database_host');
PHP

To verify this information, open the wp-config.php file and compare the values with those stored on your server. If the data doesn't match, you need to update it.

Repair a Corrupted WordPress Database:

If your login credentials are correct and the issue persists, your database might be corrupted. WordPress has a built-in database repair mode that you can activate by adding the following code to the wp-config.php file:

define('WP_ALLOW_REPAIR', true);
PHP

Then, go to https://yourdomain.com/wp-admin/maint/repair.php, where you'll have the option to repair the database.

Check Your Database Server:

A database connection error may also occur when the MySQL server is unavailable. A common cause is exceeding the maximum number of connections to the server. The database server has a maximum number of clients it can handle simultaneously. If it reaches its capacity, it may be unable to accept more connections. In this case, it's recommended to contact your hosting provider to check the MySQL server's status.

To verify if your MySQL server is functioning correctly, create a new PHP file and insert the following code:

<?php 
  $link = mysqli_connect('localhost', 'username', 'password'); 
    if (!$link) { 
      die('Could not connect: ' . mysqli_error()); 
    } 
  echo 'Connected successfully'; 
  mysqli_close($link); 
?>
PHP

Replace 'username' and 'password' with your database login credentials. Save the file on your site and open it in a browser. A successful connection means that your database user has sufficient privileges.

Update WordPress Site URL:

Attempt to update the WordPress site URL using phpMyAdmin. Access phpMyAdmin from the hosting account's control panel, select the WordPress database, click on the SQL menu at the top, and enter the following MySQL query.

Remember to provide your own site URL and change wp_options to your table prefix, as you may have altered the WordPress table prefix.

sql

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

Fix Corrupted Plugin or Theme Files:

Besides corrupted databases, damaged files in plugins or themes can also cause a database connection error. Often, this is due to a faulty theme or plugin. To address this, try deactivating all plugins and switching to the default theme. If this resolves the issue, it indicates that one of your plugins or themes is faulty.

To quickly test if the problem lies with a plugin, change the name of their directory via the file manager or FTP client. The directory is usually located in /wp-content/. Change the name to plugins-off and see if the connection is restored.

Repair Corrupted WordPress Files:

Download the latest version of WordPress from WordPress.org and upload it to your domain directory, replacing all files except:

  • /wp-uploads/ directory
  • .htaccess file
  • wp-config.php file
  • user.ini or php.ini files
  • Any other files, like domain verification for Google Search Console

These should be removed and replaced with the new files.

Contact Your Hosting Provider:

If none of the above methods resolves the problem, contact your hosting provider. It could be a server issue requiring technical intervention.

Remember to always perform backups before taking any actions to prevent data loss.

Full care for WordPress.
Turn off your worries, turn on the security and care for your WordPress
View plans

Leave a Reply

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