People have at least seen the error establishing a database connection a few times. It is one of those annoying problems that would be caused by many possible reasons and factors. Especially for WordPress beginners, this can be very frustrating when it happens and they do not know what to do. In the following article, we are going to present WordPress site owners several ways to fix the error establishing a database connection in WordPress.
Why will have this error
Simply put, WordPress site owners get this error because WordPress cannot establish a database connection, while the reason why WordPress cannot establish a database connection can be completely different. The possible reasons could be:
- the database login credentials are wrong or have been changed
- the database server is unresponsive
- the database has been corrupted
In general, most of the times the error establishing a database connection happens due to the server error, while it could be other possible reasons as well. How can we diagnose and resolve this problem properly?
Does this error occur for /wp-admin/ as well
If the error establishing a database connection happens, people need to ensure whether they are getting the same error on the front-end as well as the back-end of their WordPress website. If both pages show the same error “Error establishing a database connection”, people can go to the next step.
What if people get a different error on the wp-admin, such as “The database may need to be repaired” or “One or more database tables are unavailable”, then WordPress website owners might need to repair their database.
One of the easiest and most feasible ways to repair the database is to add “define(‘WP_ALLOW_REPAIR’, true);” in the wp-config.php file.
When the define is set, there is no need for users to be logged in to access this functionality. Because the main purpose is to repair a corrupted database, and users can always not login when the database is corrupt. WordPress site owners enable to remove this from their wp-config.php, when their database repair and optimization work is done. People might need to find another way to work if this solution did not repaid the database error or fix the problem.
Checking the WP-Config file
In the whole WordPress installation, WP-Config.php is considered as the single most important file. This is largely because it is where people specify the details for WordPress to connect their database.
The DB_Host value may not always be localhost. Based on the different web hosts, it would be also different. If something goes wrong in the wp-config.php file, people might be suggested to fix this problem by replacing localhost with the IP address “127.0.0.1:8889”. Note that IP will vary for web hosting services.
If people find that everything in this part is correct through checking the WP-Config file, maybe the problem is caused by the server end.
Checking the Hosting Provider and MySQL Server Configuration
Sometimes, it occurs for the error establishing database connection when the WordPress website gains a lot of traffic. Since the hosting provider cannot handle the load, and then the website becomes slow and even output the error. If it happens, people need to contact their hosting providers and make sure MySQL server is still responsive. Or people can test by themselves.
They can test other websites on the server. If those websites get the same error, definitely, there is something wrong with the MySQL server. If people just operate one single website, they can go to their cPanel and try to access phpMyAdmin and connect the database. If the connected successfully, it means that there is something else wrong and they need to go back to their wp-config file.
There are some solutions that worked for others, and people can try. One thing, they have to make sure that they have sufficient backups.
- if the issue was the WordPress website URL, people can run the SQL query by going to phpMyAdmin and replace YOUR_SITE_URL with the actual url
- connect the database with testconnection.php, and change the wp-config.php user to the root user
- remove the content of active_plugins in wp_options table and edit the contents of recently_edited