TRY OUR FULLY SEO-OPTIMIZED WORDPRESS THEME FOR AFFILIATE MARKETERS!

No need to hire SEO experts anymore to fix your site technical SEO issues

IMPROVE YOUR SITES RANKING TODAY

How to Fix the Error Establishing a Database Connection in WordPress

by Jay | Updated on January 3rd, 2023

Are you trying to log into your WordPress site and getting an error message? Are you frustrated because you can’t access your website? If so, then don’t worry. This blog post will help walk you through how to fix the error in establishing a database connection in WordPress.

What Is the Error Establishing a Database Connection?

The “Error Establishing a Database Connection” message is one of the most common errors you may encounter while attempting to access a WordPress site.


errors establishing a database connection

It means that the website cannot connect to the database, where all of your site’s content, settings, and other information are stored. Without a connection to the database, your WordPress site cannot function properly.

This error can be caused by incorrect database information in your WordPress configuration file, which is called wp-config.php. It can also be caused by an overloaded server or a server that is down for maintenance. In some cases, it can even be caused by corrupt WordPress files.

Whatever the cause, fixing the error is relatively simple if you have the right knowledge. This blog post will explore how to troubleshoot and fix the “Error Establishing a Database Connection” error in WordPress.

What causes errors establishing a database connection in WordPress?

Incorrect database information in your WordPress settings, a faulty database, or an unresponsive database server can contribute to the “Error establishing a database connection” problem.

A database is a piece of software that makes it simple to store, arrange, and access data because it is a content management system. When someone visits your website, it then establishes a connection with the database. 

WordPress requires the following details to connect to the database:

  • Data source name
  • Database Username
  • Database password
  • Database server

Your WordPress installation’s wp-config.php configuration file contains this data. 

WordPress would be unable to connect to your database server and display the error message “Error establishing a database connection” if any of these details were off. 

This is one of the most frequent WordPress mistakes. This error can also occur if the database server is offline or the database files are faulty, in addition to invalid login credentials. 

Let’s look at the step-by-step troubleshooting for fixing the WordPress error establishing database connection issue.  

1. Check your WordPress database credentials

One of the most common causes of the Error Establishing a Database Connection is simply that WordPress has incorrect login credentials for your database.

To make sure this isn’t causing your issue, double check the database username, password and hostname in your wp-config.php file. This file is located in the WordPress root directory and can be accessed via an FTP client or from the File Manager in your hosting control panel.

If you’ve changed any of the details in your wp-config.php file, make sure they’re correct. If they are correct, you can try generating a new set of credentials and replacing the old ones with the new ones.

The following lines in the wp-config.php file are what you need to check for. 

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

You need to confirm that the database host, username, password, and database information are all accurate.

From the dashboard of your WordPress hosting account, you can verify this information. To access MySQL databases, simply log into your hosting account and select the database option. 

Once your database name, username, and password have been verified, you can modify them if necessary in your wp-config.php file. Visit your website once again after that to determine if the database connection error has been resolved.

If the mistake is still visible, then something else must be off. For additional troubleshooting procedures, keep reading. 

2. Increase Your PHP Memory Limit

When you encounter an error establishing a database connection, it could be due to an issue with your PHP memory limit. The PHP memory limit determines how much memory is allocated to your WordPress site and can cause issues if it’s too low.

To increase the PHP memory limit, you will need to access your cPanel and edit the wp-config.php file. In the wp-config.php file, look for the line of code that reads:

define(‘WP_MEMORY_LIMIT’, ‘256M’);

Then, increase the memory limit to a higher number, such as 512M or 1024M. After you’ve saved the changes, try accessing your website again to see if the error has been fixed.

3. Check your database host information

Ensure you are using the correct database host information if you’re positive that your database name, username, and password information is accurate. 

The majority of WordPress hosting providers host your database on localhost. However, some managed WordPress hosting providers maintain databases on separate servers. Localhost won’t be listed as your database host information in that situation. Get in touch with your WordPress hosting provider to confirm your database host details. 

4. Repair WordPress Database

If you’ve checked your database credentials and connection but still find yourself facing the error establishing a database connection, it’s time to repair your WordPress database tables. Fortunately, WordPress makes this process quite easy. All you need to do is enter a special URL in your browser and WordPress will take care of the rest.

You should add the following line to your wp-config.php file to do this. Take care to insert it immediately preceding:

define('WP_ALLOW_REPAIR', true);

When you’ve finished, go to this page to see the settings:

You need to enter your WordPress website URL with “/wp-admin/maint/repair.php” added to the end. For example, if your website URL was “example.com”, you would enter “example.com/wp-admin/maint/repair.php”.

Once you enter this URL, WordPress will take you to a page that allows you to repair your database tables. Simply click the “Repair Database” button and WordPress will take care of the rest. Once the process is finished, you should be able to access your website once again.

http://www.yourwebsite.com/wp-admin/maint/repair.php

It should be noted that anyone can view the database repair page without having to log in. Make careful to remove this code from your wp-config.php after you’ve finished fixing and optimizing your database. 

5. Check if your database server is down

If everything appears to be in order, but WordPress is still unable to connect to the database, your MySQL server may be offline. 

A server experiencing high traffic may be the cause of this. In essence, your host server cannot handle the traffic (especially when you are on shared hosting).

Your website will become extremely sluggish, and it can even produce an error for some users. Therefore, the best course of action is to contact your hosting company via phone or live chat and inquire as to whether your MySQL server is responsive. 

You can check the other websites that are hosted on the same server to see if your SQL server is down. 

Go to your hosting panel, try to access phpMyAdmin, and connect the database if you don’t have any other websites hosted on the same account. 

If you are able to connect, we must check the permissions of your database user. 

Paste the following code into a brand-new file called testconnection.php:

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

Be sure to change the password and username. This file is now available for web browser viewing and upload to your website. 

If the script was able to connect, that suggests your user has the necessary permissions and something else is wrong. 

Make sure everything in your wp-config file is right by returning to it (re-scan for typos). 

Other solutions that have worked for users

You can attempt these additional actions if the troubleshooting advice provided above did not resolve the database connection error on your website. 

According to what our users have reported, these techniques have assisted some users in fixing the database connection error on their websites. 

Update WordPress site URL

Try using phpMyAdmin to modify the WordPress site’s URL. Simply choose your WordPress database in phpMyAdmin by going to the dashboard of your hosting account. 

Next, select SQL from the top menu and type the following MySQL query.

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

In case you modified the WordPress table prefix, don’t forget to supply your own site URL and replace wp options with the name of your own table. 

Re-install WordPress Files

Re-installing WordPress files is a last resort if none of the above solutions have worked. It requires you to delete all of your WordPress files from your website, which can be a daunting task.

However, it’s a relatively simple process. First, download a copy of the WordPress files from the official WordPress website. Once you’ve done that, you’ll need to connect to your website via FTP or File Manager in cPanel.

Then delete all of the existing WordPress files on your website, and upload a fresh copy of the WordPress files. This should help to resolve the “Error Establishing a Database Connection” issue.

Remember to make sure you back up your website before you start the re-installation process. If you don’t have a backup, you may lose important data.

Rebooting web server

Users of virtual private servers (VPS), local servers, and dedicated servers might attempt resetting their machines. 

This will restart your web server and database server, which may resolve any short-term issues that were the cause of the error. 

Ask for help

You might need to get in touch with your web hosting firm if all else fails. Any reputable WordPress hosting provider will assist you in identifying the issue, send you on the correct path, or even resolve it for free. 

Conclusion

If you’ve followed the steps above, you should have successfully fixed the error establishing a database connection in WordPress. It’s important to always make sure that your database connection information is correct and up to date.

Additionally, it’s important to always keep your WordPress files, and database tables backed up in the event that something goes wrong. By following the steps above, you can easily fix any database connection issues in WordPress.

Jay

I've worked for WooRank, SEOptimer, and working on a cool SEO audit tool called SiteGuru.co. Now I have build Linkilo and SEO RANK SERP WordPress theme. I've been in the SEO industry for more than 5 years, learning from the ground up. I've worked on many startups, but also have my own affiliate sites.

TRY OUR FULLY SEO-OPTIMIZED WORDPRESS THEME FOR AFFILIATE MARKETERS!

No need to hire SEO experts anymore to fix your site technical SEO issues

IMPROVE YOUR SITES RANKING TODAY