WordPress: solve database connection problems


In this article we will try to understand when and why a database connection error appears in WordPress and we will offer some solutions to eliminate the problem.

Error In Establishing A Database Connection

WordPress is an open source CMS based on PHP and MySQL. All data stored on the site, with the exception of images and multimedia files, are stored in a database, which WordPress accesses to retrieve the information requested by the visitor. Each time the database is accessed, a series of data is requested that is stored in the <code>wp-config.php</code> installation file:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'nome_database');
/** MySQL database username */
define('DB_USER', 'utente_database');
/** MySQL database password */
define('DB_PASSWORD', 'password');
/** MySQL hostname */
define('DB_HOST', 'localhost');

If one or more of this information is incorrect, WordPress will display the error message shown in the following image.

wordpress-solve-database-connection-problems - 0001

However, it is not necessarily the case that the problem must necessarily reside in the file <code>wp-config.php</code>. The error can also be determined by a database whose data is corrupted. For example, one or more tables may have been damaged due to installations and uninstallations of plugins, themes or even the WordPress core. It is also good to keep in mind that an installation or an update may not be successful.

One of the most common causes of a failure during an upgrade can be, for example, the interruption of the server in the middle of the upgrade.

A similar problem can also arise when deciding to make changes to the database without taking due precautions: the manual modification of the data on the database, if performed without the necessary skills or without due caution, can lead to irretrievable loss of data.

Another fairly frequent cause is the loss or corruption of core Core files. This, as before, can happen due to a failed installation / update or, worse, due to violations of others.

An overload of the server is another possible cause of a database connection error, even if, in this case, it is normally a temporary problem.

How to solve the problem

The same recommendation of a previous article is also valid : it is always more than opportune to have a recent and functioning backup of the entire installation available . This allows you to solve most of the problems that may arise as a result of file or database corruption.

The analysis of the problem starts with checking access credentials. The procedure, of course, will be different depending on the administration software used by your host. The following image shows the Plesk Database page.

wordpress-solve-database-connection-problems - 0002

Also in Plesk, clicking on “Connection Info” you get the information of connection to the database. The password will not be shown, but you can reset it on the same page.

wordpress-solve-database-connection-problems - 0003

Who uses cPanel will have a slightly different interface.

wordpress-solve-database-connection-problems - 0004

You can compare the two software by accessing the online demos:

  • Plesk Demo
  • Demo cPanel

Once the connection data have been verified, if the problem persists, the automatic optimization of the WordPress database can be used. To use it you will need to add the following code in the file <code>wp-config.php</code>:

define( 'WP_ALLOW_REPAIR', true );
/* That's all, stop editing! Happy blogging. */

Save the file and upload it again to the root of the installation. Then access the page <code>{$your_site}/wp-admin/maint/repair.php</code>. A page will appear like the one shown in the image below:

wordpress-solve-database-connection-problems - 0005

Note that the activation of the repair mode will be visible to all, even to unauthenticated users. For this it is necessary to delete the instruction <code>define</code> at the end of the operation.

If this is not a database problem, then it may be a problem of files corrupted or accidentally removed or due to external interference. To reinstall the Core without losing data, simply withdraw a new WordPress installation package and, before uploading it to the server, delete the folder <code>wp-content</code> and file <code>wp-config-sample.php</code>.

This will overwrite all the files in the Core, including the damaged ones, without the contents of the website being touched.


Please enter your comment!
Please enter your name here