Guide To Easily Fix WordPress Error Establishing A Database Connection Issue
“Error establishing a database connection” is one of the most common errors that WordPress websites face. If you are new to this error, then it can be scary for you and may leave you panicking. But you shouldn’t be worried much because this error can be resolved with a few easy steps and save your website from any long-term damage.
Why does this error occur?
As the name suggests, “error establishing a database connection” means that there is some issue with database connectivity. Your WordPress website uses PHP as the programming language and MySQL as the database technology. Purpose of MySQL is to store the content on your site, including images, characters, files, etc. PHP commands are used by WordPress to establish the database connection and then show the content from database to the visitors. When the connection with database cannot be established, this error occurs.
Following are the main reasons that can interrupt the connectivity with database and cause this error:
Wrong login details: If you enter the wrong login credentials for database access, then this error will occur, as your site wouldn’t be able to access the database.
Corrupted files: Some of the WordPress files may get corrupted in cases of failure in updating the plugins, themes, or the core WP version.
Corrupted database: Some plugins or themes may corrupt the database and cause this error.
Heavy traffic: If your site is not able to handle the heavy traffic, then the site may go down showing this error. In such cases, the database can’t respond to all the requests and losses connectivity.
Database server faces downtime: Sometimes, the error can happen if your website server where all the files are stored faces downtime.
How to fix “Error establishing a database connection”?
Following are a number of ways to fix this error:
1. Ensure your database login details are correct
This is the most common reason of this error. Usually when you switch to a new web hosting provider or migrate your database, the new login credentials are supposed to be used in the wp-config.php file. This file is stored in the root of your website. For a successful database connection, you need to use the correct details- database name, username, password, and database host.
define('DB_NAME', 'some_db'); /* this is the name of your WordPress database */
define('DB_HOST', 'localhost'); /* this is the web address of your database server */
To check whether you are using the right credentials, open this file from File Manager in cPanel and verify it. If anything is wrong, then make it correct and save the file. Check if the error has got resolved. If it’s still the same, then issue is different.
2. Check database host details
If the database credentials are correct, then you must check the database host information. Mostly, the hosting providers use localhost for your database, but some of them also use distinct servers. You will have to get in touch with your web hosting provider to know where your database is hosted, and confirm if the database host information you are using is correct.
3. Repair your database
Sometimes, this error can occur because the database is not working properly, and it needs some repairing. For example, your WordPress dashboard will show errors like ‘One or more database tables are unavailable”. In such instances, you have to repair the database. This can be done easily by adding a line of code to the wp-config.php file.
Open the wp-config.php file for editing, and add the following code to it:
After doing this, open this URL for your site to repair or optimize the database of your website: www.yoursite.com/wp-admin/maint/repair.php. In your case, you should go for repairing it rather than optimizing it.
4. Check database server downtime
If the above steps don’t help you in fixing the error, then maybe your database server has got down because of high traffic on the server. If there is heavier traffic on your database and the server that you have chosen is not able to handle it, then your database faces downtime. This mostly happens if you have opted for shared hosting and other consumers are also sharing the web space with you.
First thing to do is contact your hosting provider to confirm if your database server is responding to the user requests. If the answer is negative, then ask them to fix the issue. You should also be using a caching plugin on your site which helps you to reduce the number of interactions occurring on database.
5. Fix corrupted files
If some of the files on your website has got corrupted, then it may also result in showing the “Error establishing a database connection”. The files can become corrupted if these are not transferred properly via FTP, somebody has changed the file extensions, your site has got hacked, or if there is some issue with your hosting provider.
Before doing anything in this case, it is highly recommended that you take the backup of your site. Because to fix this issue, you will have to re-install the core version of WordPress. It’s not about modifying the themes or plugins, but the WordPress installation.
So, take the backup on your computer, re-install the WordPress, and then upload the backup to your site again. Don’t forget to clear the cache of your browser. Now, check if the issue has got fixed.
Following the above steps will fix the “Error establishing a database connection” issue on your WordPress website. If you are seeing this error, then don’t wait and just implement these tips.
In case, you are unable to properly implement any of these steps and not able to fix the error, then it is always great to get in touch with your hosting provider. They will help you out and make your database and website up again.
If you have any questions, let us know via the comments below.