blog posts

How To Fix The “Error Establishing a Database Connection”

Setting up a WordPress website starts with purchasing a virtual hosting server, uploading website files, and creating a database related to the data and content needed for website pages. Now, if the connection between these files and the database has a problem, the Error Establishing a Database Connection error will occur.

The Error Establishing a Database Connection error means that the files of your website located on the web server cannot be connected to the database of your website, and this error prevents access to the site. Fortunately, this error can be solved like most WordPress errors, and you can solve it quickly by using the solutions provided in the article.

Not being able to connect to the database is one of the most common errors that require quick troubleshooting, because if you neglect to solve this error, your website will be unavailable to users, and this will directly affect traffic, credibility, and SEO rankings. You have an impact.

No need to worry. If you, like most WordPress users, have encountered the problem of not connecting to the database and you don’t know what to do, we are here to help you get through this stressful process easily.

So, if you have opened your website and have been staring at a blank page containing the Error Establishing a Database Connection error for a few minutes, we have good news for you. Just try the following solutions with full enthusiasm to bring the website back to its ideal state:

Table of Contents
  • What is Error Establishing a Database Connection?
  • Reasons for not connecting to the database
  • 1) Check the correctness of database credentials.
  • 2) Repair the WordPress database.
  • 3) Reload the main WordPress files.
  • 4) Identify and remove malicious plugins and themes.
  • 5) Check the database storage space.
  • 6) Restore your backup site.
  • 7) Check for server errors.
  • 8) Contact the hosting support.
  • 3 tips to prevent the problem of not connecting to the database in the future

 

What is Error Establishing a Database Connection?

As you know, your site consists of files, each of which has a specific role in the structure of the site. On the other hand, all the information of your WordPress site, such as post data, page data, meta information, plugin settings, login information, etc., are stored in the database.

WordPress is written using PHP language and uses Mysql as a database management system. In this way, when someone visits your website page, the PHP codes of the page are executed to retrieve the required information from the Mysql database and show it to the user on the page.

If this process is not carried out correctly, that is, the connection between the PHP code and the database has a problem, there is no data and information that can be loaded on the page and displayed to the user. This is where the user is faced with the error of establishing a database connection message instead of the content of the page.

 

This error not only prevents users from accessing the website but also prevents you from accessing the WordPress dashboard.

Reasons for not connecting to the database

Although the main reason for the error in establishing a database connection is the disruption of the connection between files and the database, this is a general view. In order to identify the root cause and look for solutions, you must first pay attention to the details to find a clue of the problem of not connecting to the database. Let’s get to know some of these reasons:

  • Using incorrect credentials such as database user name and password, database hostname, and wrong database name in the configuration file
  • Restriction of database user access privileges
  • Database failure due to reasons such as installing a malicious plugin or deleting some database tables
  • Increasing the database server load and not being able to respond to all requests
  • The database service is off or disabled
  • Corruption of database tables due to installation/removal of a plugin or execution of some MySQL operations
  • Damage to WordPress files due to hacker attacks and malware, failed updates or incomplete file transfer
  • There is a problem in the hosting department

These are among the main reasons that can prevent files from being connected to the database. Fortunately, each of them has its own solutions, which we will mention below.

1) Check the correctness of database credentials.

Website PHP files need database user credentials to access database information. This credential includes username, password, database host, and database name, which is stored in the wp-config.php configuration file. Now, if any of the items are incorrect, WordPress cannot connect to the database server, and this causes the error establishing a database connection.

Moving the site to a new server is one of the most important reasons for changing the login credentials to the database. To solve this problem, you must first refer to your new hosting panel. For example, if you use CPanel, click on mysql@databases from the databases section to see your website’s databases.

 

 

Then find the name of the database and the name and password of the user who has access to it.

Then, open the website’s wp-config.php file and check the database information.

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

 

If this information is consistent with the information you saw through the hosting, then there is no need to make any changes. If this information, for example, the database name is different from the database name in mysql@databases, you need to change it.

DB_HOST is also the location of host names, which is usually localhost or 127.0.0.1, but some web hosts use a different IP address or url instead of localhost. It is better to contact your hosting to make sure of this.

Of course, if you want to make sure that you can access the database with the desired login credentials, you can use a practical solution to check the access status of the database server:

For this, just create a php file called new-database-file.php in the root directory of your website.

Then, copy the following code into it.

<?php

$link = mysql_connect('localhost', 'user', 'password');

if (!$link) {

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

}

echo 'Success establishing database connection';

mysql_close($link);

?>

 

Change the user name and password based on the desired user information.

Then include this file in the address of your site and test it by the browser. (replace your-site-domain.com with your domain or site address)

2) Repair the WordPress database.

Sometimes the corruption of the WordPress database can cause the problem of not connecting to the database. Deleting or adding some database tables by plugins or new themes is one of the main reasons for database crashes.

3) Reload the main WordPress files.

Corrupt WordPress core files can also be a reason for not connecting to the database. An error in the WordPress code, a hacker attack, or a plugin or theme failure can break the connection between the database and the website.

To make sure, the error establishing a database connection is related to this issue or not, the best thing is to upload a new version of WordPress on your site.

Although this method has nothing to do with the content of your website and only updates WordPress itself, it is better to backup your WordPress files before making any changes in files such as access. or wp-config.php can be recovered.

If you follow the steps we will review below, your website content and plugins will not be affected.

Go to the WordPress site and download the latest version.

 

 

Then, unzip the file you downloaded and delete the wp-content and wp-config-sample.php folder from it.

Upload the remaining files to the WordPress website through cPanel or Zilla file, and when facing the message that the files are duplicated, select the Overwrite duplicate files option so that the corrupted WordPress files are replaced with new and healthy files.

We recommend that you clear the browser cache after this.

Then, test the performance of your site. If the problem of not connecting to the database is caused by corrupted WordPress files, it is expected that you will succeed in solving the problem with this solution.

4) Identify and remove malicious plugins and themes.

Although the variety of plugins and extensions is one of the most important strengths of WordPress, sometimes, installing a malicious plugin can be the beginning of all the weaknesses of your WordPress site. Unfortunately, adding some malicious plugins and codes to the content of the site can cause conflicts, errors and other strange behaviors, one of which is the problem of not communicating with the database.

In order to find out that this problem is caused by plugins, we have a simple solution for you.

Just open your website files using Filezilla or any ftp client you use and find the plugins folder.

Then right-click on it select the rename option and change the name of that folder.

By doing this, all the plugins of your website will be deactivated at the same time and now you can test the performance of the site without interference of any plugin and see if the problem of not connecting to the database is caused by the plugins or not. If you see that the problem is successfully resolved, then it’s time to identify the malicious plugin.

For this, you just need to change the name of the plugins folder to the same plugins and change the name of each plugin in that folder one by one to see which one of them being inactive solves the error establishing a database connection. This is how you can identify and remove malicious plugins so that your website works properly.

5) Check the database storage space

If the database does not have enough storage space to perform the desired tasks, there will be many problems in the performance of the database, and the error establishing a database connection is one of them. Inability to store data, non-execution of transactions and increased response time of the database are among the issues that can cause the database to fail.

To provide enough space for the database, delete unnecessary data such as old logs and redundant tables. If you are still facing this problem even with all the cleaning and preventions to prevent the space from filling up again, maybe it is time to upgrade your storage space.

6) Restore your backup site

As updating WordPress files is a great idea to solve the problem of connecting to the database, restoring WordPress backup files is also an effective solution to solve this problem. In fact, you want to get rid of the changes that have happened in WordPress now, and restoring the state where everything was in order can be the key to your liberation.

Of course, provided that you have previously backed up your site in a healthy and error-free state and use it to restore it now.

7) Check for server errors

If trying the above solutions did not help to solve the problem of not connecting to the database. Maybe it’s time to check the server errors to find more specific clues about the cause of the problem.

There are many resources you can refer to to discover the problem. /var/log/mysql/error.log and /var/lib/mysql/db-name/hostname.err are files that, if you have administrative access, you can identify and fix the problem related to the error establishing a database connection.

8) Contact the hosting support.

So far, you have tried your best to solve the problem of not being able to connect to the database by yourself, but if you still do not get results after trying these methods, it is time to contact the hosting support team to solve the problem themselves if necessary. or provide you with the necessary guidance to solve the problem of not connecting to the database.

3 tips to prevent the problem of not connecting to the database in the future

If you can manage your website with an informed approach and prevent it from threats and errors, the probability of facing problems such as not connecting to the database will decrease. So try to implement the following solutions:
1. Check your website files and configurations regularly.

It is necessary to carry out a series of checks on a regular basis to ensure that the website is properly configured. Also, it’s a good idea to keep the database authentication information that WordPress communicates with updated.

Don’t forget to apply any changes you make to the database user credentials in the wp-config.php file so there are no inconsistencies between them.

2) Work with a reputable hosting provider.

In order for your WordPress website to use its maximum potential to provide excellent performance, you need a powerful and scalable server or host that has high uptime and can handle traffic well. So try to consider your needs and then choose a reliable and professional hosting provider based on your expectations and needs. If you are confused in this field and don’t know where to start, you can trust Assam Server and make a strong start for yourself by visiting the hosting virtual server purchase page.

3) Think about the security of your website.

Security is one of the most important aspects of running an online business, and errors such as error establishing a database connection can be caused by attacks such as DDOS that occur due to weak website security. For example, if your WordPress website is hacked, hackers can gain access to your database and disrupt your business by adding new admin users.

So, by implementing solutions to increase server security, maximize the resistance of your website against any type of attack, and leave no openings for hackers to penetrate.

conclusion

The error establishing a database connection is one of the most common WordPress errors that occur due to a disconnection between website files and the database. Correcting the user credentials and using the internal database repair tool are among the most prominent ways to solve this error, but if necessary, try all the solutions above, and then if you do not get the desired result, contact the support department of the hosting provider to take measures. necessary to solve the problem.

Thank you for staying with us until the end of the article. We hope that reading this article was useful for you. If you have any questions, requests and need guidance, you can contact us by registering your opinion so that we can answer you as soon as possible.