14

Error establishing a database connection (WordPress Error FIXED)

This WordPress error establishing a database connection is normal so please do not panic. I want to guide you step by step to fixing it.

I’m going to propose a solution irrespective of your Web Hosting provider. I have seen many guides how to fix this database connection error. But most of these are specific to different hosting solutions.

We will look at salvaging this in 3 different ways:

  1. Checking to make sure we are properly connecting to the database
  2. Using the WordPress Database repair tool
  3. Contacting your host

Again don’t panic. These are very friendly tools and methods and I will make sure even my grand ma can follow these instructions and fix the problem.

What causes the database connection error in WordPress?

If you are just creating a new WordPress blog, chances are that you are having issues with your database login parameters or your database server is not started.

You have to make sure you cross-check the following database connection elements:

  1. Your database host url (Provided by your host, localhost in most cases)
  2. Also check the database name
  3. Make sure the database username is correct
  4. Verify the database password
  5. Check that the database server is up and running.

The first four elements are stored in a file called  wp-config.php. This is what it looks like:

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

If a new site triggers this “error establishing a database connection” and you’ve checked and confirmed your parameters are correct, I’ll suggest you contact your web host.

If you are running on a local server, restart it and that should resolve the matter.

NB: For local servers running on your PC, be sure the MySQL database port 3306 is not being used by another app.

A sudden “error establishing a database connection” message.

If your site was online with no database connection issue, and suddenly you are getting this message, something just went wrong that can be fixed.

Maybe your MySQL database server is down. That’s an issue you cannot directly fix if you are hosting on a remote server. Quickly get in touch with your host for a solution.

If you are hosting with a reliable web hosting provider like WPX Hosting, the issue should be fixed in a couple of minute. All you need is to contact them through their live support tool.

What causes database server to stop functioning?

So many different factors:

  • Poorly optimized server
  • Low performing hardware
  • DDoS attack on your server
  • Sudden rise or spike in your traffic

Moving your WordPress site from one host to another.

error establishing a database connection in wordpress installation

Moving from one web host to the other is sometimes very challenging. Often, we face different annoying technical errors including the error establishing a database connection issue.

If this is your case, make sure you use the parameters from the new server more correctly. Generally, there will be a change in the database host URL as this doesn’t depend on you. You may want to maintain your Db name, username and password.

Some folks have reported this error using Godaddy. Others have indicated they face the database connection error on AWS (Amazon Web Services). The best walk through is to contact your service provider.

Error establishing a database connection in WordPress installation.

This error is as well common during WordPress installation. If you are running local XAMPP, WAMP or MAMP server, chances are you’ve missed something in the connection parameters.

Check to confirm your MySQL server is running. If it’s not online, make sure the local MySQL database port is available. Also double-check you are using the correct parameters: database name, username, password and of course localhost for the mos part.

Trying Repairing your database may solve the error establishing a database connection problem.

We are going to run a script that will repair your MySQL database and hopefully, this should resolve the issue. WordPress comes with a database repair tool that allows you to repair the corrupted database without or with very little technical knowledge.

But to run this, we need to enable it. By default, for security reasons, this tool is disabled. I’m going to show you exactly how to enable the tool without sweating.

a) Use FTP to download the config file

First, let’s enable the tool using an FTP software. If you are not comfortable with using FTP applications, step to the next method (b) below.

To be able to use this FTP (File Transfer Protocol) method, you need a couple of things:

  • A free FTP client
  • Your web’s FTP URL
  • FTP password
  • And FTP Username

These elements should be made available by your web hosting provider. You may want to use FileZilla or any other FTP client you are comfortable with.

Start the application and enter your Host, Username and Password. Generally, the Port should be left as it is except specific instructions given by your host.

Connect to your WordPress root folder and download the wp-config.php file.

repaire database connection errora

Open the file in any text editor.

Copy the following piece of code and paste somewhere in the file.

define('WP_ALLOW_REPAIR', true);

You may want to scroll toward the bottom of the file and add it just before/after That’s all, stop editing! Happy blogging text

Note that this file has a couple of lines that maybe confusing. Make sure you do not displace anything as this may completely break your blog.

For more safety, I recommend you safe or backup a copy of the file before doing the modifications:

Error establishing a database connection

After the changes, safe the file and upload to the server, replacing the copy that’s there. Note that without this line added to this file, this tool won’t work.

b) Using online file manager with cPanel

Now, this procedure is unique for cPanel control panel. If you are using a Web hosting Control panel that’s not cPanel, You have to locate its online file manager. Browse to the root of your WordPress installation and locate the same file.

For cPanel users:

Just click the File Manager link somewhere on your cPanel dashboard.

This will take you to the online file manager. Browse to the root of your blog. If your blog is installed at the root of your domain name, it should be something like public_html/ or www/ 

If you have the blog installed in a sub-folder, brow and locate it.

online file manager

I prefer using the ‘Code Editor’ as this opens the file is a php friendly text editor with colored and well formatted source code.

Here is what it looks like:

Click the Save Changes button

Access the tool and run the repair process from your web browser.

Now, we have our tool ready to run. The next thing is to open it in a web browser.

Go to http://yourdomain.com/wp-admin/maint/repair.php

NB: Replace yourdomain.com with your real domain name.

If your blog is installed somewhere in a sub folder, be sure to indicate the path to your the root folder:

http://yourdomain.com/path-to-blog/wp-admin/maint/repair.php

 

Here is what you get next:

error establishing database connection

There are two options here:

  1. Simply repair the Database
  2. Repair and Optimize.

I’ll recommend you click the second option. That correct any database errors and optimize the table for improved performance.

Once repair is complete, remove the line you added above from wp-config.php to prevent the tool from being used by unauthorized users. Reload your website.

The Error establishing a database connection continues.

Phew!

We are going to try another fix.

First, make sure you backup your database. If you are not comfortable with this method, please contact your host. But if you are confident, let’s run a short SQL script.

Each time a copy of WordPress is installed, it tracks the domain name and stores it in the database. That means a copy of wp that was installed on domainA.com cannot just be transfered to domainB.com. The domain name will have to be changed in the Db as well.

We want to run an SQL script that will make sure the domain name in our MySQL Db is the right one on which we want to host the blog.

Here is the SQL script to run:

UPDATE wp_options SET option_value='YOUR_BLOG_URL WHERE option_name='siteurl'

There are two points to note here:

  1. The default database table prefix wp_ may be different from yours. Check to make sure you use the correct table name.
  2. Replace YOUR_BLOG_URL with your actual domain name.

How to run this script?

There are different Db editors out there. For the most part, you are going to use phpMyAdmin. Simply paste the query in the query form and execute:

use MySQL to correct error establishing database connection

If this still doesn’t solve the problem, I highly recommend contacting your host. Trying some more complex fixes  may end up landing your website in deeper shit.

On the other hand, let me know if my suggestions got you a solution. Drop a comment below.

Please share this post on social media

HOT!!!


The Secret Path To $5000/m With 1000+ banners on 1 blog! Never talked about!


Did you ever know your blog could have Thousands of money making banners at the same time?


No this is not with any banner rotating plugin.


Read this post to find out!
Enstine Muki
 

Certified Cryptocurrency Expert, Problogger and Serial Entrepreneur

  • Hi Enstine,

    I just love this tutorial. You made it so simple and easy to do. For me, that is wonderful! It is great to know because my tech guy is not always available immediately. This is something even I can do myself.
    I appreciate you putting this all together and will save as a reference.

    -Donna

  • Bhawna says:

    Hello Enstine,

    Very nice and detailed tutorial. I never had this problem but sometimes my blog would show as it is offline and after sometimes it will get back on.
    any idea on why this might be happening?
    Thanks
    Regards 🙂 🙂

  • Ashish says:

    Hey Enstine

    This error mostly occurs when you have migrated your website form a server.
    Other than that, if you have good hosting it should come up.

    I am currently using Hostgator and sometimes (1 time to be precise)
    I got the request timeout error.
    Other than that I have never found any issues.

    I was going thought Moss’s website when I found your comment and thought of checking the blog.
    you have a nice blog.

    Keep up the good work.

  • Excellent guide Enstine,

    I wish I could have read a guide like this when I was just getting started with WordPress, this type of error caused me so much trouble back then.

    Now, not only I know better how to fix this error, but I also know where to send any newbies who are struggling with this.

    Keep it up!

  • Hi Enstine,
    I’ve had this happen a few times since I’ve switched servers. I’ve been considering changing to another host but if it keeps up, I’ll try your suggestions.

    I like that you give more than one option to work with.

  • Hello Enstine,

    This is really very daunting and annoying too when we face these error on our WordPress blog. There can be a plenty of reason for this error. However, Majority is due to low memory allocation or conflicting plugins or corrupt database. These tips are very helpful to sort out the issue. Thanks for sharing them with us.

    Have a Great Day 🙂
    Vishwajeet

    • Enstine Muki says:

      Hey bro,
      Thanks for the engagement and for adding to the post. I hope this really helps those facing the issue 😉

  • Hi Enstine,

    Thanks Buddy for sharing this amazing tutorial. I have faced this issue several times but couldn’t figure out how to deal with it.

    Now after reading this post my doubts got clear and finally I know where was I making the mistake.

    I would like to try this method and I hope this time it will work.

    Thanks,
    Umesh

  • Hi Enstine, thanks for sharing about this. I have had it happen throughout the years and it is one less thing I have to bug my techie for now 🙂
    It can be very frustrating especially when you are new to WordPress. It can take hours or days for some to fix. Stuff happens!
    Just yesterday I had an issue with a plugin and I needed help. Luckily I had it quickly and it was resolved in minutes.
    Have a great day ahead Enstine and I really appreciate this guide.

    • Hey Lisa,
      Yes this error is frightening but truth is it’s always very quick to fix for tech guys except a huge database problem with the host. Though I have provided this guide, it’s always recommended to turn to your host or tech person should this kind of message comes up. Sometimes, trying to fix it may run us into deeper waters 😉

      Good you had that plugin issue sorted out. The first thing to do whenever there is an issue with a plugin is to disable it. Then investigate the issue and try a fix.

      Thanks again for being here today Lisa. hope you are having a wonderful weekend

  • Well done Enstine 😉 Love the breakdown here because you offer step by step solutions to a common problem. I know not a blogger who has avoided this error. I always go to my developer with the issue but it seems to be a hosting problem most of the time. After the server situation gets addressed, I am up and running. Thanks for sharing 🙂

    Ryan

    • Hey man,
      Thanks for stopping by. Many of us (blogging on WP) have at one point hit this error. Thank God the fix is always quick though.
      Yes for the most part, the host is at the origin of the problem except the user has errors in db connection params.
      I’m glad your tech guy is always there. I do all the stuffs myself – being a webmaster and php coder for years now. I can always figure out all these errors.

  • GEORGE AKWOWE says:

    Enstine, good morning. Thanks very much for this well researched post on fixing this usually annoying ERROR issue on WordPress.

    I presently have no blog but I know what I am losing. I am girding up my loins to start one. I am therefore thankful to you this info came at right time. May I add that you have saved lots of people the stress and dollars they would have paid people to fix their sites.

    Thanks again and have a blessed weekend.

    • Hey George,
      How are you and good day bro.
      What happened to your blog. I thought you had one or am I just being confused.
      Thanks for the kind words

  • >