How To Fix WordPress Error Establishing A Database Connection

How To Fix WordPress Error Establishing A Database Connection

How To Fix WordPress Error Establishing A Database Connection - "Error Establishing a Database Connection" is a common WordPress error.

How To Fix WordPress Error Establishing A Database Connection – “Error Establishing a Database Connection” is a common WordPress error. When the site refuses to connect to the WordPress database, this error occurs.

Also read: What Is FTP: An Introduction To FTP For Beginners

If a site is left ignored, it will become unreachable and lose traffic, inflicting damage to its reputation.

How To Fix WordPress Error Establishing A Database Connection

This article will explain what “Error Establishing a Database Connection” is, how to troubleshoot it in WordPress, and what the likely causes are. However, before we can go to the first stage in resolving this issue, we must first understand what this database error indicates.

What Is the Meaning of “Error Establishing a Database Connection”?

When WordPress is unable to connect to the database, the warning “Error Establishing a Database Connection” occurs.

WordPress was originally created in PHP and uses MySQL as its database system. When someone visits your site, precise PHP commands will retrieve information from the database and show it in the visitor’s browser.

Also read: How to Fix ERR_SSL_VERSION_OR_CIPHER_MISMATCH In 2023

The WordPress Error establishing a database connection warning will appear on a blank page because there is no data to fetch and display on the browser.

Fortunately, this error notice does not appear on the front end as long as your website’s cache is available. This database connection problem, however, prevents you from accessing the WordPress dashboard. With that in mind, it’s critical to figure out what’s causing this mistake and repair it.

Continue reading to learn about the numerous possible causes of this WordPress database connection problem.

6 Ways to Repair WordPress Database

The six most popular remedies to repair WordPress database Error Establishing a Database Connection error are listed below. Make a backup of your site before you begin dealing with the mistake. You can use it to restore your site if something goes wrong.

1. Make Sure Your Domain Is Pointing to Your Host Provider

  • Difficulty: Easy
  • Success rate: Medium
  • Estimated time to fix: 1 minute

When you change hosting providers, your files will be automatically transferred to the new host. However, if your domain does not point to the host where the database is housed, the database will fail.

Begin by checking the nameservers via your hosting control panel and WHOIS lookup tool. Users of Hostinger can do this by navigating to Accounts -> Details in the hPanel dashboard. If both nameservers match, your domain is successfully pointed.

Also read: 8 Easy Ways To Fix 403 Forbidden Error

If the results are different, contact your domain registrar and adjust the nameservers. Simply access your hosting dashboard to obtain a list of the nameservers and enter them into the control panel of your domain registrar.

Another alternative is to use the A record to point your domain name. It’s a DNS record that points your domain to the correct IP address. Remember that the A record only relates to a fixed IP address.

To use this option, locate your current IP address or A record value and replace it with the ones from your new web host. Use a local computer console or an online DNS checker to accomplish this. Hostinger users can find their IP address on the hPanel by going to Hosting -> Manage.

The A record value for the website in the preceding example is 123.4.567.89. If the answer matches your new hosting IP address, your domain name has been correctly pointed. Otherwise, you must update it through the management panel of your domain registrar.

2. Check Your Database Server

  • Difficulty: Easy
  • Success rate: Medium
  • Estimated time to fix: 1-2 minutes

When your MySQL server is down, the “Error Establishing a Database Connection” warning may show on your WordPress site. While various factors can contribute to this mistake, too many connections are frequently the culprit.

A database server can only have a specific number of clients connected at any given moment.

When it hits its maximum capacity, it may be unable to accommodate additional connections. As a result, we recommend contacting your hosting provider to have your MySQL server checked.

Also read: 11 Ways to Fix the DNS Server Not Responding Error

Whether you run multiple sites on the same server, you can check to see if your MySQL server is down on any of them. Meanwhile, if you only host one site, use PHP to connect to MySQL and verify that your WordPress database user has adequate permissions.

Go to your hosting control panel and open the public_html folder. Create a new PHP file and give it whatever name you wish. In this example, we’ll call the file test.php.

Copy and paste the code below into the file.

<?php
$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysqli_error());
}
echo ‘Connected successfully’;
mysqli_close($link);
?>
Change the database password and username to your own. Finally, upload this file to your website and access it via a web browser.
If it successfully connects, it suggests that your user has sufficient permissions and that something else is causing the “Error Establishing a Database Connection” notice to display.
Database failures are common when a website is hosted on a shared server owing to resource sharing. Caching your website with plugins like W3 Total Cache or WP Rocket is a fantastic approach to reduce server load.

3. Check the Database Login Credentials

  • Difficulty: Medium
  • Success rate: High
  • Estimated time to fix: 7-10 minutes

Incorrect login credentials are the most prevalent cause of the WordPress “Error Establishing a Database Connection” notice. Typically, this occurs following a WordPress site migration to a different hosting provider.

Also read: How To Flush DNS Command In 2023

To fix this problem, check if the database login settings match with the ones stored in your wp-config.php file. This file can be accessed using an FTP client or the WordPress File Manager in your hosting control panel.

hPanel

The following troubleshooting steps will show you how to access the wp-config.php file via Hostinger’s File Manager. To begin, log in to your hPanel dashboard and navigate to Files -> File Manager.

By clicking on it, you will be taken to a page that has a list of your site’s files. From there, navigate to your public_html directory and locate the wp-config.php file. Then, right-click on the file and choose Edit.

This will display a list of database details and configurations. Find and record the following information:

  • DB_NAME – database name
  • DB_USER – username
  • DB_PASSWORD – password
  • DB_HOST – database host

Return to your hPanel and browse to Database -> MySQL Databases. Check the List of Current MySQL Databases And Users section for your current database details.

If the login data in your wp-config.php file is different from the MySQL database settings, match them by modifying the wp-config file.

Then, change the following lines:

define(‘DB_NAME’, ‘fill_this_with_your_current_database_name’);
define(‘DB_USER’, ‘fill_this_with_your_current_username’);
define(‘DB_HOST’, ‘fill_this_with_your_current_localhost’);
After you’ve made the changes, restart WordPress to see whether the “Error Establishing a Database Connection” error on the website has been resolved. If the issue persists, the database password may still be incorrect. In this scenario, you must change your MySQL password.
Simply navigate to the List of Current MySQL Databases And Users area, select your current database, and then click Change Password.
After you’ve entered your new password, click the Update button.
If your database username and password are correct, but you still receive the “error establishing a database connection” message, change your database host information in the wp-config.php file.

Web hosting companies using WordPress typically utilize localhost as the database host. However, we recommend confirming your database host details with your web hosting company.

If you’ve just changed hosts, double-check the DB_HOST information. Localhost will not change in most circumstances, but some web hosts utilize an IP address or a different URL.

cPanel

The instructions are nearly same for cPanel users. Go to Files -> File Manager to access your wp.config.php file. Then, navigate to the public_html directory and find the file.

Once you have the information, use Databases -> phpMyAdmin to see if the value of DB_NAME matches the name of your database.

Your database name, as well as information_schema, will appear on the left side of your window. If the name matches the DB_NAME value on your wp-config.php file, then your database name is not causing the problem.

If the name doesn’t match, edit the value in wp-config.php directly using cPanel’s file manager.

If your database name is right but the error remains, the issue may be with your database username and password. Make a new PHP file in your root directory to fix the database credentials.

You can name the file whatever you want, such as new-database-file.php.

Then, duplicate the following code:

<?php
$link = mysql_connect(‘localhost’, ‘user’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Success establishing database connection’;
mysql_close($link);
?>

Replace the ‘user’ and ‘password’ values with your database credentials. Next, test the file by visiting http://your-site-domain.com/new-database-file.php in your browser.

Don’t forget to delete the file once your database is operational again.

In this instance, go to the Databases -> MySQL Databases menu and create a new database user. Enter your new database credentials and update the values on wp-config.php accordingly.

4. Fix the Corrupted Files

  • Difficulty: Medium
  • Effectivity level: High
  • Estimated time to fix: 10-12 minutes

Corrupted files, in addition to corrupted databases, might cause “Error Establishing a Database Connection.”

One of the causes of file corruption is a defective theme or plugin. Check each component separately to determine which component is generating the fault. If the error occurs after activating a new theme, it could be your theme.

To fix this, go to Appearance -> Themes in your WordPress admin and select a different theme.

If changing the theme does not resolve the “Error Establishing a Database Connection” issue, it could be the result of a corrupted plugin. To locate the problem, try removing all of your plugins and re-enabling them one by one.

Also read: 27 Best Chrome Extension For Web Developers In 2023

You can also disable all WordPress plugins by renaming the corresponding folder in your hosting control panel. Here’s how to go about it.

hPanel

Open the File Manager, navigate to the public_html directory, and select the wp content folder. Then, rename the plugins folder that is currently in use.

cPanel

On your cPanel dashboard, go to the File Manager menu. Locate the plugins folder by clicking on public_html -> wp content. Right-click the folder and select Rename.

This will turn off all of your plugins at once. Disabling all WordPress themes requires the same troubleshooting techniques. However, don’t disable both themes and plugins at the same time, as this would make determining the source of the problem much more difficult.

If the error goes away after renaming the plugins folder, it suggests that one of the plugins is causing the problem.

Rename the plugins folder back to its original name once you’ve recovered access to your site’s backend. All WordPress plugins will remain dormant in this manner, but you can activate any one from the admin area.

Unzip the package once it has finished downloading. Remove the wp-content folder and the wp-config-sample.php file from the folder.

After you’ve deleted them, launch an FTP program and copy the remaining WordPress files to your root folder. This should now safely overwrite all of your server’s damaged files.

5. Repair the Database in WordPress

  • Difficulty: Hard
  • Success rate: High
  • Estimated time to fix: 15-20 minutes

A damaged database is another cause of the “Error Establishing a Database Connection” error in WordPress. This can be caused by data failure from plugins or themes that are added or withdrawn on a frequent basis.

If your WordPress dashboard displays a notification that says “One or more database tables are unavailable. The database may need to be repaired,” you may have a corrupted database.

Fortunately, you can easily fix this problem by inserting the following function into the bottom of your wp-config.php file:

define(‘WP_ALLOW_REPAIR’, true);

hPanel

Hostinger users can simply access the File Manager, go to the public_html folder, and edit the wp-config.php file.

Enter the code and remember to save your modifications. Then, on your browser, navigate to http://www.your-site-domain.com/wp-admin/maint/repair.php.

There will be two alternatives for repairing your database; select the one that best suits your needs.

Also read: 15 Simple Solutions To Fix The Net::Err_Cert_Authority_Invalid Error

Check your WordPress site after the restoration procedure is complete to see if everything is back to normal. If so, go to your wp-config.php file and remove the function immediately. Because anyone can view the repair page without logging in, this eliminates any website abuse.

cPanel

The processes are the same for cPanel users. Go to Files -> File Manager, open the root directory, then add the function above to the wp-config.php file.

Aside from modifying the wp-config file, cPanel customers can use its built-in MySQL database repair functionality. Begin by going to Databases -> MySQL Databases.

6. Create a New Database

  • Difficulty: Hard
  • Success rate: Medium
  • Estimated time to fix: 15-20 minutes

If none of the procedures above produce any results, your database may be completely corrupted and no longer usable. In this scenario, you must utilize your hosting control panel to establish a new MySQL database and user.

Once a new MySQL database has been built, restore your website backup to avoid losing any important website data.

hPanel

Users of the hPanel can simply navigate to Databases and select MySQL Databases. There is a section on the main page for creating a new MySQL database and database user.

Enter the name of your database, your username, and your password, and then click the Create button. Remember that the database password should have at least eight characters, including capital and lowercase letters.

Also read: How to Fix Chrome’s Err_Cache_Miss Error

When you’re finished, the new database will be visible in the List of Current MySQL Databases and Users. You can then administer the database and do other operations, such as backup creation.

cPanel

Navigate to Databases -> MySQL Databases in cPanel. The Create New Database section will be at the top of the page. Fill in the name of your new database and then click Create Database.

After that, the system will add your new database to the list of Current Databases.

From the same box, you can also create a new MySQL user and assign it to your new database. Depending on their credentials, users can conduct a variety of operations, from establishing new tables to altering rows.

In the MySQL Users section, enter your new database username and password, then click Create User.

After creating a new user, scroll down to add the user to your new database.

You will see two options to verify or fix your database under the Modify Databases section. Select the Repair Database option to repair a corrupted database.

Wait for the process to complete before accessing your WordPress site to see if the “Error Establishing a Database Connection” warning remains.

Another option is to use phpMyAdmin to repair database tables. Simply navigate to Databases -> phpMyAdmin. Check all tables by clicking on the name of your database.

Then, select the Repair table option from the dropdown menu next to the Check all button.

It will start and repair any corrupted tables using the REPAIR TABLE statement.

Methods to Repair WordPress Error Establishing a Database Connection

If none of the techniques described above work, there are some further steps you may take to resolve the “Error Establishing a Database Connection” error in your WordPress website.

Update WordPress website URL

If you have just switched web hosts, you may need to alter your WordPress URLs. Navigate to phpMyAdmin from your hosting dashboard to do so. Then, go to your WordPress database and select the SQL tab.

Copy and paste the following code into the field provided:

UPDATE wp_options SET option_value = replace(option_value, ‘http://www.old-domain.com’, ‘http://www.new-domain.com’) WHERE option_name = ‘home’ OR option_name = ‘siteurl’;
UPDATE wp_posts SET guid = replace(guid, ‘http://www.old-domain.com’,’http://www.new-domain.com’);
UPDATE wp_posts SET post_content = replace(post_content, ‘http://www.old-domain.com’, ‘http://www.new-domain.com’);
UPDATE wp_postmeta SET meta_value = replace(meta_value,’http://www.old-domain.com’,’http://www.new-domain.com’);

Make careful to change the values ‘http://www.old-domain.com’ and ‘http://www.new-domain.com’ with the appropriate values for your new and old website URLs. When you’re finished, press the Go button.

Ask for help

Expert advice is always recommended. Please do not hesitate to contact your web hosting company’s customer service. They should be able to help you troubleshoot the database connection issue you’re facing.

Those without an account can ask questions in our General Inquiry channel.

Reboot the web server and restore backup

Users with dedicated servers, local servers, and VPS (Virtual Private Servers) can also restart their machines. This will restart your web and database servers, which may resolve any issues. You can accomplish this by logging in as root and utilizing SSH clients such as Bitvise or Putty.

In some circumstances, you may need to restore both your database and your WordPress files. Check to check if your web provider offers an automatic backup service, and then download the most recent website backup accessible.

The Causes of “Error Establishing a Database Connection”

Among the various causes of the WordPress Error Establishing a Database Connection in WordPress are:

  • Incorrect database login information. Keep in mind that the WordPress database requires a different login than your hosting panel. If your database username and password change, the previous login information stored in your WordPress files will become invalid.
  • Database corruption Issues like as plugin failures and incompatible themes can result in corrupted database tables or missing data. When any database information is destroyed or corrupted, an error occurs.
  • Error on the database server. Sometimes the issue stems from your web hosting provider. A sudden spike in traffic, for example, or permission issues can render your database inoperable. This is common in shared hosting environments where numerous websites share the same resources.
    Corrupted core files The core files of WordPress define how your site works and performs.
  • Corrupted files can be caused by a variety of factors, ranging from malfunctioning plugins and themes to problems when transferring via an FTP client. When core files are wrongly edited, they can get corrupted and cause database failure.

If you encounter the following window on the backend of your WordPress site, the issue is most likely due to incorrect login credentials.

If, on the other hand, a different error message displays, the cause could be a corrupted database or a traffic spike. Fortunately, resolving this database error is simple; the following section will discuss various options to resolving this issue.

Conclusion

It can be aggravating to receive the WordPress Error Establishing a Database Connection error on your WordPress website. The website will not load, putting traffic and customers at danger.

This database issue happens when the WordPress website is unable to establish a connection to the server. It might be caused by different reasons, such as incorrect login credentials in the wp-config.php file, corrupted files, or server overload.

Fortunately, there are several straightforward solutions to this problem:

  • Point your domain to the correct host – make sure your A record matches the IP address of your web host.
  • Verify your login credentials – match the login credentials in your wp-config.php file with the ones in your MySQL Database.
  • Repair database – by inserting the define(‘WP ALLOW REPAIR’, true); function into the wp-config.php file.
  • Repair the corrupted files by deactivating corrupt themes or plugins through your WordPress dashboard, or by downloading the most recent version of WordPress and re-uploading your core files.
  • Examine your MySQL server – build a new file that contains the mysqli connect function to see if your database user has sufficient rights.
  • Create a new database – Begin from scratch with a fresh MySQL database and import a backup.

We hope that this post has assisted you in resolving the WordPress Error Establishing a Database Connection problem on your website. Identify the possible reason of this mistake, follow the steps, and you won’t have to worry about it again.

ExplainWP
The WordPress Learning Hub

Related Posts