In this post, we’ll show you what a database connection problem in WordPress looks like. Then we’ll go over four steps to resolve the issue.
Let’s get started!
Why do you get this error? What Causes This Error?
The ‘Error establishing a database connection’ issue can be caused by incorrect database information in your WordPress settings, corrupt database, or an irresponsive database server.
This form of error can be caused by several factors
- invalid login credentials,
- change in port,
- file permission,
- corrupt database,
- changes in wp-config and
- .htaccess.
5 Easy Fixes for Quick Error Establishing a Database Connection in WordPress:
- Check If Your Plugin or Theme Files Haven’t Been Corrupted.
- Check Your WordPress Database Credentials.
- Get In Touch With Your Web Host Provider.
- Repair a corrupt database with WordPress’ built-in database repair mode: define(‘WP_ALLOW_REPAIR’, true);
- Restore The Default WordPress Files
Check If Your Plugin or Theme Files Haven’t Been Corrupted
Your WordPress files can get damaged due to a few misfortunes.
For instance, you might have updated a plugin or a theme, and the whole thing collapsed? Maybe you’ve hooked the site up to an external service? You may have hand-edited some of the files?
If any of the bell rings above, you’re probably going to have your guilty one. If you haven’t already done any of that, skip this step.
First login to your cPanel > public_html (or your website if you have multiple website in the same hosting)
Second: Go into “wp-content” and rename the “plugins” folder to something else
Third: Go back to your site and see if the error is still there.
If not, you now know that it’s been caused by some plugin. Go back and rename the folder back to “plugins”. Go in it, and change the folder names of the individual plugins, one by one. The easiest way to do this is to add an underscore at the end: “_”
Keep coming back to your site to see when the error gets triggered or vanishes. Once you’ve narrowed it down to one specific plugin, you have found the troublemaker. Either delete it or go online to see if the solution has been documented.
- If the error is still there even after renaming the “plugins” folder, go through the exact same process with the “themes” folder.
- If that doesn’t help, rename both folders back to “plugins” and “themes”. The error is caused by something else.
Check Your WordPress Database Credentials
Your WordPress database credentials are stored in the wp-config.php file. It is the WordPress configuration file that contains important WordPress settings including database information.
You need to make sure that the information for the database name, username, password, and database host is correct.
This will open a set of database information and configurations. Find and take note of the following data:
- DB_NAME – database name
- DB_USER – username
- DB_PASSWORD – password
- DB_HOST – database host
You can confirm this information from your WordPress hosting account dashboard. Simply log in to your hosting account and click on MySQL databases under the database section.
This will take you to the database management page in your hosting dashboad. From here, you can find out your database name and the username.
Below that you will find the database users and links to change the user password.
Once you have confirmed your database name, username, and password, you can change that information in your wp-config.php file if needed.
After that, try visiting your website again to see if the database connection error has gone.
If you can still see the error, then this means that something else is wrong. Continue reading for more troubleshooting steps.
Get In Touch With Your Web Host Provider
If you are confident that your database name, username, and password information is correct, then you may want to make sure that you are using the correct database host information.
In most cases, if you tell them what sort of error you’re having, they will either let you know what the cause is exactly, or they will even fix it for you.
Most importantly, they will let you know if your database is alright and whether or not there’s any suspicious activity on the server.
Repair WordPress Database
If you get a different error on the wp-admin, for example, something like “One or more database tables are not available. The database may need to be repaired, “so you need to repair your database.
You can do this by adding the following line to your wp-config.php file. Make sure you add it just before ‘That’s all, quit editing! Happy blogging line wp-config.php.
define('WP_ALLOW_REPAIR', true);
Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php
Note: the user does not need to be logged in to access the database repair page. Once you are done repairing and optimizing your database, make sure to remove this code from your wp-config.php.
Restore The Default WordPress Files
As we mentioned earlier, core WordPress files can be corrupted for a variety of reasons. Maybe there’s been a hacking attempt on your web, maybe you changed some files by mistake, maybe a rogue plugin made a shift.
Whatever it might be, you can always restore the default WordPress native files. The best way to do that is to download a clean WordPress version from WordPress.org and copy it to the one you’ve got right now.
Go here and add the latest kit to your desktop. Unpack. Unpack. Browse the unpacked directory and remove the “wp-content” folder.
Next, link to your site via FTP, go to the main WordPress root, upload your latest WordPress files. Overwrite all of the previous files.
When you have completed the upload, return to your site and see the effect.
This is the last resort to any kind of repair. If this doesn’t help, you won’t have a choice but to go back to your web host support line, tell them everything you’ve done, and ask for help. At this point, you have essentially exhausted everything you could do to fix the issue yourself, and it’s the host’s turn to solve it. After all, you pay your hosting bills, aren’t you?
Other Options That Users Have Found Successful
There are further actions you may take to resolve “Error Establishing a Database Connection” in your WordPress website if none of the techniques mentioned above work.
Update WordPress website URL
Anyone who just switched to new web hosting, Perhaps your WordPress URLs need to be updated. To do so, Simply go to your hosting account’s dashboard and choose your WordPress database.
To fill up the form, copy and paste the following code:
UPDATE wp_options SET option_value = replace(option_value, 'http://www.old-domain.com', 'http://www.new-domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.old-domain.com','http://www.new-domain.com');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.old-domain.com','http://www.new-domain.com');
Make sure to update the URLs for your existing and new websites with the appropriate values for “http://www.old-domain.com” and “http://www.new-domain.com.” Click the Go button once you’re done.
How To Avoid This Error In the Future?
While you do not have a 100% unsure way to secure your site from any data base link failures, there are 2 key elements that should be at least 90% protected in your assurance puzzle:
Hosting quality. Act with hosts only who have optimized WordPress platforms. In this way, you can still depend on know-how and swift assistance. Also, several common issues with WordPress can be avoided.
Have backups. Install a simple backup plugin like UpdraftPlus (it works automatically) and you should always have a working WordPress backup that you can restore in case everything else fails.
Conclusion
Everything in WordPress is dependent on your database. WordPress cannot save or access information without it, which causes your site to malfunction. There are several difficulties that might cause failures while connecting to your database, but thankfully, they are typically simple to resolve.
There are various ways to repair a database connection problem in WordPress, including:
- restoring a current backup of your database
- WordPress theme and plugin files should be checked for corruption.
- reinstalling the core files for WordPress
- Using the WordPress database repair feature
If you are feeling overwhelmed by attempting to resolve this issue on your own, you may want to consider upgrading to managed WordPress hosting to have access to skilled WordPress hosting assistance. You might also hire a WordPress freelancer to assist you in resolving the issue.
Do you have any queries on how to resolve the WordPress Error establishing a database connection? Please leave your questions in the comments area below!