Error Establishing a Database Connection WordPress Fix

by Vincy. Last modified on July 12th, 2022.

How to fix the “Error Establishing a Database Connection” error in WordPress. This is one of the most dreaded errors in WordPress. In this WordPress tutorial, we will see the causes of this WordPress error and how to fix it.

This error is frustrating for both the users as well as the WordPress blogger. Site downtime is costly and it will give a bad reputation. “Error Establishing a Database Connection” is thrown at the face of the user and we should be well aware of this WordPress error to treat it instantly.

A word of caution. Before doing any experiment for learning purposes, do remember to backup your WordPress.

Why does this WordPress Error Occur?

The reason for this WordPress error to be infamous is the causes behind it. There are many reasons that can cause this error.

  • Incorrect WordPress database configuration.
  • WordPress database corrupted.
  • WordPress files corrupted.
  • Database connection overload.

Incorrect WordPress database configuration

The first and foremost reason for “Error Establishing a Database Connection” error is, we forget to update the database configuration parameters. We might have a local WordPress installation and when changes are uploaded, wp-config.php with the local connection settings can get uploaded.

Changing the password in Database and forgetting to update it in wp-config.php file. All these are related to human errors. So the first place you got to verify if things are right is wp-config.php

/** The name of the database for WordPress */
define('DB_NAME', 'database_name');

/** MySQL database username */
define('DB_USER', 'root');

/** MySQL database password */
define('DB_PASSWORD', 'password');

/** MySQL hostname */
define('DB_HOST', 'localhost');

Following is a PHP code snippet to verify if your database connection setting is right. Create a new PHP file and put it inside the WordPress installation with the following content and invoke it.

<?php
$db = mysqli_connect('localhost', 'root', 'password','database_name');
if (!$db) {
die('Error connection database: ' . mysqli_error($db));
}
echo 'Database connected successfully!';
mysqli_close($db);
?>

I have heard instances that the database user’s access rights for the database have been revoked. To verify that the user is able to read the database.

WordPress database corrupted

WordPress database can get corrupted. It has happened to me. When the DB gets corrupted, obviously the application will not be able to connect to the database and do a query.

When this occurs sometimes it is possible that wp-admin will work and only the blog/website may get the “Error Establishing …” error. WordPress provides functionality to ‘repair the database’. We need to log in to use this functionality and you need to make a change to the settings to enable it. Open the wp-config.php file and add the following line.

define('WP_ALLOW_REPAIR', true);

Now go to the URL http://www.site.com/wp-admin/maint/repair.php and repair the database. Remember to back up the WordPress before attempting this. Once you have completed the repair, first remove these setting changes as it will allow anybody to access this WordPress feature.

WordPress-Database-Error-Repair

‘siteurl’ value is inappropriate

There can be situations when we need to fix the WordPress by importing an old backup. When we are importing old backup we should verify that the ‘siteurl’ value is set appropriately. If you are in doubt run the following query against your WordPress database.

UPDATE wp_options SET option_value='your_siteurl' WHERE option_name='siteurl';

WordPress files corrupted

One popular solution to this issue is uploading the WordPress files again. Just delete your WordPress files, and always remember to do a backup. After deleting, upload a fresh set of WordPress files.

For many people, this solution has worked. The reason behind this fix is, that the WordPress files may have got corrupted. After uploading a fresh WordPress, you should retain the wp-config.php and wp-content folder. Importantly the uploads folder.

Database connection overload

Your WordPress database can go down primarily for two reasons. You have outgrown your plan bandwidth. Which is in a way happy news.

Check if you have installed a cache plugin like WP Super Cache. If the cache is set and still you have got database issues, then most probably you need to acquire more DB bandwidth from your hosting provider.

The second reason could be because of your neighbor. In a shared hosting environment, a bad neighbor pulls a whole cluster down. In such a scenario, you can ask your hosting provider to move your database to a decent cluster.

Some parting tips,

  • Keep your WordPress slim by using a minimal number of third party plugins.
  • Always install only a popular plugin. Install a good cache plugin and keep its settings optimized.
  • Keep a strong password.
  • Update the WordPress regularly.
  • Take frequent regular backups.
Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page