How to fix ‘error establishing database connection’ in WordPress

How to fix ‘error establishing database connection’ in WordPress

‘error establishing database connection’ means your WordPress site is having trouble connecting to its MYSQL database. There are several possible solutions to this including,

  • correcting database connection details in WordPress config,
  • increasing the memory size on server,
  • troubleshooting any slow MYSQL queries,
  • potentially change the page functionality and
  • potentially reduce the data being retrieved on page load.

Table of Contents

What does ‘error establishing database connection’ error mean?

The ‘error establishing database connection’ means that your WordPress site was attempting to access its database, where it holds all of your posts and other raw data, but it was unable to for some reason. WordPress needs to access this in almost every page load and form submission as a normal part of its working.

If you are seeing it your users are also seeing it until such time as there is a fix.

How to fix ‘error establishing database connection’ error?

There are a few things you would want to check in order to trouble shoot this one.

Option 1: Check the database connection details:

Double check you have the right details in https://yoursite.com/wp-config.php. If you are setting up your WordPress site from scratch or if you have been editing the wp-config.php file then just double check you have the correct details in here for,

  • DB_NAME
  • DB_PASSWORD
  • DB_USER

These are the connection details that the WordPress site itself needs to access the database and if they are incorrect then it will not be able to do so. If that doesn’t fix it then read on.

Option 2: Increase the Memory Size on the server:

The below solutions (option 3 and so on) are more thorough but if you just want to get the page working without redesigning the site then you can go to your server settings and increase the allowed ‘Memory Size‘ for any page on the site.

PROTIP: If you are on shared or free hosting you may not be able to access the cPanel. In that case you could try to execute the below solutions or migrate your site to a dedicated server so you have full control over your sites specific server settings. Bluehost has some of the best costs and customer service around. I use them myself.

NOTE: You will have to migrate your site to their server from your current one which will take a few hours but is a better long term fix.

To do this,

  • Go to your server account (for eg, BlueHost or Siteground or GoDaddy)
  • Find the cPanel area. This will be different on each server companies dashboard but with BlueHost, for eg, you can go to the ‘advanced’ tab on the left hand side of your dashboard and,
  • then find the ‘Multi PHP INI Editor‘ Icon.
Inside the ‘Advanced’ Tab on Bluehost server account you’ll find the cPanel. Look for the ‘MultiPHP INI Editor’ icon and click on it to edit the memory settings.
  • Click into it, select the website you specifically want to work with as there can be multiple on your server.
  • You will want to increase the ‘Memory Limit’ variable which is defined asThis sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to -1.
  • I’d suggest doubling whatever limit you find there but you could triple or quadruple if you really want also!
  • Make sure to save it and then go back to your site and reload the page. It will likely still take a while to load but thats normal in this scenario.
  • If you continue to get the error then keep raising it until it works.

What if you are on shared hosting?

If you don’t have your own cPanel access as may be the case if you have shared hosting with other websites also using the same server then you may have to instead ask your server customer support to up this limit for you.

If they are not able to raise it high enough then you can buy your own hosting plan for your site only which will give you full control of its specific settings and access to the cPanel. You could buy a hosting plan from BlueHost easily and cheaply and get set up right away. You would need to migrate your site to BlueHost itself and this is a task which might take a few hours but will be a more long term fix for your site.

Option 3: Investigate what MYSQL queries are being made on the page:

You can also see this error when your database has the correct connection details but the query required to be run before loading the HTML is so large that the database connection times out. In my experience there are two ways to solve this.

Option 4: Change the page functionality:

If the page is creaking on page load that means that this issue is stopping the page from every loading meaning the user is 100% stranded. If you move this functionality to be user controlled, for eg the data is retrieved upon form submission instead then the page will at least load and then at least the user and the form you design can create a proper user experience.

For eg, if the page instead loads a form which then lets the user perform the search query after submitting a form with AJAX, perhaps with some fields for search criteria, then you can at least add a ‘please wait’ message to the form while the site is retrieving and displaying the data. The user in this case will understand what is going on without the page breaking completely.

Option 5: Make the data being retrieved smaller:

You could investigate the SQL which is happening on the page and make it smaller by tweaking the SQL and PHP with which the data is being retrieved. If you are developer then you can find the PHP which is controlling this page and inspect it to find efficiencies. You will also want to understand how to debug MYSQL Queries in WordPress so you can see which queries exactly are slowing the database down. I wrote on article on how to achieve this kind of analysis here.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *