Easily Fix WordPress Error Establishing a Database Connection Error

Posted by & filed under Malware, PHP, WordPress, WordPress Error.

Error Establishing a Database Connection

WordPress is the most popular content management system to create websites and blogs. It is very easy to use and allows creating powerful resources. But what if you create a website, enter the page and see that instead of page content your browser displays the following message: Error establishing database connection.

In the article, we are going to review why the error happens and the effective ways to fix it. This manual will be useful for all beginning WordPress developers.

 

What causes the error establishing a database connection Error ?

WordPress uses two main components in its work: PHP and MySQL. PHP is responsible for the functionality of your site, and MySQL stores all the data related to your site including pages, posts, plugins, widgets, etc.

When you enter the name of a site in the address bar, PHP establishes connection to the database and extracts the content of the requested page from it. When PHP cannot connect to MySQL for some reason, there is no data to display on the page and, instead of the content, the browser displays Error establishing a database connection.

Causes for ‘error establishing a database connection’ error :

  • Incorrect login and password to connect to the database. This information is provided by your hosting provider and is written in wp-config.php file located in the root folder of your website. It happens when you fill in the incorrect login data, or when the registration info changed, you it wasn’t corrected in the file.
  • Corrupted database. Unsuccessful settings, incorrect plug-ins, dropped connection when transferring data to the database could damage the database. In this case, you cannot access it properly.
  • The server where the database is located is not responding. The server may not respond to requests if it is disabled or is out of service. Usually, it is caused by the hosting provider technical issues.
  • The connection limit has been reached. Usually, the hosting company does not want the clients to reload the shared database and set a limit on the number of connections from one client, for example, 8. When your site is visited by many people at the same time, the number of connection will be reached and you will see an error message.

For each reason there is its solution. Let’s see how it is possible to fix the error depending on the reason why it was caused.

 

How to fix  error establishing a database connection message

Let’s go over each cause one and one and discuss the various steps we can take to fix the error.

Incorrect login and password

First of all, check whether the login and password are written correctly in wp-config.php file as this file is perhaps the most important file in the entire WordPress installation process. Here you can set the parameters for WordPress and connect to the database. If you changed your root password, or the database password, then you will need to change this file too.

error establishing database connection
  1. Open the FTP file manager (for example, FileZilla) and connect to the server of your hosting using the login, password and server name you received when registering. This information you will be able to find on your hosting control panel in the FTP section.
  2. Find the wp-config.php file which is located in the root WordPress directory, download it to your local drive, and then open it in Notepad or any code editor like Sublime, VS Code, Brackets, etc.                                                                       error establishing database connection
  3. Find the following lines: define (‘DB_NAME’, ‘database-name’); define (‘DB_USER’, ‘database-username’); define (‘DB_PASSWORD’, ‘database-password’); define (‘DB_HOST’, ‘localhost’); check whether the information is correct. You can find the database name, login, and password in the hosting control panel.
  4. Save the changes and upload the file to the server, apply its replacement.
  5. Try to open the website again.

Remember that the ‘DB_HOST‘ value will not always be localhost. Depending on the hosting, it can be different. For some hosting services, this is localhost. Other values ​​you can find with your hosting provider.

If there are no errors in this file (check for typos), then most likely something is wrong with the server or database is corrupted.

Corrupted database

Database recovery

To restore the database using WordPress, you need to take several steps. This method does not require special skills and will restore the database very easy.

error establishing database connection
  1. Connect to the server through the FTP file manager.
  2. Download the wp-config.php file and open it in the Notepad or any other code editor.
  3. In the last line, type the following code pattern: define (‘WP_ALLOW_REPAIR’, true);
  4. Save the changes, close the file, and upload it to the server. If necessary, confirm overwriting the updated wp-config.php file.
error establishing database connection

To perform the next step, in the address bar of your browser type the following address: ‘site-name.com/wp-admin/maint/repair.php’, where site-name.com is your site address. As a result, you will see a screen with two possible options: “Repair database” or “Repair and optimize the database.” Choose one of the options, and wait a bit while WordPress completes the operation.

Note: After restoring the database, be sure to open the wp-config.php file again and delete the line with the code that you added there. Otherwise, you risk giving criminals full access to your database and losing control of the site.

Overwriting the database from the backup to the last working version

If restoring the database and fixing the login information resolved the problem, you can continue working on the site in normal mode. Otherwise, possibly, the error occurs because of the damaged database. It will be necessary to restore it from a Database manager like PhpMyAdmin or Adminer, overwriting the database from the previously saved working backup.

Before proceeding with the recovery procedure, make a backup of the current files in order to avoid data loss. Be careful with the actions in database manager, because in this panel you directly work with the service tables of the database.

  1. Go to Adminer Dashboard using the login and password provided by the hosting provider when registering.
  2. You can create a backup of the current database by clicking the export Button. error establishing database connection
  3. In the left pane, you will see your database with its tables. In the work area, select the “Import” tab. error establishing database connection
  4. Click the “Choose File” button and find the file of the previously made backup.
  5. Click OK — all the other data does not need to be changed.

 

The database server is not responding

You cannot change the situation yourself. So, you need to contact the server hosting support team and try to figure out why the server is not responding. Most providers try to fix the failures as soon as possible.

You reached the connection limit

Most lower end hosting service plans have a connection limit or have really small disk space provided to  you. To expand it, you would probably have to buy the more expensive plan and spend more money. It is always advisable to not just check the price but check the features and server resources that you are getting for your money.

 

Error establishing database connection is a quite common error especially when you are a beginner. We hope that our tutorial helped you to fix it.

If you have faced this issue and fixed it using any other method, which is not mentioned here, please comment below and share with us

Leave a Reply