How to Fix “Error Establishing a Database Connection” in WordPress

Estimated reading: 7 minutes 156 views

Introduction

Are you facing the ‘Error establishing a database connection’ pop-up on your WordPress website? This is a very common error that leaves your WordPress website inaccessible to the users. 

This error happens when WordPress is not capable to make a connection to the database. There are various elements which affect your WordPress database connection which makes it a bit difficult for beginners to troubleshoot.

In this article, we will guide you how to fix the error establishing a database connection in WordPress easily.

Prerequisites

  • an active KVM VPS
  • DirectAdmin control panel

What Causes Error Establishing a Database Connection in WordPress?

The ‘Error establishing a database connection’ issue can be caused by incorrect database information in your WordPress settings, corrupt database, or an irresponsive database server.

A quick summary of ways to fix the error connection database:

  • Check the database connection information
  • “Repair” WordPress Database
  • Disable WordPress plugins/theme
  • Replace the WordPress core file
  • Check if the server database is working or not 
  • Create a new database

1. Check the database connection information

The most common reason for database connection error in WordPress is incorrect database credentials.

At first, you need to check the connection configuration set in the file wp-config.php if it fits with the database information. 

In order to have the access to the file wp-config.php,  you need to access the File Manager in your DirectAdmin or through the FTP client such as FileZilla.

If you use DirectAdmin ->  File Manager, moving to the /public_html/ folder, right click on wp-config.php and choose edit.

You’ll be looking for the following lines in the wp-config.php file.

You need to make sure that the information for the database name, username, password, and database host is correct.

You can confirm this information from your DirectAdmin account dashboard.

You can find out your database name and the username there.

After that, try visiting your website again to see if the database connection error has gone.

If you can still see the error, then this means that something else is wrong. Continue reading for more troubleshooting steps.

2. “Repair” WordPress Database

If you website is not loading and you see this error while logging in “One or more database tables are unavailable. The database may need to be repaired” and on the main website you see the common error establishing a database connection. 

In this case, the WordPress has an automatic troubleshoot by “repair database”. You may need to activate this function. To do that, you have to access the file wp-config.php in the root directory files of WordPress.

Then open the file wp-config.php, and add the following line just before the line  * That's all, stop editing! Happy blogging. */

Save the settings, the newly added line will activate the database repair function for you. You can now access this function by directing to yourwebsite.com/wp-admin/maint/repair.php (fill in “yourwebsite.com” with your domain)

You can see that there are two options: Repair Database and Repaid and Optimize Database. You may choose one of them, but the second option will take more time to repair as it will be also optimized. 

Note: After the database repair is complete, make sure to delete the line added define( 'WP_ALLOW_REPAIR', true ); in the wp-config.phpThis will put and end to the repair function and prevent your website from unwanted intrusions.

3. Disable WordPress plugins/theme

If both of solutions above are not successful, it’s high time to check your WordPress plugins. If you mass-disable all the plugins and the websites works again, there is a possibility that one of the plugins is causing the Error Establishing a Database Connection.

Here is how to disable all your WordPress plugins and see if the “error establishing a database connection” can be fixed: 

  • Log into your DirectAdmin account -> File Manager or FTP client 
  • Access the WordPress core files in public_html ->wp-content, you will see a folder called “plugins”
  • Change the folder name into anything such as “plugins-disabled”. This will make WordPress disregard it as a plugins folder.

  • Check if the error establishing a database connection has disappeared

If the error is eliminated on the website, it means that the problems lies in one of the WordPress plugins. But how can we find out which plugin exactly is causing the error?

After disabling all the plugins, revert the plugins folder to its original name. By doing this, the Word/press plugins are now in the inactive state in the Admin area. You can now active the plugins one by one and reload your website until you have error establishing a database connection pop-up again to find out which plugin causes the error. 

4. Replace the WordPress Core Files

Make sure to backup your WordPress site first: https://green.cloud/docs/how-to-backup-wordpress/

If everything seems to be correct, and WordPress still cannot connect to the database, then you may have to replace WordPress core files.

The file might be contaminated or broken which will make WordPress not working when connecting to the database. In such case, you can replace those core files with the new core WordPress files that have never been modified before.

Replace the core WordPress file in the DirectAdmin following these steps: 

  • Download the newest version of WordPress at: https://wordpress.org/download/
  • Extract Zip/Rar file
  • Login to your DirectAdmin account -> File Manager or login via FTP Client by using FileZilla or BitviseSSH
  • Delete the file config.php and the folder wp-content
  • Upload file config.php và wp-content folder from WordPress that you have just extracted

5. Check if Your Database Server is Down

If everything seems to be correct, and WordPress still cannot connect to the database, then your database server (MySQL server) may be down.

This could happen due to heavy traffic on a server. Basically, your VPS/Server just cannot handle the load. Your site will get really slow and for some users it may even output the error.

In which case, you can have to check your VPS or contact us via Livechat/Support Ticket so we can check the VPS for you.

If you have other websites running on the same VPS/server, then you can check those sites to confirm that your SQL server is down.

If you do not have any other site on the same VPS/Server, then you can go to your DirectAdmin account and try to access phpMyAdmin and connect the database.

If you can connect, then we need to verify if your database user has sufficient permission. Create a new file called testconnection.php and paste the following code in it:

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

Make sure to replace the username and password. You can now upload this file to your website and access it via web browser.

If the script connected successfully, then it means that your user has sufficient permission, and there is something else that is wrong.

Go back to your wp-config file to make sure that everything there is correct

6. Create a new database

If the above-mentioned troubleshooting tips failed to fix the database connection error on your website, then you may have to create a new MySQL database and User

Login to your DirectAdmin account -> MySQL Management -> Create new database

After the MySQL is created, you can import the database backup so that you do not have to do it from scratch. 

Other Solutions That Have Worked for Users

If the above-mentioned troubleshooting tips failed to fix the database connection error on your website, then you may try these additional steps.

  • Update WordPress Site URL
    Try updating the WordPress site URL using phpMyAdmin. Simply access phpMyAdmin from your DirectAdmin account dashboard -> Extra Features -> phpMyAdmin

Then click on the SQL menu on the top and enter the following MySQL query.

  • Rebooting Web Server
    You can try rebooting your VPS/Server in Client area or SolusVM VPS control panel

That’s all. Enjoy GreenCloudVPS services!

Leave a Comment