Error Establishing a Database Connection – Fix it Fast!
Page last updated: November 2018
Most people who’ve built or used a WordPress site have seen the dreaded “Error Establishing a Database” at least a few times. If it’s your first time seeing this message, don’t worry—it’s nothing to stress out about!
This message just means that WordPress can’t connect to the SQL database for one reason or another, and it’s usually due to a few common errors. Because WordPress is a database-driven framework, the site can’t load if it can’t connect to the SQL database.
With that said, there are some easy troubleshooting steps that you can go through that usually resolve this error quickly. Occasionally the problem can be more complex, such as something wrong on the server-side that your host may need to take care of, but generally speaking it’s something you can resolve on your own. Let’s look at some of the basic troubleshooting steps for this error!
And a brief note before we continue on: you might see a few affiliate links on this page; they help to pay for website expenses and keep the content free for everyone to read! Don’t worry though, they don’t influence our reviews at all.
Troubleshooting Step #1:
Start by double-checking the database name, username and password in your wp-config.php file.
The information in your wp-config is what tells WordPress how to connect to the database. If some of this information is incorrect, even by one character, WordPress won’t be able to communicate with the database and you may see the “Error Establishing a Database Connection” message. The information that’s often wrong is the database name, username, or password. The code for that section looks like this:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
/** MySQL database username */
/** MySQL database password */
Open your control panel (cPanel, Plesk, etc.) and navigate to the database section and double check that the database name, username, and database password match exactly with what’s in the wp-config file. If you’re using cPanel, you may not be able to see the password, but you can reset the password and copy this information into your wp-config file and save it. Once you’ve double-checked all of this information, try and load your site to see if it loads properly.
Troubleshooting Step #2:
Make sure that your database user has been added to the database with ALL read/write/execute privileges.
If you double-checked your database, username, and password information and that didn’t work, you may have a problem with user privileges being improperly set. In order for WordPress to connect to the database, the database user needs to have all read/write/execute privileges and needs to be added to the database as a user. If you installed WordPress manually, it can be easy to miss this step.
The steps to accomplish this may be different depending upon your user control panel. In cPanel, you click on the “MySQL Databases” icon, then scroll down to the bottom of the page. After that, select the user and database that contain your WordPress hosting information from the drop-down boxes, then click “Add.” On the next page, click the check box for “All Privileges” and then click the “Make Changes” button. Now you can test your site and see if the error is gone.
Troubleshooting Step #3:
Check to see what the database location is defined as in your wp-config.php file.
In order for WordPress to communicate with the SQL database, it needs to know where on the server (or another server) the database is located. In most cases, this location is “localhost” but in some cases it may need to be something else depending upon your host. The section in the wp-config file where that information is contained looks like this:
/** MySQL hostname */
If your database location is defined as “localhost” and you’re still seeing the error message, double-check with your host to find out if you need to have your database pointed somewhere else. Some hosts (such as DreamHost) may ask you to point the database to a different URL or location. If this is the case, change “localhost” to that location and try your site again to see if the error is resolved.
Troubleshooting Step #4:
Try repairing the database in your control panel.
Sometimes your database can get corrupted and WordPress has trouble accessing it. Most control panels have an option to check and repair databases. The procedure to do this can depend upon your host and the type of server control panel that you’re using.
If you’re using cPanel, you can click on the “MySQL Databases” icon and then click on the “Check Database” button to see if there are any errors or corrupted sections of the database. If there are, you can click on the “Repair Database” button to try and fix the corrupted sections. This may not always work, but it’s worth trying if you believe that the database may have been corrupted.
Troubleshooting Step #5:
Delete and recreate the database, user, and import the database contents from a backup.
Sometimes it’s hard to pinpoint where the problem is with the database and it’s easier to just erase everything and start over.
If you decide to do this, make sure that you have a current backup before you start deleting things! If you don’t, you risk losing your data or any changes that you’ve made to the site between the current time and your last uncorrupted backup. You might be able to obtain a number of SQL backups made at different times from your host, so you may want to check with them in order see how far their data goes back.
Check your home directory (often public_html on cPanel, or httpdocs on Plesk) for any strange looking files that shouldn’t be there. If your site has been hacked, it’s possible that there are files in the home directory that are interfering with the communication between WordPress and the database. Depending upon your host, sometimes a php.ini file that’s out of place or one that shouldn’t be in the home directory can also cause this error.
If you’re still stuck and can’t figure out how to fix the problem, you might be able open a support ticket with your host and ask them to fix the problem. If you have managed WordPress hosting or a host with comprehensive support, they will most likely be able to offer assistance so you can find the problem and fix it or they may even fix it for you.