If you come across the message, “error establishing database connection” when loading your WordPress website, there could be several underlying causes. Some of the different reasons include a crashed database due to low memory, incorrect database login details, or errors with the database tables. There are even more reasons why this error can be caused.
The database is at the core of any WordPress site. MySQL stores all the key information related to your site. So whenever someone visits your site, a request is sent to the MySQL database to furnish the required information. The information is then presented on the visitor’s browser. But if there is any problem during this connection, it can result in the ‘error establishing a database connection’ message.
This guide helps you fix this error based on the underlying cause.
Some of the common causes of the ‘error establishing a database connection’ message in WP include the following:
- Incorrect Database Login Info: This can be caused due to typo mistakes, additional spaces, or incorrect quotes. When changes are made to the login details, it can block your WordPress site from getting access to the database.
- Corrupt WP Database: Issues like poorly developed plugins, themes, and add-ons can corrupt your database. Even interruptions during data transfer can cause the error.
- Server Unavailable: The error can also occur when the server housing your data has any issues. It can become unavailable due to several reasons including high traffic surge.
So how can you fix this WordPress error? If you are having the error message due to the above-mentioned reasons, you should be able to address it using the following steps.
WordPress Site & Database Backup
Make sure to start by taking backup up your WordPress site and database. In fact, it is important that you take WP backups on a regular basis. If this error has been caused by a database corruption, it is also likely that there may be some errors in the database. Before you begin the below-mentioned steps, make sure a fresh backup is maintained separately.
1. Incorrect Database Login Details
The database connection details are saved on the server in the file wp-config.php. when you move your WP install between different hosts or servers, it may be required to update these details.
- Search for the wp-config.php file in the WordPress root directory
- Open this file
- Check the following information in the file to ensure that they are correct as per your records:
- define(‘DB_NAME’, ‘database_name’);
- define(‘DB_USER’, ‘database_username’);
- define(‘DB_PASSWORD’, ‘database_password’);
- if you notice any incorrect information, make the changes and save the file.
- You will be required to furnish a password
- Run the following command while adding the right details from the config file: mysqlshow -u database_username –p
- Enter the password and if you get the message ‘access denied error’, it means that the username/password are incorrect
- If the username/password are correct, you will get an output
- Check the output to ensure that one database matches with your WP config file
If it matches, it means that there the ‘error establishing a database connection’ has not been caused due to an incorrect wp-config.php file. You can find more detailed information on this error in this 000Webhost blog post.
2. Corrupt Database
The next step is to ascertain whether the WordPress error has been caused due to corrupt database.
- Open wp-admin
- Open yoursite.com
If both these attempts result in the ‘error establishing a database connection’, it means that there may be no issues with the database. However, if you do come across this error, you should check the database for errors. It is quite simple to fix a corrupt WP database.
Locate the file, wp-config.php from the root directory using the following steps
- Log into your cPanel
- Access the File Manager
- Open WordPress root folder
The wp-config.php file is located in this folder.
- Open the file in ‘editing’ mode
- Right click on the file and click on ‘code editor’
- Add this code to the bottom of the file: define ( ‘WP_ALLOW_REPAIR’ , true);
- Click on save and close the file
This code lets you to repair the WP database without the need for logging manually into phpMyAdmin.
- Open the following: yoursite.com/wp-admin/maint/repair.php
- Click on ‘repair database’ or ‘repair and optimize database’
An automatic process will take place on the screen to fix the WordPress database.
3. Server Unavailable
If you tried the above-mentioned steps and are still facing the error establishing a database connection in WordPress, the issue could be linked with the host server. If you think this is the underlying cause, you should get in contact with your host. It may be because the server resources are unable to handle the surge in traffic, if you are using a shared hosting service.
A quality web host will take the appropriate steps to ensure that uptime doesn’t get affected. If the host is unable to fix the problem, you should consider moving over to some other host.
If the error establishing a database connection is caused due to traffic surges, you can take some alternative steps too. Install a high quality caching plugin. It should help save pages and posts as static files, thus reducing the stress on the server. However, if the issue is caused from the host side, you should let them find and fix it.
You can also fix the error establishing a database connection in WordPress error by restoring your site from a backup. For more details on the steps involved in this process, it is recommended to check this amazing blog post on 000Webhost. As already mentioned, all WordPress site owners should take regular backups of their website and database. This will keep you safe in case of any kind of error. This database connection error may sound to be something complicated, but you can fix it by following the above-mentioned simple steps. And it doesn’t take you more than a few minutes to address it.