- Case Studies
- Company Journey
- Digital Marketing
- Product Updates
- Web Hosting
- WordPress Hosting
- WordPress Plugins
- WordPress Tips and Tricks
5 Ways How To Fix Error Establishing A Database Connection In WordPress
by Jeff Trumble on
How many WordPress sites do you create each week for clients? One? Three? Seven? After a while, the WordPress installation happens without much thought. It’s like driving a car. Maybe you’ve got the window open and your favorite tunes on. Then you look up at the screen and see this: “error establishing a database connection.”
“Error Establishing a Database Connection” is one of the most common errors when running WordPress. Most often we see it shortly after installation and sometimes during the operation of a website. As we all know, WordPress — like any CMS — requires a connection to a database to operate. No database means no settings, no posts, no long-tail keyword optimized headlines. The list is a long one.
Why Can’t I Log in to WordPress When I Get the Error Establishing a Database Connection?
Unlike some errors you may encounter — like the WordPress White Screen of Death (WSOD) — this error speaks for itself. It means there is no database connection. The entire website is unable to load, including your dashboard. So in order to fix the error, you’ll need to get back in behind the scenes.
This situation is probably not the end of the world. If your customer depends on the website for sales, it’s almost the end of the world. You can see the end of the world from your desk, but you’re not quite there yet.
If your client’s website uses caching, then visitors may still see stored copies of pages they have previously visited. That means, that if you catch the error early on and resolve the problem before the cache refreshes, you can avoid many painful interruptions to your client’s website and their business. And yours.
“Smart Caching is the ability for us to treat every asset in our cache in a specific way. We’ve always had different cache-control headers for each asset, but now we’ve gone a step further to better identify when and what needs to be purged.”Rocket.net
Thankfully, as far as website errors go, a database connection error is normally pretty easy to resolve. The most common cause is incorrect login credentials for the database. That’s easy enough to correct. It’s just like backing into the planter box in the supermarket parking lot. No one is hurt, nothing is broken, and maybe no one noticed.
“When you see the error establishing a database connection on your computer screen, our first piece of advice is ‘Don’t Panic.’”Douglas Adams
What Causes Error Establishing a Database Connection in WordPress?
For your WordPress blog to run smoothly it needs a database. If the connection to this database can’t be established, the ‘Error Establishing a Database Connection’ notice will be displayed in the browser instead of your website. Since there can be several causes for this problem, you need to narrow down the reasons.
Reasons you’re seeing the error notice:
- Incorrect information in wp-config.php
- Database does not exist
- Corrupted database
- Database server is not accessible
- Corrupted WordPress files
How to Fix the Error Establishing a Database Connection
Reason 1: The Wrong Database Access Data is Stored in the WordPress Configuration
Most often, this error occurs when the wrong database connection credentials are stored in the WordPress configuration and, therefore, WordPress cannot connect to the database. This can happen if you change the password for the database while the system is running without also making the change in the WordPress Config file.
It can also happen — and this is not an error — when you move a website from a local to a live server. Your local database information will be different from the live server database.
To fix this error, open the wp-config.php in your editor of choice. You can do this e.g. directly via SSH or download the file via FTP to your PC and edit the file locally. You probably have a favorite workflow.
The following entries in the file are responsible for the database connection in WordPress:
define(‘DB_HOST’, ‘localhost’); /*this is the web address of your database server*/
Adjust these entries in your wp-config.php accordingly. The database user and database name is given to you by your system. You have assigned the password yourself when creating the database. But you can reset the password at any time if you have forgotten it.
Afterwards, WordPress should be able to connect to the database without any problems.
Reason 2: The Database Does Not Exist or Has No Content
The error message is also displayed if the database does not exist or does not contain the required tables. This can happen, for example, if you move the WordPress installation from another provider to another, or from a local to a live host, but have not yet imported the old database properly.
Check via the tool phpMyAdmin to see if the database exists and also has the correct content.
Fun Fact: It is not unknown to have created a database for a project, and to have forgotten to have imported the exported data before connecting. Stuff happens! We’re not naming names, but it was in fact (Tom) who did this.
Reason 3: The Database is Damaged and Needs to be Repaired
If the database or individual tables are damaged, this error message can also occur. This can happen in rare cases, for example, if the connection is broken while a database entry is being written.
You can easily fix this by repairing the database.
To repair the database, simply use your phpMyAdmin tool. There you can select the tables of the database and afterwards select the option “Repair table” (see image below)
You do not need a plugin to repair your database. WordPress has a built-in feature to automatically repair and optimize the database. To enable this feature, the following entry must be stored in wp-config.php:
define(‘WP_ALLOW_REPAIR’, true); (see image below)
After this entry is stored, the following URL must be visited to optimize and repair the WordPress database
Reason 4: Corrupted WordPress Files
Another possible reason for this error is that there are corrupted files in your WordPress installation directory. It is possible that hackers accessed your WordPress website and modified the files in your WordPress directory by adding new files, deleting existing ones, or editing your original files. Protecting your database is why you renamed your table prefixes.
The error could also be due to a problem with a plugin whose files are corrupted. It could be due to an edit you or your team, or someone else made. Check your list of recently edited files, and replace those, or the plugins, with the backed up versions. Always have a backed up version of everything.
Reason 5: The Database Server is Not Reachable
If you have checked and fixed the previous four items and the database connection error persists, then the error may also be due to the database server having a momentary glitch. Stuff happens. “Don’t panic”.
- Wait a few minutes.
- Contact your Managed WordPress Hosting Support.
A shared web server may be slow or inaccessible. A managed hosting provider should guarantee you uptime, so this shouldn’t be the cause of your problem. However, it is worth contacting them to see if there are any problems with your server.
Let Rocket Show You What Connected WordPress Hosting REALLY Means!
Our support team is here for you every step of the way.
Do you want to stop having issues with database connections? We can help. Let’s talk about how we can migrate your site to Rocket.net, the fastest WordPress hosting available anywhere. Launch your Mission today!