For many users having rich experience surfing on the Internet, they must have seen the error creating a database connection several times. Actually, it is a common error occurring on the web, and can be caused by various factors.
As we said it is common to see in the web hosting industry, but it is not so easy to fix, especially for those beginners. When it occurred independently without anything changed by you, it is more frustrating. Actually, even for experienced developers and webmasters, this issue can also take you more than 19 minutes to test out as well as fix.
To help you fix this common but tough issue, we plan to compose a tutorial addressing this issue. Following our methods, you can compile all the solutions listed in just one place, as so to effectively fix that error creating a database connection in WordPress.
Note: Before making any database change, you have to ensure sufficient backups.
Factors causing this error
As a matter of fact, WordPress unable to create the database connection is the direct factor causing you get this error. Well, why can’t WordPress create a databases connection? The reason is various.
If your database log-in information is wrong or has been changed, then you WordPress platform cannot create the database connection. If your databases servers are not responsive, then WordPress cannot set up the database connection. If your databases are corrupted, then your WordPress platform still cannot establish the database connection. According to our rich experience, there are other factors which can cause that issue as well.
Now, let’s look at figure out how to fix this problem together!
Does this trouble happen for /wp-admin/?
Before you get into solve this problem, you need to ensure that you are running into this problem on both back-end of your site (wp-admin), and front-end of your site. If it is, then there will be the same error message on both ends’ pages, telling you there is an error of a database connection established. When you confirm that, you can move the next. Well, there are two different error messages. For example, your wp-admin page shows an error message telling you that your database table is not available and your database needs repairing.
To repair the database, you need to add a database repairing command line into the wp-config file. Note: the following command line should be added before the line of “That’s all, stop editing! Happy blogging” in wp-config file.
When you are done, you can view these settings in the means of visiting: http://www.yourdomain.com/wp-admin/maint/repair.php
Be aware of that users needn’t be blogged in for accessing the functionality when the database repair’s define has been set. It is because that your database is corrupt and this define is to repair it. With this circumstance, you cannot login. Besides, if you have done this and repair the corrupted database, you should remove the define out from the wp-config file.
If you cannot perform this command line to repair the database or this define still cannot fix the issue, then you should following us to find out other solutions working.
Check WP-Config file
Beginners may not know that the most significant file in the whole WordPress installation is wp-config file. It is the place to specify the settings of WordPress connection with your database. When you change your database user password or root password, you have to change this wp-config file. There is one thing you need to always check. That is whether the things in the wp-config remain the same or not.
Note: the value of your DB_Host may not always keep localhost. It varies according to the host. As for some popular cheap hosts such as Bluehost and HostGator, the value is localhost, while you can still see other host values in the place.
Some users suggest that they fix their issue by using the IP to replace the localhost. If you are running the WordPress in the local server environment, then you should have seen this kind of issue. And IP can vary for the online hosting services.
If you have done the suggestion, or have checked out everything in the wp-config file is right, even you have examined the typos, but the issue is still here, then you can move to the other side to check the wrong place causing that issue.
Check MySQL Server
After you exclude wp-config file, you should know that when your website gets crowded with heavy traffic, the error establishing a database connection often happens. That is to say, your host server or MySQL server cannot deal with such heavy load, especially when you running the site on a shared hosting server environment.
Under that circumstance, your website will become really slow as well as even for partial users output that error. If that, you should contact your web hosting provider to ask whether your host server/MySQL server is responsive or not.
There are several things for you to examine if the host server/MySQL server running yourself. If you have more than one websites running on one single hosting account, then you just need to test the other websites to check out if these sites are getting the same problem; if they are, then there is the most possibility that your host server/MySQL server has something wrong.
Well, if you just host one single website on your hosting account, then it is simpler. You can directly go to cPanel control panel and then attempt to access phpMyAdmin as well as well connect a database. If it works, then you should check out if the databases user gets sufficient permission. You need to establish a file named testconnection.php and then paste the code like the following image into this test file:
Remember to replace username as well as password. If it is connected successfully, it means your user indeed gets sufficient permission, so there must be something else wrong. Now, you need to turn back to check out if everything in the wp-config file is completely right, including the typos.
However, if the test file can’t connect to a database via phpMyAdmin, you should understand that it is your server having something wrong. It of course not mean your host server/MySQL server down, but it could be your user not having sufficient permission.
Other Solutions
Before we introduce the other solutions, we have to compel all of your attention here – you need to know these methods may not take effect on your problem. Therefore, you need to user them at risks, while you need to make sure you have redundant backups in case there is anything going wrong.
There is a case that a client of a webmaster is encountering an error needing to repair the database. However, after the database is repaired, that error is still there. The webmaster verifies many things and finally understands that the error is at URL. It is changed, so that the error persisted. To fix it, the webmaster run SQL query via accessing phpMyAdmin:
As for you, please ensure to use the real URL to replace YOUR_SITE_URL and if you change your default WordPress database prefix before, then the wp_options should be different. This method can work for some others.
Besides, there is another suggestion. If the testconnection.php file can be connected with the database, then you can just change wp-config.php user to root user. After that WordPress could work well. Then, you revert these settings back to database user. after that, it will continue to work fine. According to the advisor, he finally didn’t find out the error place, and it might be a typ.
Some people suggest removing active_plugins content in the wp_options table; meanwhile, edit the recently_edited contents. After that, the problem seems fixed. However, to see if it works for your error, it needs to test.
If you encounter this frustrating error, then you really have one way to go – just to test these methods we offered and we concluded above. Of course, there is rich experience sharing outside, which you can learn from, and we hope there is always one way working for you!