What Is WordPress error Establishing a database connection Error:
In WordPress if you see WordPress error establishing a database connection this means the connection from database and your server has been lost or there is some problem with the details stored in wp-config file doesn’t match (this happens when files get corrupted) and WordPress is unable to map from your website and database details.
Database Gets Corrupted:
One of the main cause will be Database gets corrupted you will be facing this WordPress error “establishing a database connection” and sometimes depending up on your server settings and configuration and also of your website content you have to choose to allow repair a database when it gets corrupted automatically, sometimes automatic database repair may leads to other problems as well. If you are constantly facing this issue even though your database details and settings in wp-config.php file are stored correctly then you need to go for this option by allowing database repair by declaring
define( 'WP_ALLOW_REPAIR', true ); and what it does is when ever your database gets corrupted then it will automatically repair your database.
Sometimes you may be facing issues with the hosting provided as well, contact your hosting provider if details provided in your wp-config.php file are correct.
Root Cause of Error Establishing a Database Connection
Until and unless you enable slow queries you will not be able to know what’s happening on your website if you are getting error even after entering correct credentials names, database, username exactly match.
This error may vary depending up on the website as well, like too many users WordPress cant connect to database as well due to overload of requests & WordPress can’t handle those requests and serve content and leads to server crash. In this process WordPress drops database connections due to overload of requests, once database requests has been dropped all the connection and requests will also disconnect and tries to reconnect by dropping existing requests. Now, WordPress will be able to connect again by dropping all the connections and php process request which are executing previously like a fresh connection.
If Again WordPress gets too many requests to get processed then again you will see same error on your website as “error establishing database connection” and again leads your website to crash. This process will be continuous until and unless you solve the server overload.
If you face with this above issue then you need to read this article regarding php process list and server overload issue with WordPress
How to Fix and Solution for Error Establishing a Database Connection:
Enable slow queries on your server so that you get details about multiple queries and how much time each query is able to return content by communication WordPress. Also you need to Dig deeper in to error logs. Check Slow queries log and review your php code where query is executed which is causing issue.
Sometimes this WordPress error establishing database connection may also trigger even if the database credentials and all details are correctly stored.
Debugging WordPress Error Establishing Database Connections:
There are many reasons for this error to trigger in WordPress and also many ways to debug this database connection error in WordPress.
WP-CONFIG Gets Corrupted:
If wp-config file gets corrupted then WordPress will not be able to communicate with wp-config file where all the details of WordPress database, username, password, host names and host names are typically set to localhost along with DB_CHARSET and other details are provide in wp-config file and was unable to get details from expected file and leads to trigger error establishing a database connection.
Debugging error establishing a database connection in WordPress:
- – Check if database name, username, password and host details provided are correct.
- – If Details provided are correct then backup wp-config file and create a new wp-config file under root.
- – Create a new database and try to connect with different database details. If WordPress was able to connect then there is problem with WordPress database.
WordPress Database Gets Corrupted:
If WordPress was able to communicate and database was the issue, then you need to repair the database table in WordPress. Once you repair database in WordPress, then you will be able to connect with WordPress.
Database repair will be the main cause of this error establishing a database connection error in WordPress. Database sometimes in WordPress gets corrupted due to the overload of server request and also it depends on WordPress plugins play a role here in malfunctioning WordPress database.
Error Establishing Database Connection is Not Template file Error
Although, this is a frustrating error for everyone both for users and for website owners or developers. This error is not a strictly a template file or another error to hide it from your visitors. The default database connection error is ugly and exposes a little too much information to your visitor, who hopefully is a good guy and not going to use that information against your website. Always Database connection error leads to exploit vulnerabilities or leads to hack as well. Hackers inject something to your website when website is facing errors or website is down. Even though hosting blocks websites which have issues in connecting to database and even block the website as well.
Enter Correct Login Details in wp-config.php file
To Make Sure all your all details match with wp-config file like host, database name and password.
- – Login to your hosting Cpanel
- – Go to MySql Databases
- – Find your database details related configured
- – Go to file manager -> your website root folder and find wp-config.php file and edit.
- – Enter correct database details like database name, username, password and host.
- – Save changes and check if error establishing database connection is solved or not.
- – If it does not solve then restart your server. It will be solving your error establishing database connection error.
DB-ERROR.PHP for Error Establishing a Database Connection:
WordPress introduced a new function in version 2.5 and later back-ported it to previous versions where, if the database connection fails, WordPress looks for a db-error.php file in your wp-content directory.
You can put whatever code and CSS in the db-error.php template you want, except dynamic data or WordPress functions, because they will not work without the database. This is another situation where we place a typical db-error.php in all of our WordPress sites, with a generic but friendly error message and then notify the development team that an error has occurred.
The forewarning to this is that when things go really wrong, beyond just a hiccup, the developers can get flooded with e-mails.