Error Establishing a Database Connection: How To Fix It In 2 Mins

Written By Peter L

On July 5, 2021
"

Read more

Stop doing all by yourself. Save time and money by hiring our 10+ years experienced virtual professionals!

A WordPress user is bound to encounter a number of errors, and these can be the fault of the user, a system break, natural calamities strike, or when someone fails at the job.  These can be frustrating when the user finds out that the website is down, especially if the user relies on it for business. The error establishing a database connection can represent a serious issue and can prevent access to your site.

Fixing this error is the top priority, but it can surely become a confusing problem if a user is not completely familiar with how WordPress works. Although this error looks serious, it is still quite fixable, and if you take a few troubleshooting steps, it is quite possible that you can get your website back online very quickly.

PageSpeedy.io is a highly preferred plugin for website developers and also the WordPress community. This advanced plugin enhances the performance of a website as it considerably reduces page load times by leveraging CDN integration. This can result in improved SEO and user experience, and because of a number of benefits, PageSpeedy is trusted by millions of publishers all over the world.

What is error establishing a database connection?

One needs to understand how the WordPress sites work before digging much into the message ‘error establishing a database connection’. It is worth knowing that all of the website information like plugin settings, post data, login credentials, and everything else is stored and organized in the MySQL database.

When a visitor enters a website, PHP is used by WordPress to query the database and then extract the correct information, and this information is then displayed as a complete page. If for some reason, WordPress is not able to access the site’s database, then it is sure that it isn’t working in a proper manner, and therefore, it can result in WordPress error establishing a database connection message.

Because of this error, the entire page is prevented from loading, and therefore, you are not even able to access the WordPress dashboard that is the backend of your site. If your site happens to use caching, the visitors may see stored copies of your pages. So, you can still avoid too many interruptions to your website and business if you catch the error in the earlier stages and resolve the issues before the site’s cache refreshes.

It is quite fortunate for a user that when a WordPress error goes, then the error establishing database connection WordPress gets quite simple to resolve. The mismatch of the login credentials for the database is known to be the most common of the causes so far.

error establishing a database connection cpanel

Reasons behind error establishing a database connection

Basically, there are four main causes of the database connection error.

  • Incorrect login credentials

Among the most common causes of WordPress error establishing a database connection is the database’s login credentials. One should understand that it could be anything from the name of the user, password or even the name of the database. You also have to keep in mind that the login details are quite different from the ones that you use to access your website.

 

  • Database corruption

There is a lot of information contained by WordPress MySQL. If any part of this database is deleted or corrupted, then the result can surely be an error in question. The major reason for the corruption is the result of tinkering with the database manually, but there are also chances that it may happen as a consequence of normal use. Although MySQL is considered quite robust, errors can still occur because nothing is perfect.

 

  • WordPress core file corruption

There are also chances of corruption occurring in the core WordPress files that happen to make up your website. Even though you will find these files outside the database, they can sometimes result in the same error.

 

  • Problems with the hosting provider or the webserver

In the case of self-hosting, if you are having difficulty with your host or server and WordPress is not able to reach it to query the database, then an error establishing a database connection will appear. The problems that may arise are data loss, outages, and hardware failures.

 

Ways of fixing error establishing a database connection

Although it is considered to be a serious error, this error can still be resolved easily. You will find some of the commands to be very accommodating in order to resolve the issues. It is worth noting that each of the steps must be tried in order, and you can only move to the next if one doesn’t work.

  • Check the WordPress Database credentials

This is quite likely to be the main cause of the error and must be the first step in the troubleshooting process. The first step that you have to take is to find out the credentials that WordPress currently uses in order to access your database. This detailed information is stored in the wp-config.php file. The SFTP or Secure File Transfer Protocol client can be applied in order to access it.

Alternatively, if your site is hosted by some other provider, then the file system can be accessed from its Control Panel. In order to do so, you need to sign in to the control panel and then navigate. Move the mouse over the domain that you want to fix, and to see the folder icon, click on ‘manage.’ Then you have to click on the file folder symbol next to the site experiencing errors.

For WordPress, you have to select the folder that is named after your website inside the file browser and then open it up. Three different types of information can be found there, which includes the username, the password and the name of the database. All of these must be near the top of the file.

Once you are able to confirm the database name, username, and password, you have the option to change the information in your wp-config.php file if needed. You can visit the website again after doing this and see if the database error is gone or not. If the error still exists, then this means that there could be something else wrong.

 

  • Check the database host information

If you have already checked the database login credentials and have been able to fix any errors, but there is still a message about an error establishing a database connection WordPress, then containing the database host information is the next thing that you should do. You can find the hostname of your database in wp-config.php, alongside the name of the database and login credentials.

You need to note the information and head back to your host panel by navigating to More > MySQL databases. This time, you get the chance to make sure that the hostname in the wp-config.php file is listed on the page. You may be required to add it if it can’t be initiated anywhere.

Click the Add New Hostname button in order to add a hostname, and on the next page, you have to enter the hostname that you like to use, and from the dropdown, select the right website domain. You can then create the new hostname, and it is worth noting that it may take a few hours for your new hostname to propagate through DNS.

After taking a break, when you see that some time has passed, you can then come back and check if your website is working in a proper way. If there is still an error establishing a database connection, then you have to proceed to the next step.

how to solve error establishing a database connection

  • Repair the WordPress Database

After doing all the hard work, if you are still receiving the same note, then the probable file exploitation can be fixed by mending the database. One can find two diverse ways for restoring the WordPress database. The first way is to select an error establishing a database connection cPanel, and select phpMyAdmin in cPanel. Then navigate the database, select the tables and then repair the table in the dropdown menu.

The second and most recommended one is to use a built-in WordPress database repair tool. The wp-config.php file needs to be accessed to get to this tool. In the next step, open up the new browser tab and replace your site name with the actual domain of the site.

In order to let it work by itself, you need to click on the Repair Database button. If you decide to go for the Optimize database choice, this may take quite a bit of your time. When the tool finishes its work, you can load the website again and then check for the error. If the error is not there, then you will know that the main cause was a corrupted database.

You have to head to another step if you are still finding an error message. But before you do that, you have to make sure that you head back into wp-config.php and delete the code that has been added in order to turn on the tool. If it is left there, then there are chances that someone else could be accessing the site easily.

 

  • Get information regarding the database server

If all of the above steps are having failed, then there is one final possibility that the database server might be down. At this particular point, it can be a very good idea to verify with your web host provider in order to find out that everything is working in a correct manner.

The best idea is to reach out to your web host and inquire about any of the outages or other issues. You can report the problem if they are not showing any issues and then ask the support staff to look into the matter.

There are a few reasons why your server or the database host might be having issues.

  • Too many connections to the database

You have to keep in mind that some of the providers have a limit on how many connections a server might have at one time.

 

  • Issues with another site on the shared hosting server

If your hosting plan is a shared one, then there are chances that you may be splitting resources with other sites. The problem can even grasp your website, if one of the sites is having any issues. This is why a dedicated or a managed WordPress hosting plan can be very useful.

 

  • Hardware issues

Sometimes hardware fails, and there is a possibility that the server on which your server is stored might have done this. Normally, the host has redundancies in place so that if a server happens to go down, then the copy of your data is available, but this may not always be the case, especially if you are hosting a database on a home or the office server.

 

Instructions and resolutions that worked for others

If all of the tips have been tried and you are not sure about how to fix error establishing a database connection, then you have a few more options that have worked well for some users. These particular options can be tried by you before or after going through the above-mentioned troubleshooting steps.

error establishing a database connection

 

  • Update the WordPress Site URL

If, for any reason, you have recently moved to the WordPress installation, like migrating to HTTPS or moving to a new domain, then you may be required to update your site URL in the database. There can be numerous sources from where you can find the full instructions.

 

  • Reboot the Web Server

If you still can’t find a way of how to solve error establishing a database connection, then rebooting your web server can be the simplest and the most effective of the solutions. The database server can be turned off and on if it is hosted on your server. It is worth noting that computers are complex machines; therefore, a simple reboot can fix any odd problems that you may be experiencing.

 

  • Ask for Assistance

If you are still not that comfortable with doing troubleshooting, or you have been trying and are not sure about what is error establishing a database connection, then you should never be ashamed of reaching out for expert help.

You can get in touch with your host’s customer care 24/7 by clicking the support button in their panel. There are also a number of help forums that you can search platforms, and these forums can offer you a lot of guidance. You may have your questions answered on these sites or forums, but if not, you can always ask for help.

how to fix error establishing a database connection

Conclusion

Several ways are available out there regarding how to solve error establishing a database connection in WordPress. The most common of these are the invalid credentials that can easily be seen. If you are able to show that the credentials are correct, then this can be the right beginning. Your website experiencing downtime may be the last thing that you want. Therefore, a few proper steps can be very helpful for your site to get back up and running. It is very important to understand that you always have the option of running your website from a backup if required.

Stop doing all by yourself. Save time and money by hiring our 10+ years experienced virtual professionals!

Guides

  • WooCommerce not sending emails
  • WordPress Speed Optimization
  • Fixing Site Issues