How to Fix the Error ‘Establishing a Database Connection’ in WordPress

As a website owner or administrator, seeing an “error establishing a Database Connection” on your website can send you into panic mode, especially if you’ve never encountered this before. If you are in this situation right now, you have nothing to worry about because you can fix this error without any permanent damage to your website. 

This article will show you the different ways to fix this error whenever it occurs, but first, it is essential to understand what the problem is and why it happens. 

What Is A Database Connection

Two technologies –PHP and MySQL, control WordPress functions.

PHP is the WordPress programming language, while MySQL is the database technology. WordPress is a content management system that uses a database to store and manage data. Every time there is a successful connection and your website loads entirely, there was an excellent database connection via the PHP commands. 

What Causes A Database Connection Error

For your website to establish a successful database connection, it will require the following credentials :

  • Database username
  • Database password
  • Database name
  • Database server

So whenever a database connection error occurs, WordPress cannot access the WordPress database information cannot be accessed. 

This can be due to some reasons:

  1. Incorrect login credentials: To access the database, WordPress requires the correct login username and password. When these login credentials are wrong, WordPress would not be able to access the database.
  2. Corrupted database and files: WordPress files and databases can get corrupted due to a malicious attack or failure to update themes and plugins. When the database or its files are corrupted, it can be challenging to establish a database connection.
  3. Database server downtime: Whenever the WordPress database experiences a downtime, it would not be possible to connect with it at that time.

How to Fix the Error Establishing A Database Connection

The first step to take before fixing a database connection error is to backup your website

This can be done by using a simple WordPress backup plugin. 

The error in establishing a database connection can be fixed in a few ways:

  1. Check your database login credentials
  2. Repair your corrupted database
  3. Fix corrupt files
  4. Check if your database server is down
  5. Troubleshoot with your hosting provider

Let’s break these solutions down into actionable bits.

1. Check Your Database Credentials

To access the database, WordPress uses a specific username and password. 

These connection details are stored in the wp-config.php file of your file’s manager. This file cannot change on its own, so you’d typically experience this whenever you change your hosting provider or made any changes to your site’s file. 

To fix this error, locate the wp-config.php file (this is usually found in the root directory) using either an FTP client or by going to your file’s manager. 

Check for the following line of codes :

  • define(‘DB_NAME’, ‘some_db’)
  • define(‘DB_USER’, ‘some_username’)
  • define(‘DB_PASSWORD’, ‘some_pass’)
  • define(‘DB_HOST’, ‘localhost’)

Make sure that the information provided for the different parameters is correct. You can confirm the details by going to the MySQL section of your SPanel dashboard. 

If the details don’t match, you can reset them by editing the wp-config.php file. 

Now, try reloading your site to see if it works. 

If it doesn’t work, keep reading this article for more troubleshooting steps.

2. Repair Corrupted Databases

If you are getting an error that says, “One or more database tables are unavailable’’, then this means that you have to fix the error by yourself.  Repairing a corrupted database is similar to the previous step we shared above. 

This time, all you have to do is add the following line of code to the bottom of your wp-config.php file:

  • define(‘WP_ALLOW_REPAIR’, true);

Then go to your browser and visit this link https://yourdomain.com/wp-admin/maint/repair.php. You will be presented with an option to either repair the database or optimize the database. 

Choose Repair database. 

After running this command, ensure that you remove the line of code that you initially added to the wp-config.php file. 

If you don’t do this, it simply means anyone can run a repair on your database.

3. Fix Corrupt Files

Your files can get corrupted as a result of a malicious attack by a hacker or during a file transfer process. This corrupted file is another reason why you get an error establishing a database connection. 

To fix this error, we are not going to touch your existing files. We are simply going to make a fresh WordPress installation. But first of all, it is vital to backup your existing website. After the backup, visit wordpress.org to download a new version. 

After downloading this file, unzip it on your computer and locate the wp-content folder as well as the wp-config-sample.php file.  After deleting these files, upload the remaining files via SFTP to your site. This will replace all corrupted files and install the new ones. 

Ensure you clear your cache files and reload your website to see if it works.

4. Check if your database server is down

A database server can experience downtime when there is an increase in website traffic. 

If this is the case, go to your SPanel dashboard to access phpMyAdmin and connect the database. 

If you can connect, then we need to make sure that you have enough database user permission. 

To check this, create a new file and paste the following code into it:

How to Fix the Error ‘Establishing a Database Connection’ in WordPress, 4. Check if your database server is down

Replace the username and password with the correct credentials and upload the file to your website using FTP and access your website via your browser.

If your user has sufficient permission, the script will connect successfully. 

If this is the case, it simply means that something else is wrong.

Conclusion

If you still encounter an error after trying any of these steps, your final resort is to reach out to your hosting provider to ask for help. At Scalahosting, we offer around-the-clock support services to help you out with your hosting needs. 

Feel free to reach out to our support team or leave us a feedback. If any of the above-listed steps worked for you, we would also love to hear your feedback.

Was this helpful?

What’s your goal today?

1. Find the right Managed VPS solution

If you’re looking for industry-leading speed, ease of use and reliability Try ScalaHosting with an unconditional money-back guarantee.

2. Make your website lighting fast

We guarantee to make your WordPress site load in less than 2 seconds on a managed VPS with ScalaHosting or give your money back. Fill out the form, and we’ll be in touch.

Please enter a valid name
Please enter a valid website
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

3. Streamline your clients’ hosting experience

If you’re a web studio or development agency hosting more than 30 websites, schedule a call with Vlad, our co-founder and CTO, and see how we can deliver unmatched value to both your business and your clients.

Photo

Need a custom cluster or professional advice?

Book a meeting and get a free 30-minute consultation with Vlad, co-founder & CTO of Scala Hosting, who will help you select, design and build the right solution - from a single data center cluster to a multi-region & multi-datacenter high availability cluster with hundreds of servers.

Book a free consultation

4. Learn how to grow your website in 2024

An all-star team of SEO and web influencers are sharing their secret knowledge for the first time in years. Learn about the future of SEO, Web Design best practices and the secrets to getting the foundation for your website to thrive. Watch the exclusive webinar.

An Exclusive Insiders Look Behind The SEO and Web Development Curtain

Rado
Author

Working in the web hosting industry for over 13 years, Rado has inevitably got some insight into the industry. A digital marketer by education, Rado is always putting himself in the client's shoes, trying to see what's best for THEM first. A man of the fine detail, you can often find him spending 10+ minutes wondering over a missing comma or slightly skewed design.