The “Error establishing a database connection” that many a WordPress user has seen is certainly annoying, but rest assured it is most often an easy enough fix. Follow along below and by the end of the article, I suspect that you will feel confident enough to tackle the problem yourself.

The Error Mostly Occurs Because Of Incorrect Information In The wp-config File

WordPress wp-config file portion

The wp-config file, among other things, is the source of the information for connecting to the MySQL database. Most of WordPress’s data including posts and configuration settings is stored in the database. Before installing WordPress, the database must exist so that it can connect to it, add the necessary tables, and plug in the initial configuration information, including “site title,” “admin email,” etc.

And when no connection can be established to the database, the obvious error of “Error establishing a database connection” occurs. The first thing to check is the settings for the database. Use the steps below to compare the necessary information which includes database name, user name, password, and database host.

Check The Host Name First

First, make sure that the host name is correct in the wp-config.php file. In most cases it will be ‘localhost’ but with web hosts like GoDaddy that won’t be the case. Just Google “how to find mysql hostname for {web host name},” so for GoDaddy type “how to find mysql hostname for GoDaddy.”

Next Check To Be Sure That The DB User Has All Priveleges

Next, you can check to be sure that the DB username is accurate and that the user is given “all priveleges.” You will need to be certain of your database name for this step.

If working with Control Panel, go to Databases >> MySQL Databases. Locate the database in the list of Current Databases and click on the User foung in the “Users” column.

Grant “All Priveleges” if not already set, click “Make Changes” and check to see if the error disappears.

Create A New User With All Priveleges Attached To The DB If You Don’t Know The Password

If you aren’t sure the password is correct for the user attached to the database, the best bet is to create a new user. You can’t edit the password in most cases unless you know the existing password, soand since we don’t then creating a new user it is.

To create a new user, first go to: Databases >> MySQL Databases. Head down to the Add New User section, enter a name, and a password. Make note of both values. Keep in mind that for shared hosting accounts on Control Panel your Control Panel username along with an underscore will be pre-pended to the choses username.

Input these new values into the WordPress wp-config.php file, save and upload and test to see if the error disappears.

Login To phpMyAdmin If You Don’t Know The Database Name

If your naming convention doesn’t make it obvious what the database name is for your given site, you can find out the database name from phpMyAdmin. Incidentally, you can also learn the Database Hostname in there as well.

In your Control Panel go to: Databases >> phpMyAdmin. Choose a database name that you presume may be the one your site belongs to, or take a wild guess. Near the top on the left side will be the hostname followed by an arrow and the database name. It may come in handy to make note of those values for later if this turns out to be the correct DB.

Now, on the left side locate the “options” table. The table will have a prefix, most likely “wp_” but it could be a custom name as well. Select the table and on the right side you should be in “Browse” mode for the table, but if not choose the “Browse” tab at the top left. The value at the top for “siteurl” should give you a clear indication for which site this database belongs to. If it’s the wrong DB, slect another and repeat the steps.

Make Sure wp-config.php Matches The Database Info And Test Again

Using the above information you should be able to determine all of the necessary information to plug into wp-config.php to be rid of the “Error establishing a database connection” annoyance from the WordPress installation. My guess is that in 9 out of 10 cases, this will correct the problem.

Other Things To Try, Like Deleting Plugins That Were Just Installed

Failing that the problem is resolved by making certain the four values (DB name, DB username, DB username password, and DB hostname) are correct, here are a few other thins to consider.

1. Perhaps a recently installed plugin (or even theme) created the issue. Login by FTP and go to the /wp-content/plugins/ folder and delete the plugin that was recently installed. Be sure to re-upload it if that wasn’t the culprit. Database backup plugins sometimes create this issue.

2. The MySQL database server, may in fact be correct in wp-config, but may just be down at the moment. If that is the case you certainly won’t be the only one affected by this issue. If your host provides a bulletin on current known issues then you should learn the answer rather quickly. Otherwise a quick support forum entry (or broswe) or call to (or live chat with) the support staff should get the answer to that question promptly.

3. If you were testing your deployment locally (on your machine) before launching on an actual domain there may be some database entry problems. WordPress provides instructions in the Codex for “switching domains,” which is essentially what you are doing when going from the sandbox to deployment. Value such as “siteurl” and more in the database may need updating if this is the case.

4. While this is largely out of scope for this article, if a MySQL database is simply too big, there might be problems for apps such as WordPress to connect to it. A lot of times this has to do with many post and page “revisions” being stored in the database, mostly unnecessarily. What is considered “too big” I don’t know but if this is the problem suspected then you may need to go into phpMyAdmin and do some careful pruning. But again, beyond the scope.