Error Establishing Database Connection in WordPress is one of those errors that can occur unexpectedly and can cause major disruption to your website or business.
This error tells you there is a problem preventing your website from working but gives you little help as to how to fix it.
There are a number of causes that could lead to this error. The database login details used by WordPress may be incorrect, your database could be corrupt, the DB server may be overloaded and intermittently go down or might have gone down completely for any number of reasons, etc.
The good news is, you are not the first to see this error. In fact, many people who have been using WordPress for a couple of years have seen it at some point.
Thus, drawing from experience, we know the most common causes of this error, and the exact steps you can take to get your site up again.
In this guide, we will show you how to fix Error Establishing a Database Connection in WordPress.
- What Does This Error Mean?
- How To Troubleshoot Error Establishing Database Connection In WordPress
What Does WordPress Error Establishing a Database Connection Mean?
If you are aching for the solution to this error, skip to the next section. Otherwise, you can use this problem as an opportunity to learn how WordPress works with databases.
There are two main parts that make up your WordPress site, the WordPress files, and the database.
WordPress files include uploaded images, documents, and your PHP script files. The database stores the rest of your content, including page content, blog posts, comments, site settings, user account details, and just about everything else data related.
When you load a page in your browser, the PHP script file for displaying pages calls on your database to supply the page content. Your database then fetches the content, presents it back to the script file, and it is rendered as a page in your browser.
To communicate with your database, however, the PHP script file needs to establish a database connection, but for some reason, it is currently unable to do this on your site.
Let us see steps you can take to fix “WordPress error establishing a database connection“.
How To Fix Error Establishing Database Connection In WordPress?
Depending on the nature of this error, you can intelligently guess what could likely be the cause. Here are some examples:
1. Do you see “Error Establishing Database Connection” in WordPress posts and pages, but are able to access your admin dashboard with a different, less severe error message (e.g. one or more DB tables are unavailable)?
2. Do you see this error intermittently, that is, sometimes your site loads correctly, but other times it shows this error? This would indicate a problem with your database server so you may want to jump to solution 5.
3. Is this error constant and seen in every part of your site? If yes, then any of the items in the solution below could help you solve it and you may have to try each one.
1. Repair Database using WordPress
A corrupt database could lead to error Establishing a Database Connection in WordPress. You can repair your database using WordPress by adding a line of code to your wp-config file.
To do this, you first need to access your files using FileZilla FTP client and your cPanel details. Watch this video to learn how to use ftp.
After successfully connecting via FTP, you would be able to see your site files in the bottom right window.
There, search for the folder that contains your WordPress site files and double-click to open it. Usually, this folder is public_html if your WordPress site is located on your main domain (e.g. www.yourdomain.com).
If it is located in a subdomain (e.g blog.yourdomain.com), you would see a folder named after your subdomain which would contain your files.
In this folder, locate the wp-config.php file, right-click on it, and select View/Edit.
In the editor, locate the line that says “That’s all, stop editing! Happy blogging” and paste the following just above it.
Next, save and close the file. FileZilla will prompt you to upload the updated file, click Yes.
Now go to this URL (replace ‘domain’ with your domain name): http://www.domain.com/wp-admin/maint/repair.php
Click Repair Database.
When the repair completes, access your site to see if the DB issue is fixed. If yes, then the problem surely was a corrupt database.
Note: After completing this step, ensure you delete the “allow repair” line you added in your wp-config file. Otherwise, just anybody can go in and repair your database.
2. Repair Database Using cPanel
In some cases, error establishing database connection in WordPress could be caused by a corrupt database, but WordPress database repair alone may not fix it. Gladly, cPanel also gives you options to repair your database and you can usually fix the issue this way.
To do this, log in to your cPanel dashboard, locate the “Databases” section, and click on MySQL Databases.
In the “Repair Database” drop-down, select your WordPress database (if you do not know which your database is, you can find it in your wp-config file. Read step 1 above to learn how to access this file.
After opening it, locate the “MySQL Settings” section and look for DB_NAME). After selecting your WP database, click Repair Database.
You should process log with the message “Repair Complete” at the bottom.
3. Create New Login Credentials
As we explained earlier, WordPress needs to connect to your database to serve content to your browser.
To establish this connection, WordPress needs the correct database username and password; otherwise, you get the error Establishing Database Connection message.
Your database username and password are stored in your wp-config file. To remove any doubt about the correctness of these details, or if the username has the correct access permissions WordPress needs, we recommend you create a new DB user, assign this user the correct permissions for your WordPress database, and then update the wp-config file with these new details.
How to create Database User
To create a database user, log in to cPanel. Then, locate the “Databases” section and click MySQL Databases.
Scroll to the “Add New User” section, fill out the username and password fields (ensure your password is strong by mixing letters, numbers, and symbols), and click Create User.
Note: The DB username always has your cPanel username as its prefix. So if your cPanel username is “vic147” and you entered “wpdatab” in the DB username field, the DB username will be “vic147_wpdatab”.
After creating the user, go back to “MySQL Databases” page and scroll to the “Add User To Database” section.
Select the user you just created in the “User” input box, select your WordPress database in the “Database” input box, and click Add.
In the “Manage User Privileges” page, tick “ALL PRIVILEGES” and click Make Changes.
Next, you want to edit your wp-config file to reflect these new details. Since you are already in cPanel, you can do this using the file manager.
Click on the home button (top left). On the home page, go to the “Files” section and click on File Manager. A new tab will be opened showing your files.
Double-click the folder that contains your WordPress files to open it. In this folder, locate your wp-config.php file, right-click on it, and click Edit.
In the text editor dialog box, click Edit.
Locate the “MySQL Settings” section.
In the username section, replace the right side with the new DB username you just created. Ensure you do not alter the quotes or anything else.
/** MySQL database username */
In the password section, replace the right side with the password for the DB user you just created.
/** MySQL database password */
Next, click Save Changes.
Finally, reload your site.
4. Re-Upload WordPress Files to Fix WordPress Error Establishing a Database Connection
Many WordPress users report being able to fix this issue by re-uploading core Files. This makes sense because there are a number of code issues that could lead to error Establishing Database Connection in WordPress.
Follow the steps in the upload section of this guide to learn how to re-upload your WordPress files.
After completing this successfully, reload your site to see if the issue is fixed.
5. Check If DB Server Is Down (Contact Your Webhost)
There are a number of things that would point to a problem with your database server. For example, does your site display sometimes but shows this error at other times? If yes, then the problem is your DB server.
If you have gone through all the steps above and have not resolved the issue, the next step is to contact your web host and have them check the database server.
At this point, the problem is very likely from their end and they should be able to guide you to a solution.
If this problem persists with a particular host, it is time to consider moving to another host. For reliable WordPress hosting, we recommend WP Engine. Our Rocket Plan comes with both WP Engine hosting and Fixrunner support each month to keep your site optimized and error-free.
How Can You Use This Information?
The steps covered above can help you resolve error Establishing Database Connection in WordPress. Hopefully, you have been able to fix your issue at this point. You may also wish to know how to clean your database.
If you still need help resolving this, we can have one of our developers fix it for you. Simply request our service.
Also, are there other steps you tried that solved this on your site? Please share them with us and our readers in the comments section below.
- How to Scan WordPress Database for Malware
- WordPress Database Plugin
- MailChimp for WordPress
- Weebly vs WordPress