Error Establishing A Database Connection

Reading Time: 3 minutes

Everyone has seen this, right? In essence, the error is pretty self-explanatory. Your PHP script/s cannot make a connection to the website’s database.

Before we try to troubleshoot the error let’s explain in brief how WordPress works and what programming languages are used. On the front-end(what you see in the browser) of your website, there are three languages that work together in order for you to see the well-structured and styled page. They are HTML(which is used to provide the structure of the web page), CSS(used to provide the styling of the web page) and there is JavaScript(used to add some interactivity). On the server-side there is the PHP programming language which makes the whole site dynamic and provides different functionalities. And there is one more language – MySQL the language of the database which is used for adding, editing, querying and deleting records and data from your database.

So whenever you type in your domain name in the browser and press Enter on your keyboard a request is made from the browser towards the server where the site is hosted. Once the listening server takes the query and then WordPress executes the PHP scripts. The PHP then evaluates the request and decides if it needs to make an additional request towards the database. For example, when you want to see a particular post of yours and all the posts are stored in the database the PHP is making a query towards the database to retrieve this post. Once this is all done the server sends back the requested information in the form of an HTML document. This document is then rendered again by the browser and presented to the user. And as we all know WordPress as a CMS needs a database. The default information stored in the database includes the users and their hashed passwords, the website URL, the active theme and the plugins installed. So we can see that if a connection to the database cannot be made then WordPress cannot function properly.

  502 Bad Gateway Error in WordPress

As we can see from the pointed above there is a connection between the PHP and the MySQL language and when this connection is not functioning as it should — an error occurs and this is when you see the Error establishing database connection in your browser because the PHP was not able “to talk” to the MySQL or vice versa.

After we have established what this error means let’s point some common fixes and checks that you can make on an attempt to troubleshoot this:

– the first and most common reason that this error occur is wrong database credentials. Check your wp-config.php file and verify the name of the database, the username, the password and the server.

– corrupted database – there might be some corrupted records, tables or even malware present in the database and this might make the database unusable. What you can try here is to enter the following line in your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

This will try to repair the database. And make sure you put it before the line ‘That’s all, stop editing! Happy blogging’ in the file.

– Server/hosting issue – sometimes there might be an issue with the database server or with the database service(daemon) that runs on the server. If you have tried to above two and you are still seeing this then this is likely to be the case. And you will need to get in touch with the hosting for further information.

  Fatal Error in WordPress

– Network connectivity issues. On most shared servers you can see that the database is hosted on the same server as the website and the used server is localhost. However, this is not always the case and your database server might be on another physical(or virtual) server and if the connection between both servers is not possible for some reason then the error will occur again.

So these are the most common reasons and fixes for this error. And you will definitely need to double-check the credentials as in the vast majority of the cases this is the root cause.

Inquire About Error Establishing A Database Connection

Share us all the details you have so we can start working on your WordPress website immediately!