Jump to content

343: Wordpress - "Error Establishing a Database Connection"


Guest
 Share

Recommended Posts

What causes this error?

This error can occur due to a few different causes, each one is described along with recommended solution.

Incorrect connection parameters
By far the most common, having incorrect connection credentials will cause the error. These credentials are located in the wp-config.php file found in the root folder for your WordPress installation. There are three credentials: database name, database user, and password. Make sure these match the information located in your cPanel. If you know the current database user password, you can reset it. If you do not know it, you will not be able to view the current password, so you will need to delete and then recreate the database user, giving it the password found in the wp-config.php file. If this is the issue, then the problem should be solved and the site should display normally


Corrupted database tables
If you have confirmed the credentials are correct and still get the error, the issue could be caused by corrupted database tables. Database tables can be checked and repaired from within the cPanel.

How to check your tables

  1. Log into your cPanel
  2. Click the phpMyAdmin icon
  3. Choose the database you are working with by clicking on it in the left menu
  4. On the right side of the page, you will see a listing of your tables. Click "Check All" and then from the drop down choose "Check Table"
  5. The page will refresh and give you a summary of table that may be corrupted. As you can see from the screenshot below, all the tables are OK. If you receive any errors, you can continue reading to find how to repair your tables after checking tables in phpMyAdmin

How to repair your tables

  1. Log into your cPanel
  2. Click the phpMyAdmin icon
  3. Choose the database you are working with by clicking on it in the left menu
  4. On the right side of the page, you will see a listing of your tables. Click "Check All" and then from the drop down choose "Repair Table"
  5. The page will refresh and give you a summary of the tables that were repaired.

Too many concurrent database connections
The error can also occur if there are too many connections to your database at once. Currently, the default limit for our servers is 30 connections at once. If this is your issue, you can certainly curb it by enabling caching plugins on your website. This will limit the overall number of database interactions your Wordpress site performs.

Optimizing WordPress with W3 Total Cache plugin

By default WordPress is a dynamic CMS (Content Management System). This means that for every visitor request that WordPress has to process, it must first connect to the database to see if the requested page even exists.
 
In a lot of cases this might not be problematic on a site that doesn't receive much traffic. However a sudden surge in traffic caused by search engine bots, or just a general increase in normal traffic can quickly cause your WordPress site to use up a lot of CPU resources from the server while trying to fulfill needless duplicate requests again and again.
 
You can counter this increase in CPU usage by implementing a caching plugin. What these do is cache the first visitor's request of a new page to a plain HTML file on the server, then when another visitor comes through and requests the same page, so long as the page wasn't updated in your administration section, or updated by a comment, the cached HTML page will be served.
 
This can greatly reduce CPU usage of your WordPress site very easily. As an example let's say you had 100 views of your front page, without caching that would require the same database query to have to run 100 times and every time it's just getting back the exact same data anyways.

With a caching plugin only the first user would have the database query run to generate the cached HTML file, then the next 99 visitors would get that cached HTML served to them right away, without having to wait for any database activity to complete.
 
This is just about always a win-win, because your visitors don't have to wait as long for your pages to load, and you're reducing the impact of WordPress's requests on the server's performance.
 
One of the simplest WordPress caching plugins to setup is WP Super Cache which is discussed in a previous article of ours.

However if you need more advanced caching options such as the ability to serve a static 404 error page the steps below will walk you through installing and configuring the W3 Total Cache plugin for WordPress.

**Note: This will not work with Wordpress Multisites.

  1. Hover over Plugins in the left-hand menu, then click Add New.
  2. In the Search box, type in w3 total cache and click on Search Plugins.
  3. Under W3 Total Cache click on Install Now.
  4. Click OK in the installation pop-up.
  5. Click Activate Plugin.
  6. You should now see Plugin activated.
  7. From the left-hand menu, you should now have a new Performance section, hover over this and click on General Settings.
  8. Scroll down the general settings page ensuring that each main section is enabled.
  9. Hover over Performance again in the left-hand menu, and click on Page Cache.
  10. Ensure that these options have a checkmark beside them, then click on Save all settings:
  11. Finally to confirm that you've setup everything correctly, in your web-browser open up your site and hit (Ctrl-U), or go to View -> Page source to view the source of the page. Scroll to the very bottom of the page and you should see the W3 Total Cache banner letting you know the page has been optimized.
        w3tc-confirm-page-source
    Once you've confirmed that W3 Total Cache is up and running properly on your website you're done. Now you can enjoy quicker page loads, and reduced resource usage from your WordPress site!

Below #8

    Page Cache

    Minify

    NOTE: If you are using minify, make sure to enable the option, save it and then immediately look at your WordPress site to make sure that the site looks normal. If you see problems with formatting, then it's possible that a theme or plugin is causing issues due to the minification. You should then disable minification and not use this option with your website.

    Database Cache

    Object Cache

    Browser Cache, after enabling this option click on Save all settings
   
 
Below #10   

    Cache home page

    Cache feeds: site, categories, tags, comments

    Cache 404 (not found) pages

    Cache requests only for yourdomain.com hostname

    Don't cache pages for logged in users
   
    
    
Optimizing WordPress with WP Super Cache Plugin
WP Super Cache can help optimize your WordPress website. WP Super Cache will enable your website to load faster and use less server resources. You can install WP Super Cache by first logging into your WordPress Dashboard.

  1. Once you have logged into your WordPress Dashboard, click on Plugins on the left menu pane. Then click Add New.
  2. Use the search box, and search for "super cache".
  3. After the search is finished, you can locate the plugin and click "Install Now"
  4. Once the plugin installation is complete, click on "activate Plugin"
  5. You will see a "red box" to remind you to enable WP Super Cache. click on the "Plugin Admin Page".
  6. Enable caching by clicking on the button next to "Caching On" and then click "Update Status"
  7. On the same screen as above, click on the "advanced" tab.
  8. Please select the "Use mod_rewrite to serve cache files" option and click "update status"
  9. Next you should receive this message below.
  10. Scroll down the page and you will will a large yellow box. Click on the button labeled "Update Mod_Rewrite Rules" at the bottom of the box.
  11. Once you click the box in the above screenshot it will change to green and be labeled "Mod rewrite rules updated".

You have two different options to verify that WP Super Cache is installed correctly. The first way is to look at the source code of your website. The source code will show that the webpage was successfully cached. In most web browsers you can right click within the WordPress website and select "View Page Source". Look at the bottom of the page

The second way to verify if super cache is working correctly and installed is checking the ,htaccess file. You can access the .htaccess file using a FTP client or through file manager in cPanel. In the same folder that contains your WordPress site, look for the .htaccess file and open it. In the .htaccess file

Once you have confirmed that WP Super Cache is installed and functioning correctly, you're done!

Additionally, there can be other causes for extraneous database interaction. These can be prevented by logging out of WordPress admin dashboard when not in use, disabling the WordPress autosave, and limiting or disabling WordPress revisions.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...