How To Fix Error Establishing a Database Connection in WordPress

Are you getting the dreaded “error establishing a database connection” message?

If you are familiar with WordPress, you must have seen this message at least once.

It is one of the biggest headache of a webmaster and God forbid your website visitor sees this message, you will lose that visitor and the revenue you could have earned.

How could we solve this problem? is it a simple fix? maybe a change of host might fix the error.

Let us rid your website of this error for good.

What Does “Error Establishing a Database Connection” Mean?

WordPress is written using PHP and MySQL.

Website information is stored in a MySQL database, and PHP is used to store and retrieve that information from the database.

When you get the “Error establishing a database connection”, it means that for something is preventing the PHP code from connecting with your MySQL database, and that leads to “Error establishing a database connection”.

Possible Causes of “error establishing a database connection” error

There are a couple of reasons is having issues connecting to your database, here are the most common reasons for an error establishing a database connection message.

1. Wrong Database login details

If the username or password defined in your wp-config file is not correct, there will be no connection to the MySQL database.

Solution

– Access the root folder of your WordPress installation

– Download WP-config file

– Open file with text editor

– This is what you need to check

// ** 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' );
  1. (“DB_NAME”) – Database Name
  2. (“DB_USER”) – Database Username
  3. (“DB_PASSWORD”) – Database password
  4. (“DB_HOST”) – Database host

The correct credentials should be entered inside the inverted commas e.g replace – ‘database_name_here’ with “wpseerdb”

The correct details is under your host panel. Hosting companies use different panels. The best way to get the correct details is to ask your host or check their support documentation for details on how to get your correct details.

– Save changes if any, and

– upload to the root folder of your WordPress installation

– Replace existing file if prompted.

2. Corrupted Database

Another potential cause of an “error establishing database connection” message or any other similar database error you might encounter is a corrupted database.

Solution

– Open the root folder of your WordPress installation

– Download your wp-config.php file

– Open the wp-config file

– Add this line of code to the bottom

define( 'WP_ALLOW_REPAIR', true );

– save changes

– upload the file

– type in “www.yourwebsite.com/wp-admin/maint/repair.php” in your browser (replace “yourwebsite.com” with your URL).

– This lead to the prompt below:

error establishing a database connection

– Click on either, repair database or repair and optimize database (takes longer).

– once the repair is done

– remove the line of code you added to wp-config.

– As long as the code is there, anyone can access the page you just accessed.

3. Hosting

This is always the first place I look when I have issues relating to database or any performance issue, 60% of the time it is caused by your host.

Solution

If you are on a shared hosting plan, the most like cause is either your host, if the problem persists after you exhausted the other options, check you host.

You are exceeding your hosting plan

If you are exceeding your memory allocation, or the resources allocated to your website, you will definitely get database errors.

– Upgrade you plan

– If the problem persists, change hosts.

– consider hosts focused on WordPress.

Reduce plugin abuse

If you are on a shared hosting plan, you have to learn how to use your resources efficiently.

This is a very useful guide on plugin abuse: Popular WordPress Plugins To Avoid on Shared Hosting

– Use plugins only when required; if you can do without a plugin, do not use a plugin.

– Disable the plugins that serve as tools when you are not using them; Broken link checker, Image optimizer’s

– Stay away from themes that try to do too much; functions are best served by plugins.

– Stay away from plugins or themes that are not regularly updated.

– Use plugins from respected theme authors, they have cleaner code.

The obvious solution is to upgrade your plan, but I will be looking for another host if this error persists.

Caching

Caching plugins have become better, take advantage of them, WordPress Cache Enabler is downright the leading choice, Wp Super Cache is also excellent.

CDN

Are you serving large media files? take some load off your server by getting a Content delivery network to serve those files to your visitor.

Here is an excellent guide to help you make the right choice – Best CDN For WordPress: Which one is right for you?

Get More Stuff Like This

Marketing, WordPress, Blogging Tips, SEO and Reviews, once a week.

Thank you for subscribing.

Something went wrong.

4 thoughts on “How To Fix Error Establishing a Database Connection in WordPress”

  1. This is really a very awesome and comprehensive tutorial man,
    Indeed, getting that error establishing a database connection thing can really be very annoying especially when you do not have any idea what it means and what you can do to get it solved.

    However, I’m sure that a lot of people will start mastering it after reading this post.

    Thanks for sharing.

Leave a Comment