ِAn error in establishing a database connection is one of the most common problems that you may encounter on WordPress sites. When this problem occurs, your site will become unavailable to visitors, which may cause the loss of a large number of them.
In order to fix your site and avoid losing your visitors, we have prepared this article in which we will introduce you to the problem of an error in establishing a connection to the database and its causes, and provide you with a number of solutions that you can rely on to solve the problem.
What is the error problem in establishing a database connection?
An Error Establishing a Database Connection problem occurs on a WordPress site when the content management system cannot access the site’s database, which leads to the error message appearing and making the site unavailable to users.
Causes of an error in establishing a connection to the database
There are many reasons that could cause this problem, and below we will mention the most important of them: The credentials to log in to the database are incorrect.
WordPress uses a database to store all of your website’s content, and this database is connected every time someone visits your website.
WordPress needs the following information to connect to the database:
- Database name.
- Username – Database username.
- Database password.
- Database server.
This information is stored in the WordPress configuration file called ( wp-config.php ).
If any of the above items are incorrect, WordPress will fail to connect to the database, hence you will get this error.
A problem with the server or hosting of the website
Any problem with the web server or hosting can cause this error. This error can appear when the number of visits to your site is large and does not match the resources of the hosting plan you chose for your site, etc.
The site’s database is corrupted
Deleting or corrupting any part of the MySQL database information may corrupt it, thus creating an error problem in establishing a connection to the database.
Corruption of WordPress themes, plugins, or files
Damage to any WordPress plugin, template, or file may cause an obstacle in establishing a connection with the database and thus cause this problem.
Ways to solve the problem of an error establishing a connection to the database
There are many methods that you can try to solve this problem, and most often the solution is to review the files or data related to the database and ensure that there is no corruption or incorrect information in it. Below we will explain to you the most important ways to solve the problem:
Solution #1: Verify database information and data
The most common reason is that the database information is incorrect.
WordPress stores database-related information in the wp-config.php file, so we will need to edit, modify, and verify the file.
Steps:
- Log in to the control panel and go to the file manager or use FTP.
- Access the folder (public_html) then (wp-config.php) and open the file to edit it.
3. You will search for the following lines in the file (wp-config.php).
where:
- database_name: The name of the WordPress database.
- username: The name of the database user.
- Password: is the password of the database user.
- localhost: is the name of the server, and it varies from one hosting company to another; Here we need to check the existing information related to database name, username, password and database host.
4. From within the control panel, you must go to the (Database) section and then to (MySQL & Database).
5. After that, a new page will open where you can find out the database name and user name.
6. Here you must verify the existing information and compare it with the information in the (wp-config.php) file and modify accordingly.
After completing verification and saving the file; You must try logging into the site again. If the error disappears, this means that the problem was due to incorrect data, and if the problem remains, you must move to the next solution.
Solution #2: Repair the WordPress database
Sometimes you need to modify and repair the database to solve the problem, by modifying the file (wp-config.php).
Steps:
- Log in to the control panel, access the file (wp-config.php) and open the file to edit it.
- Add the following code
3. After that, you should open the following link in your browser: http://example.com/wp-admin/maint/repair.php, replacing example.com with your website link.
4. After that, a new page will open for you enabling you to modify the database. All you have to do is click on “Repair Database.”
after that; Your site should be working properly.
Note : The user does not need to log in to access the database repair page. Once the database is repaired and optimized; Make sure to remove the code you added in the wp-config.php file in step No. (2).
If the issue still exists, you should move on to the next solution.
Solution #3: Check if the database server is down
If you have reached this solution and all of the above are correct, there is a possibility that the database server is down.
Sometimes, if your site receives a large number of visits, the server can crash; Especially if you are dealing with shared hosting. Your site becomes slow as a result, and sometimes stops, and then you must contact the hosting support team to find the appropriate solution for you.
There is another way to check if the server is down; If you have another website on the same hosting, you should try it, and if the same problem appears, this means that it is from the hosting, and you should contact the service provider.
If you do not own another site, you can verify the database user’s permissions through these steps:
- You must create a new file called (connection.php) and put the following code inside it.
2. Do not forget to replace (localhost) with the server name, (root) with the database username, and (pass) with the database password, then save the file.
3. Now call up the file in the browser, by going to the following link yoursite.com/connection.php. If it shows you “Connected successfully,” this means that the database server is working properly. Now you must return to the file (wp-config.php) and verify Ensure that you have entered the data correctly.
If you see a message “Could not connect:” this means that the user does not have the necessary permissions to connect to the database server, and you must check with the hosting company.
Solution #4: Remove damaged plugins or themes
Removing damaged add-ons or templates can help solve this problem, and you can do this through FTP or through the special control panel provided by the hosting.
Remove the template or plugin that you suspect is causing the error using one of the methods we mentioned in the article ( Steps to properly delete templates and plugins from WordPress ), then reload the site in order to see if the problem is resolved or not.
Solution #5: Other methods
There are many other methods that you can try if the previous methods do not work, including:
- Restart the web server: If you rely on a private server or VPS server to store your site data, try restarting the server, as this may solve many of the problems you are facing.
- Replace the database: Try replacing the database completely, even if it seems fine to you. This may solve the problem, because it may contain hidden problems.
- Hire an expert to help you solve the problem: If you do not have enough time to try these solutions, you can hire an expert through to help you solve the problem.
- Restore a previous backup: Restoring a backup copy of the site before the problem occurred will solve it, so you should create backup copies of the site from time to time.
- Contact the hosting provider: If none of the previous methods work or you are not able to try all of them, write to the hosting provider in order to benefit from their experience in providing a solution to the problem.
Thus, dear reader, we have informed you of the problem of an error in establishing a connection to the database and its causes, and we have explained to you some methods that can help you solve it. In conclusion, it must be pointed out that relying on good hosting and using the latest secure templates and plugins can help avoid this problem from occurring. In the future.
Leave a Reply