List of content you will read in this article:
WordPress has emerged as one of the leading content management systems available in the market. It allows you to create and customize websites as per your choice. However, you will get plenty of benefits of using WordPress, still, you may face some errors while connecting or logging in to WordPress.
One of the major issues that you may face is the error connecting to the database. There is nothing to worry about as it is a commonly occurring error. This error simply means that your website has failed to retrieve the required information for loading the webpage.
WordPress mainly uses PHP language and MySQL for the database. Whenever a user visits the website, some PHP commands will run and retrieve the data from the database.
If the commands failed to get the data, the website will not load and you will get an error message on your screen. If your website is not able to retrieve the data from the database, you will lose the traffic and customers’ trust in your brand.
In this article, we will understand what are the reasons for database connection failure and how you can fix database connection errors.
How can you fix “Error establishing a database connection”?
It is a commonly occurring error and you need not to worry much as you can fix them with easy steps. All you need to have the admin rights for making the required change. But make sure before you make any changes to your website, you must take the backup of the website. So that you can roll back it to the original state of the website. Below we have mentioned some fixes that help in resolving the database connection error.
- Checking the Login Credentials
- Repairing the WordPress Database
- Fixing the Corrupted Files
- Checking the server of the Database
- Creating a New Database
1. Checking the login credentials
One of the most common errors that we face during the login is an incorrect id or password. This is usually a situation that occurs whenever the owner of the website migrates to a different hosting provider. If you are facing the database connection error warning on both the frontend and backend of the website, then you can implement the below fix.
First, you need to make sure that the database login information or credentials match with the data that is stored in the wp-config.php file. For checking the details of the wp-config file, you need to log in to the website’s server using FTP or WordPress file manager using the hosting control panel. If you are opting for the file manager option, then you need to go to the /public_html/folder, then right-click on the wp-config folder and then select edit.
Once you click the edit option, look for the below details and confirm if you are providing the right details
- DB_NAME – database name
- DB_USER – username
- DB_PASSWORD – password
- DB_HOST – the host of the database
After that, go to the MySQL Databases and check your current database name under the List of Current MySQL Databases And Users section. There, you will find your database name, DB user, and host details as shown below.
If in case the login data present in your wp-config.php file is different from the details present in the MySQL database settings, make the details identical by making required changes to the wp-config.php file as shown below.
After making the changes to the file, you need to reload the website for confirming the changes. This time you should not be getting the error while establishing the database connection. If you are still getting the error, look for the DB_PASSWORD details and make sure it is correct. For fixing this issue, you need to reset the MySQL password from the MySQL databases menu and go to the current database and select change password. Then you can save the changes.
If there is no issue regarding the username, password, and other details, then look for the host details. For fixing this type of error, you need to contact your web hosting company for confirming the database information for your website.
2. Repairing the WordPress database
Another reason for this error to occur is your corrupted database. Mostly such error occurs at the backend and the error is displayed at the front-end like an error establishing a database connection message. But you can easily resolve this error without worrying and much effort. Only you need to make changes to the wp-config.php file by adding the following code snippet before the section “That’s all, stop editing! Happy blogging.” as shown below in the image.
After you make the changes, you can navigate to http://www.yoursite.com/wp-admin/maint/repair.php and make changes to repair the database. From here, you can select the Repair database option to optimize the database option.
You can choose any of the options that you will find suitable. Once you are done with repairing the database, you need to go to the wp-config.php file and then remove the added function, preventing the website from any external abuse and from accessing the repaired page URL without logging into it.
3. Fixing the corrupted file.
This error may also occur due to the corrupted theme or plugin. If you face this problem after you activate the new theme, you will come across this warning. For resolving this error, you need to navigate to the Appearances from the admin dashboard and go to the themes. Then you need to activate another theme for your website from other available options.
If there is no problem related to the theme, then you can deactivate all your plugins and then try to activate them one by one to check which plugin is causing the error. Once you find out the culprit, you can remove that plugin and can install the alternate plugin for that functionality.
If you do not have access to your WordPress dashboard, then you can make changes from the web hosting control panel. For this go to the file manager and open the wp_content folder stored under the public_html directory. After that, you need to rename the currently used theme or the plugin folder for disabling the themes and plugins.
Try not to disable the plugins and themes at the same time. If renaming the plugin folder and reloading the website will work then there is no need to go to the themes portion to check for a solution.
4. Checking the server of the database
If after trying the above-mentioned methods, you are still getting the same error then it might be possible that the issue is related to the MySQL server. Generally, this error occurs due to the heavy traffic on the server and the server is not able to handle all the traffic and users trying to get access and establish the connection.
If your server is facing huge traffic, it will slow down your server and you will not be able to connect to the database. It is the usual case when there is a limit on the number of users using the server. To solve this, you can connect to the web hosting provider and check if your database is still responsive.
You need to go to the dashboard and click phpMyAdmin and then try connecting to the database as shown below.
If the connection is established, then you need to check if your WordPress database user has appropriate permission.
For this, you should create a new file called tes_tconnection.php and use the below mentioned code in it:
Make sure to replace the username and password accordingly and then upload the file to your website, making it accessible from your browser. If accessing the file via browser establishes the connection to the database successfully then the user has enough permissions and there might be another reason for causing this issue.
5. Creating A New Database
If none of the above mentioned methods did resolve the issue then the database is entirely corrupt and it cannot work anymore with your website. In this case, you need to create a new database with the right details and try to make the connection again.
For creating a new database, you need to head over the MySQL Databases top page and fill in the form.
After creating the database, import the backup of the database without worrying about starting from scratch.
Various reasons for this error
We have mentioned various reasons for this error to occur.
- Due to incorrect database credentials.
- If you have a corrupted database that is due to the hacking attempt, plugin or theme failure.
- If there is a sudden spike in the traffic for accessing the website.
- If the database server is down.
Before fixing anything make sure you have access to the admin dashboard of WordPress and the website’s server access for making changes to the wp-config.php file.
Having an amazing website for your brand helps a lot to attract more traffic to your website. But sometimes handling huge traffic may crash your website or cause errors like failed connection to the database. If your request fails to retrieve data from the database then you will get a warning. You can implement any of the above mentioned fixes to resolve the issue. If none of the fixes will solve the issue, you can connect to the WordPress team for more troubleshooting and getting to the root cause of the issue.