What Does “Error Establishing a Database Connection” Mean?

First, let’s take a look at how WordPress works to display your website, and it’ll become clear why this error is so devastating.

The WordPress software is written using PHP and MySQL. Why two languages? Because all the information that makes up your website is stored in a MySQL database, and WordPress uses PHP to store and retrieve information from that database.

Generally, whenever a page is loaded on your website, it’s built on-the-fly. PHP code is used to access your MySQL database and retrieve every bit of information needed to create the page. There’s a PHP query to access the post title, another to grab the author name, another to retrieve the publication date, and so on.

Back to your error message: “Error establishing a database connection.” This means that for some reason the PHP code was unable to connect with your MySQL database to retrieve the information it needs to build that page.

That’s why this error is often all you see on an otherwise blank page. If you can’t connect to your database, there’s no way to know what to display; no information about your website is available.

What Causes This Error?

While the meaning of the error message is clear, the reasons behind its occurrence often aren’t.

There are many reasons why your website could be having trouble connecting to your database, but they generally fall into three categories:

Your login credentials for your database are wrong. Your database uses separate login credentials (username and password). If those credentials have recently been changed, your website won’t be able to access the database with the old credentials.

Your database is corrupted. Any number of things could cause your WordPress database to become corrupted, from uploading a bad plugin, to lightning strikes to the server (seriously — though luckily that isn’t too common).

The server that hosts your database is down. Your database’s server may be down due to a problem or error on the part of your web hosting company, or you could be experiencing a big traffic spike that your server just couldn’t handle.

So with all these possibilities, how can you narrow down the problem and fix it?

Troubleshooting “Error Establishing a Database Connection”

1. Determine where the error is occurring

Can you see your website normally, but get the error when trying to access wp-admin? Or maybe your website is down, but you’re getting a slightly different error when you try to login: “One or more database tables are unavailable. The database may need to be repaired.”

These are signs of database corruption.

WordPress has a built-in way to attempt to repair your database, but you’ll have to enable this feature first. To do so, you’ll need to access your wp-config.php file, which contains your WordPress installation settings and configuration.

You can find wp-config in the your root WordPress file directory. You can access it by logging into your cPanel, selecting File Manager, and navigating to the folder where you installed WordPress.

Once you have wp-config open, add this line of code to the bottom:

define( 'WP_ALLOW_REPAIR', true );

This line will enable you to optimize and repair your database by navigating to www.yourwebsite.com/wp-admin/maint/repair.php (just replace “yourwebsite.com” with your actual URL).

You should see the above screen with two options to repair, or repair and optimize, your database. Feel free to choose either; just note that optimizing will take longer.

Please note that this database repair page is not secure; anyone can access that URL without having to login. Once you’re done repairing your database, be sure to remove the line of code you added to wp-config. This will disable access to the repair page and prevent anyone else from messing with your database.

Keep wp-config open for now until you’re certain the error is resolved and your website is working again. If you’re still getting the same error message, you’ll need wp-config open for the next step.

If this doesn’t apply to you, or if it didn’t work, move on to the next step to continue troubleshooting.

2. Check your database login settings in wp-config

If the above step didn’t work or didn’t apply to you, then the next thing you can do is take a look at your database settings in your wp-config file.

Wondering how your database login credentials could have suddently stopped working? Things like switching hosting companies or changing anything about your database name or user information could cause the error if your wp-config file wasn’t updated to the new information.

You may still have the wp-config file open from the last step. If not, log into your host’s cPanel and open the File Manager. Navigate to the directory where you installed WordPress, and look for a file called wp-config.php. Go ahead and open that for editing.

You’ll see the login credentials for your database, probably near the top of the file. It should look something like this:

As you can see, there are four pieces of information WordPress needs to access your database:

The name of the database (“DB_NAME”)

The login username (“DB_USER”)

The login password (“DB_PASSWORD”)

The database host (“DB_HOST”)

If any one of these values is incorrect, WordPress will not be able to connect to the database.

One way to check on your database outside of WordPress is by using PHPMyAdmin, a tool for accessing and editing MySQL databases. PHPMyAdmin is included with most hosting plans, and you can find it in your cPanel dashboard.

Warning: Be very careful using PHPMyAdmin, since you’ll be dealing directly with your site’s database.

Once you’ve logged in to PHPMyAdmin, you will see a list of databases on your server in the left-hand column. Click on the one that matches the name in your wp-config file (it will be the value after DB_NAME, where it says “database_name_here” in the above sample code).

(Don’t see any databases? Contact your hosting company, since the entire problem may be an issue with your server.)

After clicking on your database name, you’ll see the main screen populate with the names of the tables in your database. To make sure this is the correct database, you can find the table named “wp_options” and click on the ‘Browse” option next to it. You should see your website’s name, URL, and your other general settings here. Now you know whether or not you have the correct database name in your wp-config file. If it’s not correct, go ahead and fix it in wp-config.

Now let’s check on the username and password.

There are a few ways to do this, some more convoluted than others. I’ll give you two options.

You can either create a simple .php file to test whether you are able to connect to your database with the credentials in your wp-config file, or

You can just create a new user and password, and update your wp-config file with the new info. You’ll have to do this if #1 fails, so you might just want to skip to this step.

Option 1: Test your existing credentials.

Create a file in your WordPress directory called something like testconnection.php (the name doesn’t matter; just the .php extension). Paste the following code:

Once you’ve created that file, just navigate to the URL of the file in your browser (e.g. your site.com/testconnection.php). You’ll see either a successful connection message, or an error with more details.

If the username and password isn’t working, we can just create a new one.

Option 2: Create a new database user and password.

We can do that using a different tool available in cPanel called “MySQL® Databases.” Go ahead and click on that, then scroll down to the heading: MySQL Users: Add New User.” Choose a username and strong password, and make note of them for your wp-config file. Click “Create User.” Now scroll down to the heading “Add User To Database,” and choose your new username and your WordPress database, and click “Add.” Update your wp-config file with the new username and password.

Now your database name, username, and password are all correct. That just leaves DB_HOST.

In most cases, this value will be “localhost,” but it depends on your host’s settings. WordPress has a list of common hosts with their DB_HOST values on their website. If your host isn’t listed there, you can contact your hosting company to ask what value to use.

3. Still getting the error message? Check with your hosting company

If you’ve followed the above troubleshooting steps and your website is still down, it could be a problem on your host’s end. Contact your web hosting company’s support and give them all the details of the error and the troubleshooting actions you’ve taken, and they may be able to help you determine the cause of the error.

By KeriLynn Engel

KeriLynn Engel is a copywriter & content marketing strategist. She loves working with B2B & B2C businesses to plan and create high-quality content that attracts and converts their target audience. When not writing, you can find her reading speculative fiction, watching Star Trek, or playing Telemann flute fantasias at a local open mic.

Kerilyn, this is an excellent article on a critical topic, and I’d like to add a bit to the conversation for those curious to drill down a little more (fancy speak for us geeks). From an historical perspective, there were these massive (i.e. ‘expensive’) relational database systems from the likes of Informix and Oracle. The language used to insert, delete, update, and query records from these databases became known as ‘Structured Query Language’ or “SQL”.

As smallish systems emerged, people loved the power and flexibility of the relational databases, but they were just too expensive. Around the mid-1990’s, Ulf Michael Widenius (Monty) from Finland began writing an open-source relational database system which became known as “MySQL”. Of course, open source meant free and those lovers of relational expressions began using MySQL like crazy.

Today, it is beyond prolific. Like most software, it has evolved enormously and powers millions of web sites and, as Kerilyn pointed out, WordPress has it at the core of its operations.

Now, on some hosts such as DreamHost, they put the file servers on one machine (where your PHP, CSS, and HTML code reside inside WordPress), and the MySQL server on another. As Kerilyn alluded to, if that particular machine is down, so is your database, even though all the PHP and other code is being served up just fine. Of course, that doesn’t work, as you may know all too well.

I would squirrel away (and I have) these excellent debugging techniques that Kerilyn graciously shared. If nothing else, it will give you peace of mind and a place to start calmly looking when (not if) you ever encounter the dreaded “Error Establishing a Database Connection”.

Sites do go “down” at times, and in most cases that I’ve experienced it’s from being bombarded by requests from dodgy IPs.

What I would like to do is somehow have an alternate site or page served up to visitors, when the “Error Establishing a Database Connection” message kicks in.

Is this possible?

Sometimes our site/s have been down for hours, and it’s quite damaging. We can’t get into the back end, either, at these times. Being able to offer an alternate, non-database driven area on the site would be helpful.

There could be another reason, your mysql server might have low memory. A recent add on or upgrade to your operating system or WordPress might have changed your memory requirements. Mysql tends to crash when memory is too low for it work. You can see this in your server when you try to start your mysql server but you get an error and can not start it.

If you can’t switch to a server with higher memory you can setup a swap space or if you already have swap space, increase the size of it.

Also the magic trick is to watch for clues before these problems happen. I suggest that you use some kind of app level monitoring to watch what is happening with your application. I’m not going to advertise anyone so please Google ‘WordPress application level monitoring’ and choose the service you prefer.

Safe hosting to you all.

I’m the cofounder of http://purdox.com FREE one click WordPress installation on the Cloud. Yep, I advertised myself but nobody’s perfect;)

First and foremost I suggest you NEVER trust your hosting company completely. It is up to you to have a complete off site / server backup of your files and database. Even for those of us who run our own servers and have restore ability from the WHM it is important to have off site backups.

Nobody is going to have more interest in your website backups and security than you are. If you are a novice I would advise hiring a professional who knows what they are doing to help protect your information.

WordPress is so easy to initially setup and manage that tons of people are doing it themselves. That is fine for a basic site but when the chips are down you will want to have an experienced WordPress specialist at your side. This isn’t the kind of website a friend of a friend should be helping you with anymore.

Trust me, I’ve done hundreds of WP sites and hosts are simply a bunch of great guys and gals in tech support. They are not going to lose their livelihoods if your website goes away forever or needs completely rebuilt.

I’ve seen many database errors and the above article is great as long as you have a good backup and someone that knows how to access your database.

While not a database error, I did run into the equally-dreaded Internal Server Error a few days ago. It happened right after updating to the latest version of Jetpack. It asked me to activate a new feature and BAM, Internal Server Error. Fortunately the main website was up and running, however the admin panel wasn’t accessible. For roughly an hour I ran through all the common troubleshooting techniques. Renaming .htaccess, disabling plugins by renaming the folder, increasing PHP memory to 64MB, replacing the wp-admin and wp-includes folders. I even tried restoring the website to a previous date from my hosts control panel.

Nothing. I kept receiving the Internal Server Error message.

As a last resort I shot an email over to my host provider for assistance, hoping they could point me in the right direction. Shortly after sending them a ticket, I grew curious about something. I tried accessing the admin panel in another browser (Chrome) and…it worked. I was equally relieved and frustrated. Relieved because I knew how to fix the issue, and frustrated because I wasted time on something that shouldn’t have happened in the first place.

By working in Chrome, I knew the issue was with Firefox’s cache. For some ungodly reason Firefox wasn’t updating the cache after resolving the original issue. So yes, I did indeed receive and error message, but it likely resolved itself shortly thereafter. Firefox simply did not update the cache to reflect this, and thus kept showing me the error message. A quick clear of my history and cache solved the issue.

Annoying, to say the least.

James
July 29, 2015

Thanks for that comment Evan, saved me what I’m sure would have been another frustrating hour banging head on desk 😛

That’s happen to me twice now – 2 different sites. The first time I panicked and did a lot of Google searches and reading… ended up adding new username & password to database via cPanel MyPhp. Then edited wp-config. All was fine – site came back. Next time it happened months later on another site, I didn’t panic but couldn’t remember what I did the first time. 🙂 Finally figured it out and the new user/pwd trick worked again.

Wow – this is the definitive article on this subject in my opinion. I just had this problem earlier this week, and spent hours researching how to fix it. I found bits and pieces of the advice you are giving, but nothing anywhere near this clear. All the others assumed that I already knew what they were talking about.

I keep good backups using BackupBuddy, and was able to restore the site that way faster than troubleshooting with the incomplete information I was able to patch together. I’m keeping this article in PDF format on my cell phone, where I can access it any time, anywhere.

Thanks – and a thanks to commenter “Evan” who has an excellent point about checking the site in a different browser after attempting a fix. I actually run a different browser in “private” mode to check results after attempting a fix.

I monitor my website using Pingdom. It does a string check every 10 minutes to make sure a line of text is visible on the homepage, then sends an email (or optional text message) to me when the site is down. I am using WP-Engine and it never goes down.

From my experience, it was perhaps more than 90% of the times caused by the third mentioned issue: mysql db was down. I solved it by putting up a chron job to restart mysql service every 2 hours. After that, I never had the problem again, really!

Mostly this message is shown when the hosting provider runs an upgrade or system maintenance. After a short period of time it is restored. The best solution is to select a hosting provider in the country you are targeting, because the upgrades and system maintenance occur during the night and will not lose much traffic.

This line of code
$test Connection = mysql_connect(‘localhost’, ‘root’, ‘password’);
has a bug.

You have to remove the space between $test and Connection.
The correct is
$testConnection = mysql_connect(‘localhost’, ‘root’, ‘password’);

Sometimes, when i try to open my webpage (http://allabout.ge/) when i enter adress in browser adress bar, appears just white space nothing more, then after i click again enter then opens the page. will this tutorial help me to solve this problem? Tahnk you very much! 🙂

Hi KeriLynn,
So I have been having some issues with my website database and it involved loading my portfolio pages. When I did some test, I found that there were multiple connection. Can I use the repair method:
define( ‘WP_ALLOW_REPAIR’, true );

This will allow me to do a repair and optimization of the site, I was planning on redesigning the site to correct the issue but I am will to try this first. I would love some input on this before I do though.

Hi elegant i am in problem , two month ago i purchase a plan with godaddy manged wp hosting i writen here atleast 44 article every thing is right today i m going to my site for writing a article i receive a bug on home url
Error establishing a database connection

and on http://www.bestiper.com/wp-admin like this
Error establishing a database connection
This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at best2339903321.db.2339903.hostedresource.com:3308. This could mean your host’s database server is down.

Are you sure you have the correct username and password?
Are you sure that you have typed the correct hostname?
Are you sure that the database server is running?
If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

now i try to fix them i read almost article about them. but no found a good and after reading your post i have question… i login my panel in godaddy and i thing may be i fix with upload backup but nothing hapend.
i try to conatct there support team but after searching k goadaddy have no email support i m upsed
they provide a phone support but due to low quility english i can’t cotact them.
is there any sulotion to fix this. because i not done any thing wrong i file or change user pass…i think its be a server down eror
so guide me how many time to come back server on postion?
thanx if you help me i bookmark the page….need a quickly responce

Sanya Tayal
August 4, 2015

Hey there, I have been suffering from the same problem. No solution here too!
So if u come across any information worth sharing, kindly reply.

It’s a great article KeriLynn. But I am a beginner so I don’t want to do these things.
Recently I started a website from godaddy, and after installing wordpress I got “Error establishing in database connection” 3-4 times after after 1-2 minutes site becomes fine. Now, is there any need to do these things, because currently it’s working fine but like yesterday I don’t know when it will show error.

I’m unable to connect to my phpMyAdmin through cPanel – so I’m guessing it’s a MySQL connectivity issue – right? How do I go about remedying that? Do you know any commands specifically that would reboot phpMyAdmin so my database can connect again?

Thanks,

Fox
March 4, 2015

Recently I have come up against the following weird situation: my WP site loses connectivity with the database, let’s say once every 2-3 days and for a timespan of…maybe 15 minutes, and while the database is unavailable, WP serves the installation page ./wp-admin/install.php,,, then after 15 minutes the site gets back to normal as nothing has happened. I contacted the hosting provider, they said everything seems to be from their point.
Now, I have 2 questions (if anyone else faced similar incidents):
1. Could anyone else reinstall successfully my wordpress software while wp-admin/install.php page is displayed? (i read a post written 5 years ago where is said that it may be very likely, but that posts addressed old versions up to 2.8,,, now just wondering if wordpress fixed such an issue, otherwise it could be a serious vulnerability as any visitor will see the my installation page while Mysql database is down!!!
2. I noticed also that mysql DB is 3 times smaller compared to 3 months ago. In december my database was like 387 Mb, now is like 100 Mb. Is there is possibility that any kind of software upgrade was made on the server that compresses the database? I want to note that besides those short interruptions of up to 15 minutes, the site looks and runs perfectly, no issue.
Kind regards!!

himanshu dhingra
March 13, 2015

Thankyou so much for this article.
Changing the username password worked.

Happened to me 4x in 2 days! The first times, my hosting provider fixed it for me, but it keeps coming back. Changed the SQL accounts. It works fine now. I hope it wouldn’t come back. I’m updating the plugins too and deleting the unnecessary ones.

I just followed your troubleshooting article after experiencing this issue. I don’t know how, but the username and password no longer worked. I created a new username and password, plugged it into my wp-config file, and all fixed. Thanks a bunch!

Kristin
February 10, 2016

I had the same Parse error issue. After some effort, (and not knowing php very well) I determined there’s an extra space in the first occurrence of $testConnection — on ‘line 2’. Should be one word. Odd the author didn’t notice that!

Got that fixed, now just have to figure out the “Error: Access denied for user ‘root’@’localhost’ (using password: YES)”

This is a great article for the begginner to start with debugging the error. Sometimes too many connection to database also give the same error but at the same time works for many users. Confusing huh! But It happens.

My website http://karaokeonlineaz.com often have been the error. Error establishing a database connection. After I “Go to WHM > Restart Services > SQL Server” to restart SQL Server, the error is resolved.

But my website http://karaokeonlineaz.com often still have been the error every one or two day. Can you help me resolve the error?

Thanks alot!

Erica
June 19, 2015

Thank you for this!! Just totally fixed my site going through the troubleshooting steps. You are awesome.

Just wanted to say thanks to the author of this post! Adding a new user to the database worked!

Bryan William
July 24, 2015

Recently I have transferred from Arvixe to Phi 9 web host.
Actually I forgot to change the previous host “MySQL Connections Strings” in wp-config.php.
Now it’s working. Thank you for your great article I really appreciate it! 🙂

Christopher
August 4, 2015

Actually, I’m getting “Error establishing a database connection” in my home page only. All other pages are working fine. Please let me know what to do.

Kang Tri
August 11, 2015

Problem solved after connecting to the internet, i do not know what happens why in offline mode database running low and always get error “error establishing a database connection”

Sir Help Me Please
My Website Is Loading, And Working Fine But I Get Error Establishing Database Connection When I Tried Wp-admin And Again I Reloaded Page 3-10 Times And My Site Is Working Again And Again I Get That Error Unexpectedly And My Whole Site Shows Same But Again After Some Refresh , My Site Is Working Again… Help Me Please

So our site is currently down saying Error establishing a database connection and have tried every solution. I have looked into the wp-config.php file to see if it has the correct information and it all does. Next I tried to see if the databse is corrupted by doing the repair trick by adding define(‘WP_ALLOW_REPAIR’, true); and when I go to the link it still gives me the Error. The wordpress repair doesnt show up. Any ideas why? Thanks

Many Thanks for a very useful guide!
Our site was hanging exactly as described. Followed the steps with no response
Tricky bit on the test connection php script because of the extra space in the second reference to $testconnection but once removed this showed the database was down.
I logged into my hosts (1&1) phpadmin panel and everything looked ok. I clicked into the wp-users table to make sure my login was there. Couldn’t see anything that looked wrong at all.
I re-tried the website and it magically sprung into life!
Everything appears normal and I can’t see any problems on the site.
Looks as if the database locked up although I have several other on this 1&1 package and they were all ok.
I’ll leave the test script in place for next time!
Great article for a quick fix – wonder how long it had been down……

If I have wordpress posts on an external page, how do I ensure the page still loads cleanly if the database is down?
Right now I use an include(‘wp-blog-header.php’) at the top of the external page then do the query loop. If mysql isn’t running the page only shows Error Establishing a Database Connection. I want it to just skip the blog posts if there’s an error.

I have the same issue. I don’t why it happened. Most time there wasn’t any problem with database connection. I can suspect that some updates cause a problem. Maybe last wordpress upgrade with some plugins but frankly I don’t know.
I know only that there is a lot apache process that consuming a lot of memory and when that happens mysql crashes too. I made some improvements but to recover server and some setting changes but frankly I doubt if that helps for a long time. I do have vps hosting but not sure now if it is better solution from shared.
I see that jetpack using cron. It can be also W3 cache. It usually happens after a week in some period of time and last about 10-24 hours, so it could it. I hope it can help somebody.

I know only that there is a lot apache process that consuming a lot of memory and when that happens mysql crashes too. I made some improvements but to recover server and some setting changes but frankly I doubt if that helps for a long time. I do have vps hosting but not sure now if it is better solution from shared.
I see that jetpack using cron. It can be also W3 cache. It usually happens after a week in some period of time and last about 10-24 hours, so it could it. I hope it can help somebody.

I have weired issues with my wordpress installation.
In trying to install wordpress I created a database with a unique name. I the config file, I input the required connection field, and i was told that wordpress could connect to my localhost server, but not able to select the database. I had spent several days trying to trouble shoot the problem, when suddenly i created a database with the name ‘test’ and it worked. I have tried several other names for database non is successful other than the name ‘test’. I have not had this issue before although i have not used wordpress in a while, may be it is from my wamp installation. But I dont know what the problem is.
Best regards.
Queeneth.

Oh my god. Thank you SOOOOOOO much for this post.
I found resources helping with this error that were suggesting I needed to log into my wp-admin backend, which I couldn’t access… because of the error!

This post saved my website and many, many hours of frustration.
My host support was basically useless and takes days to respond- no live support. Thanks to this experience, I’m considering changing hosts.

Just following this post step by step, checking everything, and I was able to get my site back up and running after the last step.

I can’t adequately verbalize at the moment just how relieved I am that this worked.