One of the most commonly occurred errors WordPress users face is error establishing a database connection. The occurrence of this error may have a few reasons. Such as wrong database credentials or damaged WordPress files.
Error establishing a database connection has a serious tone to it, but like any other WordPress errors, it can be fixed.
In this article, we talk about the error in detail and find ways to fix it. But as always before you make any changes to your database we recommend you get a backup.
Why do we receive an error establishing a database connection?
Error establishing a database connection occurs when WordPress fails to communicate with the database. In fact, this is the main reason behind it. But why WordPress can’t communicate with its database?
There are many possibilities. However, some reasons are more solid than others. For example, maybe you have used the incorrect credential to enter your dashboard.
Maybe there is an error in your database or maybe your hosting provider is undergoing maintenance. Sometimes when there is a lot of traffic going through the website, we may see error establishing a database connection, Since our database cannot handle that amount of incoming traffic.
Users usually see error establishing a database connection in an empty page with that message writing on the top left corner and because there is no connection between your website and its database no content will be displayed. Although in WordPress the error may look like the one above.
Check your database credentials (username & password)
The most basic fix to error establishing a database connection is by checking your database’s credentials. This error usually occurs when you type in the wrong username and password. Especially, when you change your hosting provider you are likely to see error establishing a database connection.
Your database information, username, password, etc. are written in wp-config.php file. This file stores crucial information about your database and if they are incorrect, it will cause an error.
Make sure you check the values of Database Name, MySQL database username, MySQL database password and MySQL hostname (server).
database name: define(‘DB_NAME’, ‘xxxxxx’)
database username: define(‘DB_USER’, ‘xxxxxx’)
database password: define(‘DB_PASSWORD’, ‘xxxxxxxxx’)
database hostname: define(‘DB_HOST’, ‘localhost’)
Keep in mind, some hosting providers don’t use localhost as their database URL. We recommend you to use your hosting’s IP address. This will ensure you are using the right address.
Fix your database
There are a lot of reasons that cause your database to display errors or even break. Sometimes using a template or plugin which is not compatible with your database could cause the error.
A lot of users install and delete a lot of plugins and this may cause some issues for your database. There have been many cases reported that by installing and deleting too many plugins, the database actually deleted some crucial tables and as a result ran to some troubles.
Even if all of your files are opened correctly, moving tables in your database is not advisable. However, you can repair your database by adding the code below to your wp-config.php file:
Then head over to http://yourwebsite.com/wp-admin/maint/repair.php In this page. You will see two options, repair database or repair and optimize database. They both do similar tasks. But repair and optimize option will take longer to fix the issues.
Repair damaged files
WordPress damaged files can also cause error establishing a database connection. Your files may be damaged due to a third-party access, uncompleted transfer from FTP or your hosting provider.
Once again we recommend you to backup your files before making any changes.
You have to replace your files with their newer versions. Keep in mind when we say damaged files we don’t mean themes, plugins, etc. we’re strictly talking about WordPress core files. To do so, download the latest version of WordPress from its official website and follow these steps:
- Unzip the downloaded file
- Delete wp-content and wp-config-sample.php
- Upload the rest to your server
You have now replaced damaged files and hopefully, you have fixed the problem. Once you are done we recommend you to delete your browser’s cache before entering the site.
Lastly: restore backup
Finally, if you have tried everything and still see the error establishing a database connection, then you have no chance but to restore one of your backups. This may roll back some of the recent changes you’ve made to the website. But it also rolls back the damage.
If you don’t know how to restore your WordPress backup read the extensive article we’ve written on the issue.